Second Life of a Hungarian SharePoint Geek

November 25, 2012

How to reduce the number of requests sent to the server when working with the Client Object Model?

Filed under: JSCOM, Managed Client OM, SP 2010 — Tags: , , — Peter Holpar @ 21:27

Assume you need to access data on SharePoint server. The information you need consists of a batch of similar items, for example, properties (like name) of users having their ID from a specific array, or the title of sites having their URL from an array as well. In the case of users (or other entities represented via list items) you could use CAML (see my former post for a similar, simple JSCOM example), although the query itself would be rather complex after a few dozens of items, however, other cases (like site names) might raise more serious issues.

You could send the requests consecutively, sending the next request only after you received the response for the former one, or you can send a set of asynchronous requests one after the other, and then process the responses parallel. I found neither of these options to be ideal. On one side, the high number of roundtrips between the client and server won’t help performance, on the other side, synchronizing requests and responses (have we received all the responses for our requests?) could be a nightmare.

Instead of these ones, we can implement a solution, where we cumulate all of the requests into a single context, then send the query at once, and process the single response.

Below I provide an example of this implementation for the managed client object model:

  1. List<int> lookupids = new List<int> { 19, 23, 32 };
  2. List<ListItem> userInfos = new List<ListItem>();
  3. using (ClientContext clientContext = new ClientContext("http://yoursharepointsite&quot;))
  4. {
  5.     lookupids.ForEach(id =>
  6.         {
  7.             ListItem userInfo = clientContext.Web.SiteUserInfoList.GetItemById(id);
  8.             userInfos.Add(userInfo);
  9.             clientContext.Load(userInfo);
  10.         });
  11.     clientContext.ExecuteQuery();
  12.     IEnumerable<string> userEmails = userInfos.Select(userInfo => userInfo["EMail"].ToString());
  13.     Console.WriteLine(String.Join(", ", userEmails.ToArray()));
  14. }

and the JavaScript version as well:

  1. <script type="text/javascript">
  2.  
  3. var siteUrl = '/';
  4. var userInfos;
  5.  
  6. function retrieveUsers() {
  7.     var ids = [19, 23, 32];
  8.     userInfos = new Array();
  9.  
  10.     var clientContext = new SP.ClientContext(siteUrl);
  11.     var web = clientContext.get_web();
  12.     var userList = web.get_siteUserInfoList();
  13.  
  14.     for(i=0; i<ids.length; i++) {
  15.         var id = ids[i];
  16.         var userInfo = userList.getItemById(id);
  17.         clientContext.load(userInfo);
  18.         userInfos[i] = userInfo;
  19.     }
  20.  
  21.     clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        
  22. }
  23.  
  24. function onQuerySucceeded(sender, args) {  
  25.     var result = '';
  26.     for(i=0; i<userInfos.length; i++)
  27.     {
  28.         var userInfo = userInfos[i];
  29.         result += userInfo.get_item('Name') + ', ' + userInfo.get_item('Title') + ', ' + userInfo.get_item('EMail') + '\n';
  30.     }  
  31.     alert(result);
  32. }
  33.  
  34. function onQueryFailed(sender, args) {
  35.  
  36.     alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
  37. }
  38.  
  39. ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.ribbon.js");
  40.  
  41. </script>

Note: The key factor of this solution is that you subsequent queries must be independent from the responses of the former requests.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: