Recuperación de datos de grupo en el mismo objeto

Última modificación: viernes, 27 de mayo de 2011

Hace referencia a: SharePoint Foundation 2010

Disponible en SharePoint Online

Para mejorar el rendimiento, intente siempre agrupar las solicitudes de recuperación de datos en el mismo objeto.

En el ejemplo siguiente, tanto Method1 como Method2 recuperan el título y la descripción de un sitio web y la descripción de la lista de anuncios, pero el rendimiento de Method1 es superior al de Method2.

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class RetrieveData
    {
        // The following example increases performance by grouping the Title and Description 
        // property calls together in the same Load method call.
        static void Method1()
        {
            ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
            Web oWebsite = clientContext.Web;
            List oList = oWebsite.Lists.GetByTitle("Announcements");

            clientContext.Load(oWebsite, 
                website => website.Title, 
                website => website.Description);

            clientContext.Load(oList, 
                list => list.Description);

            clientContext.ExecuteQuery();
        }

        // The following example decreases performance because the Title and Description
        // property calls are not grouped together in the same Load method call.
        static void Method2()
        {
            ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
            Web oWebsite = clientContext.Web;
            List oList = oWebsite.Lists.GetByTitle("Announcements");

            clientContext.Load(oWebsite, 
                website => website.Title);

            clientContext.Load(oList, 
                list => list.Description);

            clientContext.Load(oWebsite, 
                website => website.Description);

            clientContext.ExecuteQuery();
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class RetrieveData

        // The following example increases performance by grouping the Title and Description 
        // property calls together in the same Load method call.
        Shared Sub Method1()
            Dim clientContext As New ClientContext("http://MyServer/sites/MySiteCollection")
            Dim oWebsite As Web = clientContext.Web
            Dim oList As List = oWebsite.Lists.GetByTitle("Announcements")
         
            clientContext.Load(oWebsite, Function(website) website.Title, _
                Function(website) website.Description)

            clientContext.Load(oList, Function(list) list.Description)
         
            clientContext.ExecuteQuery()
        End Sub 'Method1
      
        ' The following example decreases performance because the Title and Description
        ' property calls are not grouped together in the same Load method call.      
        Shared Sub Method2()
            Dim clientContext As New ClientContext("http://MyServer/sites/MySiteCollection")
            Dim oWebsite As Web = clientContext.Web
            Dim oList As List = oWebsite.Lists.GetByTitle("Announcements")
         
            clientContext.Load(oWebsite, Function(website) website.Title)

            clientContext.Load(oList, Function(list) list.Description)

            clientContext.Load(oWebsite, Function(website) website.Description)
         
            clientContext.ExecuteQuery()

        End Sub
    End Class
End Namespace
// The following example increases performance by grouping the Title and Description 
// property calls together in the same Load method call.
function Method1 () {

        var clientContext = new SP.ClientContext('/sites/TestWebs/TestWeb1');
        this.oWebsite = clientContext.get_web();
        this.oList = oWebsite.get_lists().getByTitle('Announcements');

        clientContext.load(oWebsite, 'Title', 'Description');
        clientContext.load(oList, 'Description');
        
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

// The following example decreases performance because the Title and Description
// property calls are not grouped together in the same Load method call.
function Method2 () {

        var clientContext = new SP.ClientContext('/sites/TestWebs/TestWeb1');
        this.oWebsite = clientContext.get_web();
        this.oList = oWebsite.get_lists().getByTitle('Announcements');

        clientContext.load(oWebsite, 'Title');
        clientContext.load(oList, 'Description');
        clientContext.load(oWebsite, 'Description');
        
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

En Method1, el código para recuperar el título y la descripción del sitio web está agrupado. En Method2, el código para recuperar esas mismas propiedades está separado por otra acción. Method2 desencadena dos consultas separadas en el mismo objeto de sitio web y, por lo tanto, hay dos conjuntos de resultados para el mismo objeto. Debido a que la biblioteca cliente tiene el objetivo de devolver datos coherentes, el segundo conjunto de resultados incluye el título y la descripción del sitio web y el primer conjunto de resultados también incluye el título del sitio web.

La diferencia entre los métodos anteriores se ilustra mediante la diferencia entre los siguientes comandos de SQL.

Method1:
SELECT Title, Description FROM Webs WHERE ...
SELECT Description FROM Lists WHERE ...

Method2:
SELECT Title FROM Webs WHERE ...
SELECT Description FROM Lists WHERE ...
SELECT Title, Description FROM Webs WHERE ...

Vea también

Conceptos

Introducción a la recuperación de datos

Llamada a Load y ExecuteQuery antes de obtener acceso a las propiedades del valor

Los objetos valor no se pueden usar entre métodos en una consulta

Los objetos de cliente pueden usarse en métodos en una consulta

La recuperación de un objeto de cliente no recupera todas las propiedades