asp.net-mvcodatadata-paging

Can I consume a server-paged OData result in ASP.NET MVC?


I'd like to consume a server-paged OData result.

That means, the JSON code includes an element

"__next": "http://server/odata.svc/table?$skiptoken=guid'4dda1cd9-7eff-423f-a314-08edf26a22e8'"

How do I do this in ASP.NET MVC? I see quite a few examples for consuming OData, but none handles paged data.

I'm quite new to both OData and MVC, so please be as obvious as possible in the answers. ;-)


Solution

  • The main way of consuming OData in .NET is with the WCF Data Services client library. In Visual Studio, you can right click on a project, and select "Add Service Reference" to get started. That "Add Service Reference" uses the WCF Data Services client library under the hood. For general information on how to use the client library, check out the MSDN documentation here: http://msdn.microsoft.com/en-us/library/cc668772.aspx

    A caveat, though: the JSON you posted (with "__next") is the old OData JSON format, sometimes referred to as "Verbose JSON". The WCF Data Services client does not have support for this format. It only supports Atom and the new JSON format. As long as your server is able to support either Atom or the new v3 JSON, that shouldn't be a problem for you.

    As for your actual question, you can use the .GetContinuation() method on a QueryOperationResponse object. For example:

    // "DemoService" is the auto-generated subclass of DataServiceContext created when you run "Add Service Reference"
    DemoService context = new DemoService(new Uri(<url to the service root>));
    var firstPage = context.Customers.Execute() as QueryOperationResponse<Customer>;
    var token = firstPage.GetContinuation();
    if (token != null)
        var secondPage = context.Execute<Customer>(token);
    

    Obviously, you'd probably want to turn this into a loop, but that should give you a basic idea of the API usage. For more information, see this page on MSDN: http://msdn.microsoft.com/en-us/library/ee358711.aspx