Export (0) Print
Expand All

Saving Data in Extensions of the SharePoint Project System

When you extend the SharePoint project system, you can save string data that persists after a SharePoint project is closed. The data is typically associated with a particular project item or with the project itself.

If you have data that does not need to persist, you can add the data to any object in the SharePoint tools object model that implements the IAnnotatedObject interface. For more information, see Associating Custom Data with SharePoint Tools Extensions.

When you have data that is associated with a particular SharePoint project item, such as the value of a property that you add to a project item, you can save the data to the .spdata file for the project item. To do this, use the ExtensionData property of an ISharePointProjectItem object. Data that you add to this property is saved in the ExtensionData element in the .spdata file for the project item. For more information, see ExtensionData Element.

The following code example demonstrates how to use the ExtensionData property to save the value of a string property that is defined in a custom SharePoint project item type. To see this example in the context of a larger example, see How to: Add a Property to a Custom SharePoint Project Item Type.

private const string PropertyId = "Contoso.ExampleProperty";
private const string PropertyDefaultValue = "This is an example property.";

[DisplayName("Example Property")]
[DescriptionAttribute("This is an example property for project items.")]
[DefaultValue(PropertyDefaultValue)]
public string ExampleProperty
{
    get
    {
        string propertyValue;

        // Get the current property value if it already exists; otherwise, return a default value. 
        if (!projectItem.ExtensionData.TryGetValue(PropertyId, out propertyValue))
        {
            propertyValue = PropertyDefaultValue;
        }
        return propertyValue;
    }
    set
    {
        if (value != PropertyDefaultValue)
        {
            // Store the property value in the ExtensionData property of the project item.  
            // Data in the ExtensionData property persists when the project is closed.
            projectItem.ExtensionData[PropertyId] = value;
        }
        else
        {
            // Do not save the default value.
            projectItem.ExtensionData.Remove(PropertyId);
        }
    }
}

When you have project-level data, such as the value of a property that you add to SharePoint projects, you can save the data to the project file (the .csproj file or .vbproj file) or the project user option file (the .csproj.user file or .vbproj.user file). The file you choose to save the data in depends on how you want the data to be used:

  • If you want the data to be available to all developers who open the SharePoint project, save the data to the project file. This file is always checked in to source control databases, so the data in this file is available to other developers who check out the project.

  • If you want the data to be available only to the current developer who has the SharePoint project open in Visual Studio, save the data to the project user option file. This file is not typically checked in to source control databases, so the data in this file is not available to other developers who check out the project.

To save data to the project file, convert an ISharePointProject object to an IVsBuildPropertyStorage object, and then use the IVsBuildPropertyStorage.SetPropertyValue method. The following code example demonstrates how to use this method to save the value of a project property to the project file. To see this example in the context of a larger sample, see How to: Add a Property to SharePoint Projects.

private ISharePointProject sharePointProject;
private IVsBuildPropertyStorage projectStorage;
private const string ProjectFilePropertyId = "ContosoCustomProjectFileProperty";
private const string ProjectFilePropertyDefaultValue = "Default";

public CustomProjectProperties(ISharePointProject myProject)
{
    sharePointProject = myProject;
    projectStorage = sharePointProject.ProjectService.Convert<ISharePointProject, IVsBuildPropertyStorage>(sharePointProject);
}

[DisplayName("Custom Project File Property")]
[DescriptionAttribute("This property is saved to the .csproj/.vbproj file.")]
[DefaultValue(ProjectFilePropertyDefaultValue)]
public string CustomProjectFileProperty
{
    get
    {
        string propertyValue;
        int hr = projectStorage.GetPropertyValue(ProjectFilePropertyId, string.Empty, 
            (uint)_PersistStorageType.PST_PROJECT_FILE, out propertyValue);

        // Try to get the current value from the project file; if it does not yet exist, return a default value. 
        if (!ErrorHandler.Succeeded(hr) || String.IsNullOrEmpty(propertyValue))
        {
            propertyValue = ProjectFilePropertyDefaultValue;
        }

        return propertyValue;
    }

    set
    {
        // Do not save the default value. 
        if (value != ProjectFilePropertyDefaultValue)
        {
            projectStorage.SetPropertyValue(ProjectFilePropertyId, string.Empty, 
                (uint)_PersistStorageType.PST_PROJECT_FILE, value);
        }
    }
}

For more information about converting ISharePointProject objects to other types in the Visual Studio automation object model or integration object model, see Converting Between SharePoint Project System Types and Other Visual Studio Project Types.

To save data to the project user option file, use the ProjectUserFileData property of an ISharePointProject object. The following code example demonstrates how to use this property to save the value of a project property to the project user option file. To see this example in the context of a larger sample, see How to: Add a Property to SharePoint Projects.

private const string UserFilePropertyId = "ContosoCustomUserFileProperty";
private const string UserFilePropertyDefaultValue = "Default";

[DisplayName("Custom Project User File Property")]
[DescriptionAttribute("This property is saved to the .user file.")]
[DefaultValue(UserFilePropertyDefaultValue)]
public string CustomUserFileProperty
{
    get
    {
        string propertyValue = string.Empty;

        // Try to get the current value from the .user file; if it does not yet exist, return a default value. 
        if (!sharePointProject.ProjectUserFileData.TryGetValue(UserFilePropertyId, out propertyValue))
        {
            propertyValue = UserFilePropertyDefaultValue; 
        }

        return propertyValue; 
    }

    set
    {
        // Do not save the default value. 
        if (value != UserFilePropertyDefaultValue)
        {
            sharePointProject.ProjectUserFileData[UserFilePropertyId] = value;
        }
    }
}                
Show:
© 2014 Microsoft