Working with WCF RIA Services JSON endpoints: some helpful hints

It is well-known that WCF RIA Services can be used for dealing with data not only from Silverlight client. It is possible to call the same DomainService's methods from other non-Silverlight code, for example from jQuery. There is a couple of very helpful tutorials on how to deal with JSON endpoint (for example, articles by Joseph Connolly and by David Rousset), we just wanted to elaborate on some topics that were left beyond the scope of this articles.

Filtering Get-requests using JSON endpoint

Support of IQueryable in GET-methods is one of the real powers of WCF RIA Services. You can send your own filters (as lambda-expressions) from client application, and as a result you'll be getting - and loading from database - only the objects you actually want to see, not all the objects in existence. And client application can decide for itself which objects are actually needed.

Now, is the same valid when calling the Get-method from JSON endpoint? Is it possible to send filters to server directly from your jQuery code?

Yes, it turns out this is possible as well. Let's suppose you have a server-side method GetPersons, taking one parameter "shopId", and want to filter it by some condition.

Sample object for WCF RIA Services usage that takes filter as parameter:

    function RiaClient(){}
        communicationContext: {},
        constants: {
            host: 'http://localhost/',
            serviceUrl: 'Services/Sample-Web-SampleService.svc/JSON/'
        Load: function () {
            var query = arguments[0];
            var params = {};
            params.success = arguments[1];
            if ((arguments.length > 2) && {
                var where = escape(arguments[2]);
      $where = where;
            params.error = function (data) {
            params.type = "GET";
            params.datatype = "json";
            params.url = + this.constants.serviceUrl + query.request;
        GetPersonsQuery: function (shopId) {
            var query = {};
            query.request = "GetPersons";
   = { shopId: shopId };
            return query;

sample of its usage:

var where = 'it.LastName.toLower()=="' + this.Persons.LastName.toLowerCase() + '"';
            where += '&&it.FirstName.toLower()=="' + this.Persons.FirstName.toLowerCase() + '"';
            where += '&&it.EMail.toLower()=="' + this.Persons.EMail.toLowerCase() + '"';
            where += '&&it.Phone.toLower()=="' + this.Persons.Phone.toLowerCase() + '"';
_RiaClient = new RiaClient();
_RiaClient.Load(_RiaClient.GetPersonsQuery(this.shopId), function (data) { }, where);

Be careful with the order of fields when submitting object

If saving data to WCF RIA Services won't work, you should first check the order of declaring fields in JSON object. __type should follow first, for example:

    "Entity": {
        "__type": "Persons:#myApp.Web",
        "ID": 48,
        "Email": "",
        "Phone": "21242345235"

Other order of __type field, for example:

    "Entity": {
        "ID": 48,
        "Email": "",
        "__type": "Persons:#myApp.Web",
        "Phone": "21242345235"

will result in error.


how to CUD multi entities with json endpoint ?

good article, helpfull, thank you !

in the realworld, we need to insert (update, delete) multi entities to the server. how can we do that? a guid please, thank you agian!

Insert multiple entities

Thank you for interest in my article. To insert multiple entities, you should do something like

Insert: function (tenantId, save, callback) {
            var changeSet = [];
            var i = 0;
            for (var key in save) {
                var entity = save[key];
                var changeSetEntry = {};
                changeSetEntry.Id = i++;
                changeSetEntry.Entity = entity;
                changeSetEntry.Operation = 2;

see also WCF RIA Services, jQuery, and JSON endpoint - Part 2

What about date comparisons?

I'm having trouble doing this with dates...

ErrorMessage=Operator '>' incompatible with operand types 'DateTime' and 'String' (at index 44)

I tried just giving it a raw javascript date, and that didn't work either. Tried the LINQ syntax " new DateTime(x,y,z) " and it choked on the new keyword. Ideas?

Re: what about date comparisions

JSON dates should be properly formatted, see for example
(section DateTime Wire Format of this document:
"DateTime values appear as JSON strings in the form of "/Date(700000+0500)/"....)