Home / AJAX

OData: HOW TO Execute a Sequence of Actions Through an OData Service Using Ajax

Modified on 2010/03/08 23:10 by Shayne Burgess Categorized as Uncategorized
You can access an OData Service from an ASP.NET AJAX application by using the Sys.Data.OpenDataServiceProxy class. From client script, you can query and modify data that is exposed by a data service that is hosted in the same Web site as the current Web page.

In some cases, you must to execute more than one data operation as a single batch. If so, you can use the Sys.Data.OpenDataActionSequence script class to create a batch of data operations that are executed in the order that they were added, instead of executing each operation as a separate command.

The example in this procedure shows an ASP.NET AJAX-enabled Web page that lets the user insert ten records in the Customers table of the Northwind database.

To run the example code in this topic, you will need the following:
  • The current release of ASP.NET AJAX, which you can download from the CodePlex site.
  • The SQL Server Northwind sample database installed on your computer.
  • An OData feed that exposes the Northwind database. The easiest way to create this is to use WCF Data Services to create a service over the Northwind database. The service must be configured to allow reading from the Customers feed. For more information, see Data Service Quickstart.
  • An ASP.NET Web page where you can add code to invoke a Web service.

Executing a Sequence of Actions

You can create a page that uses client script to execute multiple data operations through an OData service.

To execute a sequence of actions

  • Create a script element or a JScript file that includes a client-script function that does the following:
    1. Creates an instance of the OpenDataServiceProxy class.
    2. Calls the OpenDataActionSequence method to create an instance of the OpenDataActionSequence JavaScript object.
    3. Calls the Sys.Data.OpenDataActionSequence.addInsertAction Method method, Sys.Data.OpenDataActionSequence.addUpdateAction Method method, or Sys.Data.OpenDataActionSequence.addRemoveAction Method method as many times as needed in order to create the sequence of data operations that you want to execute. In each call, pass the following parameters: the object with the data, and the relative URI of the resource that will receive the data. You can also pass a parameter with context or state, which is passed to the callback function when the request returns.
    4. Calls the execute method to execute all the data operations that have been added to the sequence. You can also pass a callback function that is called when the data operations have been executed.

The following example shows a function named pageLoad that creates an instance of the OpenDataServiceProxy class. The instance references a fictitious OData service that has the relative URI /Northwind.svc. The function named ExecuteOperations calls the createActionSequence method to create an instance of the OpenDataActionSequence script class. The function includes code that adds ten insert operations to the sequence. Finally, it calls the execute method and passes cbComplete as the callback function.
var northwindService;
function pageLoad() {
    northwindService = new 
function ExecuteOperations() {
    var actSequence = northwindService.createActionSequence();
    // Create 10 customer objects and insert them.
    for (var i = 0; i < 10; i++) {
        var customer = {
            CustomerID: "TEST" + i,
            CompanyName: "Sample Company " + i
        actSequence.addInsertAction(customer, "/Customers");
        "insert 10 Customers" /* user-provided context */);