Condividi tramite


Salvataggio dei dati nelle estensioni del sistema di progetto SharePoint

Quando si estende il sistema di progetto SharePoint è possibile salvare dati di tipo stringa che vengono conservati dopo la chiusura di un progetto SharePoint.I dati sono in genere associati a un elemento di progetto particolare o al progetto stesso.

Se si dispone di dati che non è necessario salvare in modo permanente, è possibile aggiungerli a qualsiasi oggetto nel modello a oggetti degli strumenti di SharePoint che implementa l'interfaccia IAnnotatedObject.Per ulteriori informazioni, vedere Associazione di dati personalizzati alle estensioni degli strumenti di SharePoint.

Salvataggio di dati associati a un elemento di progetto

Quando si dispone di dati associati a un elemento del progetto SharePoint specifico, ad esempio il valore di una proprietà che si aggiunge a un elemento di progetto, è possibile salvare i dati nel file con estensione spdata per l'elemento di progetto.Per effettuare questa operazione, utilizzare la proprietà ExtensionData di un oggetto ISharePointProjectItem.I dati aggiunti a questa proprietà vengono salvati nell'elemento ExtensionData nel file con estensione spdata dell'elemento di progetto.Per ulteriori informazioni, vedere Elemento ExtensionData.

Nell'esempio di codice seguente viene illustrato come utilizzare la proprietà ExtensionData per salvare il valore di una proprietà di tipo stringa definita in un tipo di elemento di progetto SharePoint personalizzato.Per vedere questo esempio nel contesto di un esempio più esaustivo, vedere Procedura: aggiungere una proprietà a un tipo di elemento di progetto SharePoint personalizzato.

Private Const TestPropertyId As String = "Contoso.ExampleProperty"
Private Const PropertyDefaultValue As String = "This is an example property."

<DisplayName("Example Property")> _
<DescriptionAttribute("This is an example property for project items.")> _
<DefaultValue(PropertyDefaultValue)> _
Public Property ExampleProperty As String
    Get
        Dim propertyValue As String = Nothing

        ' Get the current property value if it already exists; otherwise, return a default value.
        If False = projectItem.ExtensionData.TryGetValue(TestPropertyId, propertyValue) Then
            propertyValue = PropertyDefaultValue
        End If
        Return propertyValue
    End Get
    Set(ByVal value As String)
        If value <> PropertyDefaultValue Then
            ' 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(TestPropertyId) = value
        Else
            ' Do not save the default value.
            projectItem.ExtensionData.Remove(TestPropertyId)
        End If
    End Set
End Property
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);
        }
    }
}

Salvataggio di dati associati a un progetto

Quando si dispone di dati a livello di progetto, ad esempio il valore di una proprietà aggiunta ai progetti SharePoint, è possibile salvare i dati nel file di progetto (con estensione csproj o vbproj) o nel file delle opzioni utente del progetto (il file csproj.user o vbproj.user).I file in cui si sceglie di salvare i dati dipende dalla modalità di utilizzo del dati desiderata:

  • Se si desidera che i dati siano disponibili per tutti gli sviluppatori che aprono il progetto SharePoint, salvare i dati nel file di progetto.Questo file viene sempre archiviato nei database del controllo del codice sorgente, in modo i dati presenti nel file siano a disposizione degli altri sviluppatori che estraggono il progetto.

  • Se si desidera che i dati siano disponibili solo per lo sviluppatore corrente che ha il progetto SharePoint aperto in Visual Studio, salvare i dati nel file delle opzioni utente del progetto.Questo file non viene generalmente archiviato nei database del controllo del codice sorgente, pertanto i dati presenti nel file non sono a disposizione degli altri sviluppatori che estraggono il progetto.

Ff406949.collapse_all(it-it,VS.110).gifSalvataggio dei dati nel file di progetto

Per salvare i dati nel file di progetto, convertire un oggetto ISharePointProject in un oggetto IVsBuildPropertyStorage, quindi utilizzare il metodo IVsBuildPropertyStorage.SetPropertyValue.Nell'esempio di codice riportato di seguito viene illustrato come utilizzare il metodo per salvare il valore di una proprietà del progetto nel file di progetto.Per vedere questo esempio nel contesto di un esempio più esaustivo, vedere Procedura: aggiungere una proprietà ai progetti SharePoint.

Private sharePointProject As ISharePointProject
Private projectStorage As IVsBuildPropertyStorage
Private Const ProjectFilePropertyId As String = "ContosoCustomProjectFileProperty"
Private Const ProjectFilePropertyDefaultValue As String = "Default"

Public Sub New(ByVal myProject As ISharePointProject)
    sharePointProject = myProject
    projectStorage = sharePointProject.ProjectService.Convert(Of ISharePointProject, IVsBuildPropertyStorage)(sharePointProject)
End Sub

<DisplayName("Custom Project File Property")> _
<DescriptionAttribute("This property is saved to the .csproj/.vbproj file.")> _
<DefaultValue(ProjectFilePropertyDefaultValue)> _
Public Property CustomProjectFileProperty As String
    Get
        Dim propertyValue As String = String.Empty
        Dim hr As Integer = projectStorage.GetPropertyValue(ProjectFilePropertyId, String.Empty, _
            CUInt(_PersistStorageType.PST_PROJECT_FILE), propertyValue)

        ' Try to get the current value from the project file; if it does not yet exist, return a default value.
        If Not ErrorHandler.Succeeded(hr) Or String.IsNullOrEmpty(propertyValue) Then
            propertyValue = ProjectFilePropertyDefaultValue
        End If
        Return propertyValue
    End Get
    Set(ByVal value As String)
        ' Do not save the default value.
        If value <> ProjectFilePropertyDefaultValue Then
            projectStorage.SetPropertyValue(ProjectFilePropertyId, String.Empty, _
                CUInt(_PersistStorageType.PST_PROJECT_FILE), value)
        End If
    End Set
End Property
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);
        }
    }
}

Per ulteriori informazioni sulla conversione degli oggetti ISharePointProject in altri tipi di oggetti nel modello a oggetti di automazione o di integrazione di Visual Studio, vedere Conversione tra tipi di sistemi di progetto SharePoint e altri tipi di progetto Visual Studio.

Ff406949.collapse_all(it-it,VS.110).gifSalvataggio dei dati nel file delle opzioni utente del progetto

Per salvare i dati nel file delle opzioni utente del progetto, utilizzare la proprietà ProjectUserFileData di un oggetto ISharePointProject.Nell'esempio di codice riportato di seguito viene illustrato come utilizzare la proprietà per salvare il valore di una proprietà del progetto nel file delle opzioni utente del progetto.Per vedere questo esempio nel contesto di un esempio più esaustivo, vedere Procedura: aggiungere una proprietà ai progetti SharePoint.

Private Const UserFilePropertyId As String = "ContosoCustomUserFileProperty"
Private Const UserFilePropertyDefaultValue As String = "Default"

<DisplayName("Custom Project User File Property")> _
<DescriptionAttribute("This property is saved to the .user file.")> _
<DefaultValue(UserFilePropertyDefaultValue)> _
Public Property CustomUserFileProperty As String
    Get
        Dim propertyValue As String = String.Empty
        ' Try to get the current value from the .user file; if it does not yet exist, return a default value.
        If Not sharePointProject.ProjectUserFileData.TryGetValue(UserFilePropertyId, propertyValue) Then
            propertyValue = UserFilePropertyDefaultValue
        End If
        Return propertyValue
    End Get
    Set(ByVal value As String)
        ' Do not save the default value.
        If value <> UserFilePropertyDefaultValue Then
            sharePointProject.ProjectUserFileData(UserFilePropertyId) = value
        End If
    End Set
End Property
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;
        }
    }
}                

Vedere anche

Concetti

Estensione del sistema di progetto SharePoint

Associazione di dati personalizzati alle estensioni degli strumenti di SharePoint

Conversione tra tipi di sistemi di progetto SharePoint e altri tipi di progetto Visual Studio