WCF DataServices and Total Number of Rows

Cristian Merighi () 0.00

OData protocol and ASP.NET Ajax 4 tools: how to use them in order to retrieve, along with a subset of rows, the total row number of elements stored in database.
This article is obsolete. Some functionalities might not work anymore. Comments are disabled.

ASP.NET Ajax 4 just came out of the box in its final vest, and white papers don't offer - so far - omnicomprehensive support for all the available features.
That's why I decided to pin newly discovered nuggets, as soon as I get 'em, here on my online diary.

This article is about data-paging (generally speaking). In particular it's about retrieving the total number of rows (stored in database and given a particular filter clause) via WCF Data Services using OData protocol URI conventions.

We're going to use retrieve data from a client browser through javascript scripts.
Let's start by loading necessary scripts (à la ASP.Net 4, I assume that the following code would result clear, otherwise please refer to this documentation or similar).

<script type="text/javascript">
// jQuery has been already (statically) loaded
    Sys.require([Sys.components.openDataServiceProxy, Sys.components.dataView]);

Now the call to a System.Data.Services.DataService that exposes data in a "RESTful" fashion.

<script type="text/javascript">

var totalCount;
Sys.onReady(function () {
    // setting default values for our hypothetical Sys.UI.DataView.
    $().dataView.defaults = {autoFetch: true, itemTemplate: '#dataViewItemTemplate'};
    // instantiate the jQuery way the web service proxy.
    var proxy = $.openDataServiceProxy('<%=ResolveClientUrl("~/services/modelservice.svc") %>');
    var topRows = 3;
    // I use this utility (Sys.Data.OopenDataQueryBuilder) to ease the URI formatting.
    var qb = new Sys.Data.OpenDataQueryBuilder('BudgetSet');
    // IMPORTANT: set the $inlinecount parameter to allpages:
    // the result of the request will be enriched with a total count information.
    qb.set_orderby('EndDate desc,StartDate');
    // Limit the number of rows to be returned.
    // Filter clause:
    qb.set_filter("EndDate le datetime'<%=DateTime.Today.ToString("yyyy-MM-dd") %>'");
    proxy.query(/* here it is the formatted URI -> */qb.toString(),
        // And now the black magic!
        // Among the arguments passed to the callback function for successful retrieval...
        // the 4th(!) contains the information that we need: the totalcount.
        function(results, ctx, uri, c){
        // let us store it...
        totalCount = c.count;
        // let us use the retrieved data to feed the dataview...
        $("#expiredBudgets").dataView({ data: results });

The interface implementation is omitted, how you will exploit the total row count information is less important than how to get it!
Hope it helped.

Take care. Bye.




Cristian Merighi facebook twitter google+ youtube

Latest articles

Top rated


Where am I?


Cristian Merighi facebook twitter google+ youtube

I'm now reading