Difference between Load and LoadQuery() in CSOM SharePoint

Client-Side Object Model (CSOM) has two method for ClientContent to load the data: Load() and LoadQuery().

Let’s see what is the difference between the two and when can we use them.

Load() method populates the object directly with the data from the server like ListItemCollection. It retrieves the properties of a client object from the server. This object can be cleaned up by garbage collector ONLY when the ClientContext object is destroyed. Below is the example:

public static void main(string args[])
{
     ClientContext context = new ClientContext(“http://serverurl”);
     context.Load(context.Web.Lists);
     context.ExecuteQuery(); 
     Console.WriteLine(“Total number of lists in web: ” + context.Web.Lists.Count);
     Console.ReadLine();
}

LoadQuery() method returns entirely new collection of objects in IEnumerable format. These objects are separate from ClientContext, and can be destroyed anytime. It is flexible especially when working with more than one query, have better control over memory consumption and query processing is more efficient. Below is the example of LoadQuery()

public static void Main(string[] args)
{
     ClientContext context = new ClientContext(“http://serverurl”);
     IEnumerable allLists = context.LoadQuery(context.Web.Lists);
     context.ExecuteQuery();
     Console.WriteLine(“Total number of lists in web: ” + allLists.Count());
     Console.ReadLine();
}
Power Platform Academy

Start or Upgrade your Career with Power Platform

Learn with Akanksha

Python | Azure | AI/ML | OpenAI | MLOps

Design a site like this with WordPress.com
Get started