Here is a nice concept I want to tell to my readers about what is the difference between the load() and loadQuery() methods. The main definition both will use to load the data or objects you want from the SharePoint server. Then where is the difference?
To reduce the confusion, first, we will discuss these in the Managed Client OM [C#].
1. Difference in syntax:
Managed Client object model supports two types of syntax in data retrieval process. One is Query syntax [Linq] and another is Method Syntax.
In Query syntax we are using Linq to create queries. Remember, here we are doing Linq to Objects querying but not Linq to SharePoint Provider. It means all the stuff we are writing is client side coding, we can't make or write queries to Linq to SharePoint provider. So, everything should be Linq to Objects only. LoadQuery() syntax can only be used with the query syntax. I mean if you want to write Linq query and execute against client object model, then we should use LoadQuery(). In other words, LoadQuery method takes an object of type IQueryable
var query = from listIn the above example, I have used the Linq query to find all the lists which are not hidden. So, this is a straight Linq query and we should use the LoadQuery() method to execute it and get result.
in clientContext.Web.Lists
where list.Hidden != false
select list;
var result = clientContext.LoadQuery(query);
clientContext.ExecuteQuery();
Method syntax can be used with either Load() or LoadQuery(). We can use Lambda expressions to query the data. We can use the same above example with lambda expressions as below.
clientContext.Load(clientContext.Web,It also produces the same result as above. But the only difference is syntax.
website => website.Lists.Include(
list => list.Title).Where(
list => list.Hidden != false));
clientContext.ExecuteQuery();
In the above example, we have used the Include phrase. What's is this? It's main advantage is for the performance and efficiency.
How it is improving performance? Because we are returning only the properties we need, not all from the server belongs to requested object. So, Include phrase will tell the server to return only the properties defined in me. In our example, it only asks for Title, so server returns only Title property and loads into the response. This will increase the performance for sure.
How to use Include in LoadQuery() syntax?
IEnumerablehiddenLists = clientContext.LoadQuery(
listCollection.Include(list => list.Title).Where(list => !list.Hidden));
2. Difference in implementation:
Load method populates the client object directly with what it gets data from the server. But LoadQuery returns the data as a completely new collection. For better understanding, LoadQuery() returns the data in the format of IEnumerable<object>. It means all the collection is of type IEnumerable but not straight object collection. For example, in the above example, we have tried to retrieve all lists from the server which are not hidden. If we use Load() method, it returns the ListCollection type. If we use LoadQuery() it returns the IEnumberable
- type instead of ListCollection. Because of this, when you use Load() method the data will be garbage collected if and only if the ClientContext object goes out of scope. Whereas LoadQuery() object will be garbage collected if the enumerable object goes out of scope irrespective of the ClientContext object.
Using ECMAScript LoadQuery() Vs Load()
What we have discussed about them in the Managed Client Object Model are same here except the server side concepts like garbage collection etc... We will discuss now on how we can implement them in the ECMAScript.
load():
var web = clientContext.get_web();loadQuery():
var lists = web.get_lists();
clientContext.Load(lists, 'Title');
var collList = clientContext.get_web().get_lists();For filtering data, we need to write the CAML Queries and pull information from server.
this.lists = clientContext.loadQuery(collList, 'Include(Title)');
Hope this whole concept is completely about the difference between and loadquery and load. And I believe you better understand it. Please let me know the feedback and questions if you have any.