del método WebPart.AfterDeserialize

Se llama después de las propiedades de elementos Web de SharePoint se deserialización desde la base de datos de SharePoint o desde un archivo de descripción de elementos Web (.dwp). Contexto sugerido para el código de actualización de elementos Web de SharePoint.

Espacio de nombres:  Microsoft.SharePoint.WebPartPages
Ensamblado:  Microsoft.SharePoint (en Microsoft.SharePoint.dll)

Sintaxis

'Declaración
Public Overridable Sub AfterDeserialize
'Uso
Dim instance As WebPart

instance.AfterDeserialize()
public virtual void AfterDeserialize()

Comentarios

El método AfterDeserialize es compatible con escenarios de actualización que necesita una nueva versión de un elemento Web para dar cabida a los cambios realizados en las propiedades que se han guardado para una versión anterior de un elemento Web. Si un programador del elemento Web simplemente agrega una o más propiedades nuevas, no se requiere ninguna lógica de actualización. Sin embargo, si las propiedades se ha eliminado o ha cambiado o se cambian los valores guardados, a continuación, reemplazar el método AfterDeserialize proporciona una forma para controlar estos cambios cuando un elemento Web de SharePoint que se actualiza.

Ejemplos

En el ejemplo de código siguiente se muestra cómo controlar una situación cuando una o varias propiedades se han quitado de la versión posterior de un elemento Web. Dado que los elementos que no puede reconocer el ensamblado se agregará a la colección UnknownXmlElementCollection , en el ejemplo de código siguiente se muestra iterar a través de esa colección y quitar los elementos desconocidos.

Para obtener más información acerca de cómo utilizar el método AfterDeserialize al actualizar elementos Web, vea actualizar un ensamblado de elementos Web.

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

Vea también

Referencia

clase WebPart

Miembros WebPart

Espacio de nombres Microsoft.SharePoint.WebPartPages