Difference between the Load and Load Query in sharepoint 2013 CSOM

Difference between Load and Load Query in SharePoint 2013 CSOM

The Client Object Model has two load methods: Load() and LoadQuery().

This is must know questions for programmers who are working on Sharepoint, that what is difference between Load and Load Query in SharePoint.

Load method CSOM model  populates the client object directly with what it gets data from the server i.e. a collection object like ListItemCollection. so Load method gets the client object or client object collection that is passed into it.

LoadQuery method in CSOM model returns an entirely new collection of client objects.  as a completely new collection in IEnumerable format.
LoadQuery method instead returns an entirely new collection of client objects.

Lets see the example retrieving number of list by Load and LoadQuery Method in a console application.

By Load Method

class Program
{
static void Main(string[] args)
{
ClientContext ctx = new ClientContext(“http://serverurl”);
ctx.Load(ctx.Web.Lists);
ctx.ExecuteQuery();
Console.WriteLine(“” + ctx.Web.Lists.Count);
Console.ReadLine(“RETURN…”);
}

in above console application will write the count of lists in the web since the ctx.Web.Lists gets loaded with the lists in the context.

Now if we use loadquery in the above console application you will get CollectionNotInitialized exception while accessing the ctx.Web.Lists.Count property beacuse it returned  a completely new client object collection and ctx.Web.Lists property has not been populated.

 

Retrieving the Number of Lists via the LoadQuery Method.

class Program
{
static void Main(string[] args)
{
ClientContext ctx = new ClientContext(“http://serverurl”);
IEnumerable allLists = ctx.LoadQuery(ctx.Web.Lists);
ctx.ExecuteQuery();
Console.WriteLine(“” + allLists.Count());
Console.ReadLine(“RETURN…”);
}
}

Advantage of LoadQuery over the Load method

The Collections that you load using the Load() method are eligible for garbage collection only when the client context variable itself goes out of scope where as, in these collections go out of scope at the end of IEnumerable list.

LoadQuery over the Load method is its flexibility, especially when working with more than one query.

Leave a Reply

Your email address will not be published. Required fields are marked *

9 + 8 =


All Rights Reserved 2019 | DesignzRush (OPC) Pvt. Ltd.
Inline
Inline