Skip to main content

Weblog Ton Stegeman [MVP]

Go Search
Home
  

ODC 2008
If you have a question or suggestion, please contact me through Windows Live Messenger.
My status: .

If I am not online, please send me an e-mail.
Weblog Ton Stegeman [MVP] > Posts > SharePoint 2010 Silverlight Client Object Model – ExecuteQuery vs ExecuteQueryAsync
SharePoint 2010 Silverlight Client Object Model – ExecuteQuery vs ExecuteQueryAsync

I have written some weblog articles (ECMAScript and Managed models) and an article for the Dutch .NET Magazine on the new SharePoint 2010 client object models. Last week I did a presentation for the SDE event of the SDN on this subject. While presenting the Silverlight demo described in this blog post, one of the attendees was paying more attention than I did when writing the code. When I explained the code snippet below, the question was why I was using ExecuteQuery and I just explained the Silverlight API is asynchronous.

   1: ClientContext context = ClientContext.Current;
   2: Web web = context.Web;
   3: String listName = "CommentsList";
   4: ListCollection lists = web.Lists;
   5: IEnumerable<List> resultLists = context.LoadQuery(lists.Where(list => list.RootFolder.Name == listName));
   6: context.ExecuteQuery();
   7: List commentsList = resultLists.FirstOrDefault();
   8: if (commentsList != null)
   9: {
  10:     CamlQuery camlQuery = new CamlQuery();
  11:     camlQuery.ViewXml = "<View><Query><Where><IsNotNull><FieldRef Name='City'/></IsNotNull></Where></Query></View>";
  12:     _comments = commentsList.GetItems(camlQuery);
  13:     context.Load(_comments,
  14:          items => items.Include(
  15:             item => item.Id,
  16:             item => item["Title"],
  17:             item => item["Date"],
  18:             item => item["City"]));
  19:  
  20:     ClientRequestSucceededEventHandler success = new ClientRequestSucceededEventHandler(SuccesHandler);
  21:     ClientRequestFailedEventHandler failure = new ClientRequestFailedEventHandler(FailureHandler);
  22:     context.ExecuteQueryAsync(success, failure);
  23: }

 

In the presentation I did not have a very good answer to that question (other than ‘uhhhmmmm’). Time to sort it out and get a better answer. This MSDN article describes the asynchronous way of querying SharePoint 2010. This is why I am using this method to submit the CAML query. To get a reference to the SharePoint list, I copied a code snippet from another demo. I am using the RootFolder name to find the list that I want to use, and therefore I need an extra call to SharePoint. I could have used Lists.GetByTitle instead. So I am using an extra roundtrip to SharePoint, in a synchronous call, to get the reference to the list. So apparently the Silverlight client object model is also synchronous. After some more digging in the SDK I found the answer in the article Client Context as Central Object. This is a quote from that page:

The Silverlight client object model provides both an ExecuteQuery() method, which can be called synchronously from threads that do not modify the user interface (UI), and an asynchronous ExecuteQueryAsync() method for cases where threads do modify the UI.

Conclusion is the Silverlight client object model can handle both scenarios. If your code does not modify the UI directly, you can use the synchronous method. If your application directly updates the interface, you need to use ExecuteQueryAsync. In my application the snippet above is running in a background thread that does not update the UI, so both methods work. There was no real need to use the asynchronous method.

Thanks for paying attention in the session and asking the question!

Comments

RSS Feeds

Would you be starting RSS feeds for your blog?
at 1/16/2010 3:04 PM

Feed available

Hi, the RSS feed is available here:
http://feeds.feedburner.com/tonstegeman
Ton Stegeman at 1/31/2010 6:38 AM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


Your city *


Type the name of the city you live in (making it easier to handle spam...)

CurrentDate *

Select the current date (see if this gives me fewer spam...)
Attachments

 Links

  SharePoint Object on CodePlex
  Screencast introducing SharePoint Objects
  Content by Type and Filter Web Parts on CodePlex
  Archive
  Archive (Calendar)

 My Latest Blog Posts

Scripting SharePoint 2007 setup: choices and conceptsUse SHIFT+ENTER to open the menu (new window).
Adventures in Visual Studio 2010: Migrate the Content By Type web part to SharePoint 2010Use SHIFT+ENTER to open the menu (new window).
Register SharePoint themes by using a featureUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010 development on Windows 2008 Server R2 – Getting StartedUse SHIFT+ENTER to open the menu (new window).
New release SharePoint Objects: features and groupsUse SHIFT+ENTER to open the menu (new window).
Constructing the url to the SharePoint Edit Permissions pageUse SHIFT+ENTER to open the menu (new window).
Screencast: introduction to SharePoint ObjectsUse SHIFT+ENTER to open the menu (new window).
SharePoint 2007 and Reporting ServicesUse SHIFT+ENTER to open the menu (new window).
SharePoint Objects – Insight in usage of your SharePoint artifactsUse SHIFT+ENTER to open the menu (new window).
SharePoint 2007 Custom list schema and the Content Query Web PartUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010 Silverlight Client Object Model – ExecuteQuery vs ExecuteQueryAsyncUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010, the Client Object Models and Bing MapsUse SHIFT+ENTER to open the menu (new window).
Having fun with SharePoint 2010, Silverlight 3 and Bing MapsUse SHIFT+ENTER to open the menu (new window).
Connecting TFS 2010 projects to SharePoint sitesUse SHIFT+ENTER to open the menu (new window).
Adding a database to the SharePoint database Server using SPDatabaseUse SHIFT+ENTER to open the menu (new window).