Home / AJAX

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

RSS
Modified on 2010/03/02 01:01 by Shayne Burgess Categorized as Uncategorized
You can access an OData Service from an ASP.NET AJAX application by using the Sys.Data.AdoNetServiceProxy 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.AdoNetActionSequence 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 AdoNetServiceProxy class.
    2. Calls the AdoNetActionSequence method to create an instance of the AdoNetActionSequence JavaScript object.
    3. Calls the Sys.Data.AdoNetActionSequence.addInsertAction Method method, Sys.Data.AdoNetActionSequence.addUpdateAction Method method, or Sys.Data.AdoNetActionSequence.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 AdoNetServiceProxy 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 AdoNetActionSequence 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 
        Sys.Data.AdoNetServiceProxy("/Northwind.svc");
}
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");
    }
    actSequence.execute(cbComplete, 
        "insert 10 Customers" /* user-provided context */);
}