Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

How to: Construct and Process Batch Queries by Using REST


This topic shows you how to submit batched queries to the Microsoft® SharePoint® Foundation representational state transfer (REST) interface from a Microsoft Silverlight® application, and how to process the response from the service.

This how-to topic assumes that you have an existing Microsoft Visual Studio® development system solution that contains a Silverlight project. It also assumes that you have added a reference to the SharePoint Foundation REST interface, as described in How to: Create a REST Service Proxy.

This topic uses the Team Discussion list as an example. The Team Discussion list is created by default on SharePoint 2010 team sites. If you created your SharePoint site from a different site definition, this list may not be available.

Summary of Steps

This topic describes the following steps:

  • Step 1: Create Multiple REST Queries. In this step, you use your service reference to construct multiple REST queries that will be sent to the server in a single batch.
  • Step 2: Create the Delegate Method and Process the Results. In this step you create the delegate method that will process the batch results.

Step 1: Create Multiple REST Queries

This procedure creates several queries against the Team Discussion list, and submits them to the REST interface as a batched request.

Typically, you add the code examples listed below to the code-behind file for your Silverlight control. In this example, we use the click event handler of a button to create and submit the query.

To create and submit a batched request

  1. Add a using statement that points to the REST service reference. Use the following format.

    In the following example, Client.SilverlightSample is the Silverlight project namespace and Sample.ServiceReference is the service namespace.

    using Client.SilverlightSample.Sample.ServiceReference;
  2. Use the default constructor to instantiate the data context entity for the service, as shown in the following example. The name of your data context class will take the form <sitename>DataContext, where <sitename> is the name of your SharePoint site.
    private void button1_Click(object sender, RoutedEventArgs e)
      DataDataContext context = new DataDataContext();
  3. Create your query objects. The following example creates three queries against the Team Discussion list.
    // Get anything with the word 'Business' in the Body
    var query1 = context.TeamDiscussion
      .Where(p => p.Body.Contains("Business"));
    // Get anything that was created within the previous 14 days
    var query2 = context.TeamDiscussion
      .Where(p => p.Created.Value > DateTime.Now.AddDays(-14));
    // Get anything with an attachment
    var query3 = context.TeamDiscussion
      .Where(p => p.Attachments != null && p.Attachments.Count > 0);            
  4. Call the BeginExecuteBatch method on the data context to submit your queries as a batched request. Supply the following objects as arguments to the BeginExecuteBatch method.
    1. A callback delegate that points to the method you want to invoke when the query returns. In this case, we will create a callback method named DisplayDiscussions in step 2.
    2. A state object, which in this case is the data context instance.
    3. Each of your query objects. The method accepts a parameter array of query objects; therefore, you can submit as many queries as required.
    context.BeginExecuteBatch(DisplayDiscussions, context, query1, query2, 

Step 2: Create the Delegate Method and Processing the Results

This procedure creates a callback method to process the results of the batch query. In this example, we create a method named DisplayDiscussions to match the delegate used in the last task of step 1.

To process the batched results

  1. Create a new private method named DisplayDiscussions that accepts a single parameter of type IAsyncResult.
    private void DisplayDiscussions (IAsyncResult result)
  2. In the DisplayDiscussions method, add a call to the Dispatcher.BeginInvoke() method. This is used to process the results on the UI thread.
    private void DisplayDiscussions (IAsyncResult result)
      Dispatcher.BeginInvoke(() =>
        //Process results here
    The results are processed on the UI thread because, typically, Silverlight applications maintain results in ObservableCollection instances, which are tightly data-bound to the UI. Updating the observable collection will also update the UI. All UI updates must be performed on the UI thread.

  3. Add code to retrieve the response from the data service and process each response operation. For each response operation, check the following before you process the results.
    1. Check that the operation is error free.
    2. Check that the operation is of the expected type.
    private void DisplayParts(IAsyncResult result)
      Dispatcher.BeginInvoke(() =>
        //Get the Batch Response
        DataServiceResponse Response = context.EndExecuteBatch(result);
        //Loop through each operation
        foreach (QueryOperationResponse Operation in Response)
          if (Operation.Error != null)
            throw Operation.Error;
          //Check the Operation is of the expected type
          if (Operation is QueryOperationResponse<TeamDiscussionItem>)
            //TODO: Process your results

    Note: This is a simple example in which each query returns a result of the same type (TeamDiscussionItem in this case). If you are processing results of varying types, you must check the type of each OperationResponse before processing it. For more information, see Using the REST Interface in the Client Reference Implementation.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.