Tuesday, April 8, 2014

Difference between LoadQuery() and LoadDataQuery() in SharePoint 2010


In this post I will discuss what is the difference between LoadQuery() and LoadDataQuery() in SharePoint 2010.

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

Load method populates the client object directly with what it gets data from the server i.e. a collection object like ListItemCollection etc. But LoadQuery returns the data as a completely new collection in IEnumerable format.

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.

The Load() method does an in-place loading of the data returned from the server. All of the data returned from the server is stored and tracked inside of the ClientContext object. The ClientContext tracks the object IDs of the items and properties returned from the server.

The LoadQuery() method returns the results as new objects. These objects are not part of the ClientContext and a result can be easily managed and discarded when not needed any more.

Example LoadQuery():
 
var query = from list in clientContext.Web.Lists  
where list.Hidden != false  
select list;  
 
var result = clientContext.LoadQuery(query);  
clientContext.ExecuteQuery(); 

Example Load():
 
var web = clientContext.get_web();  
var lists = web.get_lists();  
clientContext.Load(lists, 'Title');

No comments: