Share via


WebPart.AfterDeserialize method

Chamado após as propriedades de uma Web Part do SharePoint estão desserializadas do banco de dados do SharePoint ou de um arquivo de descrição de Web Part (. dwp). Local sugerido para código de atualização de Web Part do SharePoint.

Namespace:  Microsoft.SharePoint.WebPartPages
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaração
Public Overridable Sub AfterDeserialize
'Uso
Dim instance As WebPart

instance.AfterDeserialize()
public virtual void AfterDeserialize()

Comentários

O método AfterDeserialize oferece suporte a cenários de atualização, onde uma nova versão de uma Web Part precisa para acomodar alterações às propriedades que foram salvos para uma versão anterior de uma Web Part. Se um desenvolvedor de Web Part simplesmente adiciona novas propriedades de um ou mais, nenhuma lógica de atualização é necessária. No entanto, se as propriedades são excluídas ou renomeadas ou valores salvos forem alterados, e em seguida, substituindo o método AfterDeserialize fornece uma maneira de lidar com essas alterações quando uma Web Part do SharePoint são atualizados.

Examples

O exemplo de código a seguir mostra como lidar com uma situação quando uma ou mais propriedades foram removidas da versão mais recente de uma Web Part. Porque os elementos que o assembly não reconhece serão adicionados à coleção UnknownXmlElementCollection , o exemplo de código a seguir mostra a iteração através da coleção e removendo os elementos desconhecidos.

Para obter mais informações sobre como usar o método AfterDeserialize ao atualizar Web Parts, consulte Atualizando um Assembly de Web Part.

' 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.
    }
}

Ver também

Referência

WebPart class

WebPart members

Microsoft.SharePoint.WebPartPages namespace