Sharepoint - Difference in clientcontext.load() and clientcontext.executeQueryAsync() in javascript for SP

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.

Tags:

Javascript