Search code examples
jsonrestwcf-ria-servicesdomainservices

How to insert entity over WCF-RIA-Services JSON endpoint


My WCF-RIA DomainService has an insert method that looks like this:

public void InsertWidget(WidgetDef widgetDef)

class WidgetDef
{
    [Key]
    int widgetID;
    string title;
    int x;
    int x;
    // there are more properties, but I think you get the idea...
}

To access this through the JSON endpoint, I think I need to POST a changeset to the url:

[serverURL][namespace]WidgetService.svc/json/SubmitChanges.

I'm pretty sure I got the URL correct, because my request reaches the WidgetService.Initialize method, but then I get an exception in the server - which is no surprise because I don't know what the content of the request should look like.

My Question: What is the (JSON) format of a HTTP request's content for an insert operation?


Solution

  • Insert changeset for the example given would look like this:

    {"changeSet":[ 
            {"Id":0, 
             "Entity":{"__type":"WidgetDef:#widgetDefNamespace",
                        "widgetId":0, 
                        "title":"the new title", 
                        "x":10, 
                        "y":10, 
                    }, 
                "Operation":2    // '2' for insert, '3' for update, '4' for delete 
            } 
        ] 
    } 
    

    Thanks to following blog post: http://www.joseph-connolly.com/blog/post/WCF-RIA-Services-jQuery-and-JSON-endpoint-Part-2.aspx