15

Please tell me the working of Load() and executeQueryAsync() in javascript for sharepoint 2013 i.e why we have to call Load() method when we have to call executeQueryAsync() .

25

The Load and ExecuteQuery combo (or ExecuteQueryAsync) is a necessary evil of any client-server programming. Your data resides in a remote location (your SharePoint server), so you cannot directly access it. In fact this is true even of any server-side programming, it is only less obvious.

When you call Load, you are only preparing your query. As this article states, in SQL terms think of it of building your SELECT statement.

You then call ExecuteQuery (or ExecuteQueryAsync, I'll get to the difference in a minute), to submit your prepared query to the remote server and retrieve the specified data.

The difference between ExecuteQuery and ExecuteQueryAsync is whether you will wait for the web request to return in-place (in the same execution path) or you will submit a callback function to handle the return when it does come back. It has an impact on how you will structure your code, but not so much on the end result.

So, to be clear:

  • No communication occurs with the remote server until you call ExecuteQuery or ExecuteQueryAsync
  • You can build up your query by calling Load multiple times, even for properies of different objects, and the client API will construct the appropriate query.
  • You can narrow down the exact properties to fetch so as to be as brief as possible in your web traffic

Basically these are typical client-server communication patterns, and not at all unique to SharePoint.

  • 11
    To be clear ExecuteQuery does not exist in the JavaScript version of the SharePoint client object model. You can only work asynchronously (since your question is tagged for JS and not C#) and so you must use executeQueryAsync() – John-M Jan 13 '15 at 21:57
  • @John-M Yes excellent comment, I didn't look at the tags :S. One could also use Promises to sort of get around the spaghetti code of async calls and callbacks. – Louis Jan 15 '15 at 0:28
  • Very nice @Louis – Thirusanguraja Venkatesan Jan 28 '16 at 10:54
0

Load is to prepare query what you want from the server and executeQueryAsync/executeQuery is to get the data from the server

-2

For LOAD: Reference: http://www.codeproject.com/Articles/399156/SharePoint-Client-Object-Model-Introduction

ClientContext context = new ClientContext("http://test.com");
    List list = context.Web.Lists.GetByTitle("Tasks");
    context.Load(list);
    context.ExecuteQuery();

    Console.WriteLine(list.Title);
    Console.ReadKey(false);

In above example, you 1st have to load the LIST so that you can EXECUTE the query against it.

 ClientContext context = new ClientContext(ServerText.Text); 

List list = context.Web.Lists.GetByTitle("Tasks"); CamlQuery query = new CamlQuery(); query.ViewXml = "<View/>"; ListItemCollection items = list.GetItems(query);

    context.Load(items);

    context.ExecuteQuery();

    foreach(ListItem item in items)
        if ((item.Id % 2) == 0)
        {
            item["Title"] += "**";
            item.Update();
        }

    context.ExecuteQuery();

In above example you have to LOAD the listitems to EXECUTE query against it.

In above cases you could have also LOADED the web but that puts lot of load and effects performance.. Since it is the list or list items against which you are querying it is sufficient to LOAD only the listitems in above case.

Suppose you want to get the name of the web, then, you have to LOAD the web before executing the web.name...

executeQueryAsync is a different concept.. http://sharepointfordeveloper.blogspot.in/2012/06/sharepoint-2010-ecma-scripts-code.html

Use the asynchronous method executeQueryAsync to retrieve the information from SharePoint. This method needs a reference to a callback method when the execution succeeds and a reference to a callback method when the execution fails.

1. Load Site Data  




 <script type="text/javascript">
    var clientContext = null;
    var web = null;
    ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
    function Initialize()
    {
    clientContext = new SP.ClientContext.get_current();
    web = clientContext.get_web();
    clientContext.load(web, 'Title');
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onSiteLoadSuccess), Function.createDelegate(this, this.onQueryFailed));
    }
    function onSiteLoadSuccess(sender, args) {
     alert("site title : " + web.get_title());
    }

    function onQueryFailed(sender, args) {
     alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
    }</script>​

2.Load List Items Data 



  <script type="text/javascript">
    var clientContext = null;
    var web = null;
    ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
    function Initialize()
    {
    clientContext = new SP.ClientContext.get_current();
    web = clientContext.get_web();
    var list = web.get_lists().getByTitle("Pages");
    var camlQuery = new SP.CamlQuery();
    var q = '<View><RowLimit>5</RowLimit></View>';
    camlQuery.set_viewXml(q);
    this.listItems = list.getItems(camlQuery);
    clientContext.load(listItems, 'Include(DisplayName,Id)');
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemsLoadSuccess),
    Function.createDelegate(this, this.onQueryFailed));
    }
    function onListItemsLoadSuccess(sender, args) {
     var listEnumerator = this.listItems.getEnumerator();
     //iterate though all of the items
     while (listEnumerator.moveNext()) {
         var item = listEnumerator.get_current();               
         var title = item.get_displayName();
         var id = item.get_id();
                alert("List title : " + title + "; List ID : "+ id);
            }
    }

    function onQueryFailed(sender, args) {
     alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
    }</script>​
  • 1
    It means Load() method is like getting the table and executeQuery() is like applying query on it and getting the result. – nbi Jul 16 '14 at 5:41
  • Thats, right, let others also confirm to this answer... :) – variable Jul 16 '14 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.