WebPart.AfterDeserialize - Méthode

Appelé après que les propriétés d'un composant WebPart SharePoint désérialisation à partir de la base de données SharePoint ou d'un fichier de description de composant WebPart (.dwp). Place suggéré pour le code de mise à niveau de composant WebPart SharePoint.

Espace de noms :  Microsoft.SharePoint.WebPartPages
Assembly :  Microsoft.SharePoint (dans Microsoft.SharePoint.dll)

Syntaxe

'Déclaration
Public Overridable Sub AfterDeserialize
'Utilisation
Dim instance As WebPart

instance.AfterDeserialize()
public virtual void AfterDeserialize()

Remarques

La méthode AfterDeserialize prend en charge les scénarios de mise à niveau où une nouvelle version d'un composant WebPart doit tenir compte des modifications apportées aux propriétés qui ont été enregistrées pour une version antérieure d'un composant WebPart. Si un développeur de composant WebPart ajoute simplement une ou plusieurs nouvelles propriétés, aucune logique de mise à niveau n'est requis. Toutefois, si les propriétés sont supprimées ou renommées, ou les valeurs enregistrées sont modifiés, substituez la méthode AfterDeserialize offre un moyen pour gérer ces modifications lorsqu'un composant WebPart SharePoint sont mis à niveau.

Exemples

L'exemple de code suivant montre comment gérer les situations où une ou plusieurs propriétés ont été supprimées de la dernière version d'un composant WebPart. Étant donné que les éléments qui ne reconnaît pas l'assembly seront ajoutés à la collection UnknownXmlElementCollection , l'exemple de code suivant montre itération dans la collection et supprimer des éléments inconnus.

Pour plus d'informations sur l'utilisation de la méthode AfterDeserialize lors de la mise à niveau des composants WebPart, voir mise à niveau d'un Assembly de composant WebPart.

' Override the AfterDeserialize method to remove any items in 
' the UnknownXmlElementCollection within the MyWebPartNamespace
Public Overrides Sub AfterDeserialize()
    Try
       Dim itemsRemoved As Boolean = False
       If UnknownXmlElements.Count > 0 Then
          Dim element As XmlElement
          Dim index As Integer = UnknownXmlElements.Count - 1

          ' Loop from the end of the collection to eliminate the need to
          ' update the index based on whether or not an item was deleted. 
          While index >= 0
             element = UnknownXmlElements(index)

            ' Check if the element is within the namespace.
            ' Do a case-insensitive match to be consistent with the
            ' XmlSerializer.
            If element.NamespaceURI = "MyWebPartNamespace" Then
               ' If the element is in the namespace, remove it 
               ' from the collection.
               UnknownXmlElements.RemoveAt(index)
               itemsRemoved = True
            End If
            index -= 1
          End While
      End If
      ' If your assembly is installed in the application's bin directory, which defaults to minimal trust, 
      ' make sure that your Web Part assembly has UnsafeSaveOnGet permission or the following 
      ' attempt to save changes will return an error.  
      If itemsRemoved Then
         SaveProperties = True
      End If
   Catch Else
       ' Handle or ignore errors.
   End Try
End Sub
// Override the AfterDeserialize method to remove any items in 
// the UnknownXmlElementCollection within the MyWebPartNamespace
public override void AfterDeserialize()
{
    try
    {
        bool itemsRemoved = false;
        if (UnknownXmlElements.Count>0)
        {
            XmlElement element;
            int index = UnknownXmlElements.Count - 1;

            // Loop from the end of the collection to eliminate the need to
            // update the index based on whether or not an item was deleted. 
            while (index >= 0)
            {
                element = UnknownXmlElements[index];

                // Check if the element is within the namespace.
                // Do a case-insensitive match to be consistent with the
                // XmlSerializer.
                if (element.NamespaceURI == "MyWebPartNamespace")
                {
                    // If the element is in the namespace, remove it from the collection.
                    UnknownXmlElements.RemoveAt(index);
                    itemsRemoved = true;
                }
                --index;
            }
        }
        // If your assembly is installed in the application's bin directory, which defaults to minimal trust, 
        // make sure that your Web Part assembly has UnsafeSaveOnGet permission or the following 
        // attempt to save changes will return an error.  
        if (itemsRemoved)
        {
            SaveProperties = true;
        }
    }
    catch
    {
         // Handle or ignore errors.
    }
}

Voir aussi

Référence

WebPart classe

WebPart - Membres

Microsoft.SharePoint.WebPartPages - Espace de noms