In my last post, Going deep with Mobile Services data, we looked at how we could use server scripts to augment the results of a query, even returning a hierarchy of objects. This time, let us explore some trickery to do the same, but in reverse.

Imagine we want to post up a series of objects, maybe comments, all at once and process them in a single script… here’s a (somewhat manufactured scenario) demonstrating how you can do this. But first, we’ll need to understand a little more about the way data is handled in Mobile Services.

If you think of the Mobile Service data API as a pipeline, there are two key stages: scripting and storage. You write scripts that can intercept writes and reads to storage. There are two additional layers to consider:

Pre-scripting This is where Mobile Services performs the authentication checks and validates that the payload makes sense – we’ll talk about this in more detail below.

Pre-StorageAt this point, we have to make sure that anything you’re about to do makes sense – this validation layer is much stricter and won’t allow complex objects through. This stage is also where we handle and log any nasty errors (to help you diagnose issues) and perform dynamic schematization.

The pre-scripting layer (1) expects a single JSON object (no arrays) and if the operation is an update (PATCH) and has an id specified in the JSON body – that id must match the id specified on the url, e.g. http://yourapp.azure-mobile.net/tables/foo/63

But with that knowledge in mind, we can still use the scripting layer to perform some trickery, if you so desire, and go to work on that JSON payload however it sees fit. Take the following example server script; it expects a JSON body like this:

Sending the correct payload from Fiddler is easy, just POST the above JSON body to http://yourapp.azure-mobile.net/tables/ratings and watch the script unfold the results for you. You should get a response as follows:

HTTP/1.1 201 Created
Content-Type: application/json

Content-Length: 19

{"ratingIds":[7,8]}

Which is pretty cool – Mobile Services really does present a great way of building data focused JSON APIs. But what about uploading data like this using the MobileServiceClient in both C# and JS?

C# Client

The C# client is a little trickier. You’re probably using types and have a Rating class that has a MovieId and Rating property. If you have an IMobileServiceTable<Rating> then you can’t ‘insert’ a List<Rating> it simply won’t compile. In this case, you’ll want to drop to the JSON version of the client – it’s still really easy to use.

If you were doing this a lot, you’d probably want to create a few helper methods to help you with the creation of the JSON, but otherwise, it’s pretty straightforward. Naturally, JavaScript has a slightly easier time with JSON: