Validate and modify data in Mobile Services by using server scripts
This topic shows you how to leverage server scripts in Windows Azure Mobile Services. Server scripts are registered in a mobile service and can be used to perform a wide range of operations on data being inserted and updated, including validation and data modification. In this tutorial, you will define and register server scripts that validate and modify data. Because the behavior of server side scripts often affects the client, you will also update your Android app to take advantage of these new behaviors.
This tutorial walks you through these basic steps:
- Add string length validation
- Update the client to support validation
- Add a timestamp on insert
- Update the client to display the timestamp
This tutorial builds on the steps and the sample app from the previous tutorial Get started with data. Before you begin this tutorial, you must first complete Get started with data.
Add validation
It is always a good practice to validate the length of data that is submitted by users. First, you register a script that validates the length of string data sent to the mobile service and rejects strings that are too long, in this case longer than 10 characters.
-
Log into the Windows Azure Management Portal, click Mobile Services, and then click your app.

-
Click the Data tab, then click the TodoItem table.

-
Click Script, then select the Insert operation.

-
Replace the existing script with the following function, and then click Save.
function insert(item, user, request) {
if (item.text.length > 10) {
request.respond(statusCodes.BAD_REQUEST, 'Text length must be 10 characters or less.');
} else {
request.execute();
}
} This script checks the length of the text property and sends an error response when the length exceeds 10 characters. Otherwise, the execute method is called to complete the insert.
Note
You can remove a registered script on the Script tab by clicking Clear and then Save.
Update the client
Now that the mobile service is validating data and sending error responses, you need to verify that your app is correctly handling error responses from validation.
-
In Eclipse, open the project that you created when you completed the tutorial Get started with data.
-
In the ToDoActivity.java file, locate the addItem method and replace the call to the createAndShowDialog method with the following code:
createAndShowDialog(exception.getCause().getMessage(), "Error");
This displays the error message returned by the mobile service.
-
From the Run menu, then click Run to start the app, then type text longer than 10 characters in the textbox and click the Add button.
Notice that error is handled and the error messaged is displayed to the user.
Add a timestamp
The previous tasks validated an insert and either accepted or rejected it. Now, you will update inserted data by using a server script that adds a timestamp property to the object before it gets inserted.
-
In the Scripts tab in the Management Portal, replace the current Insert script with the following function, and then click Save.
function insert(item, user, request) {
if (item.text.length > 10) {
request.respond(statusCodes.BAD_REQUEST, 'Text length must be under 10');
} else {
item.createdAt = new Date();
request.execute();
}
} This function augments the previous insert script by adding a new createdAt timestamp property to the object before it gets inserted by the call to request.execute.
Note
Dynamic schema must be enabled the first time that this insert script runs. With dynamic schema enabled, Mobile Services automatically adds the createdAt column to the TodoItem table on the first execution. Dynamic schema is enabled by default for a new mobile service, and it should be disabled before the app is published.
-
From the Run menu, then click Run to start the app, then type text (shorter than 10 characters) in the textbox and click Add.
Notice that the new timestamp does not appear in the app UI.
-
Back in the Management Portal, click the Browse tab in the todoitem table.
Notice that there is now a createdAt column, and the new inserted item has a timestamp value.
Next, you need to update the Android app to display this new column.
Update the client again
The Mobile Service client will ignore any data in a response that it cannot serialize into properties on the defined type. The final step is to update the client to display this new data.
-
In Package Explorer, open the file ToDoItem.java, then add the following import statement:
import java.util.Date;
-
Add the following code to the private field definitions in the TodoItem class:
/**
* Timestamp of the item inserted by the service.
*/
@com.google.gson.annotations.SerializedName("createdAt")
private Date mCreatedAt; Note
The SerializedName annotation tells the client to map the new mCreatedAt property in the app to the createdAt column defined in the TodoItem table, which has a different name. By using this annotation, your app can have property names on objects that differ from column names in the SQL Database. Without this annotation, an error occurs because of the casing differences.
-
Add the following methods to the ToDoItem class to get and set the new mCreatedAt property:
/**
* Sets the timestamp.
*
* @param date
* timestamp to set
*/
public final void setCreatedAt(Date date) {
mCreatedAt = date;
}
/**
* Returns the timestamp.
*/
public Date getCreatedAt() {
return mCreatedAt;
} -
In Package Explorer, open the file ToDoItemAdapter.java, and add the following import statement:
import java.text.DateFormat;
-
In the GetView method, add the following code:
String createdAtText = "";
if (currentItem.getCreatedAt() != null){
DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT);
createdAtText = formatter.format(currentItem.getCreatedAt());
} This generates a formatted date string when a timestamp value exists.
-
Locate the code checkBox.setText(currentItem.getText()); and replace this line of code with the following:
checkBox.setText(currentItem.getText() + " " + createdAtText);
This appends the timestamp date to the item for display.
-
From the Run menu, then click Run to start the app.
Notice that the timestamp is only displayed for items inserted after you updated the insert script.
-
Replace the existing RefreshItemsFromTable method with the following code:
private void refreshItemsFromTable() {
mToDoTable.where().field("complete").eq(false).and().field("createdAt").ne((String)null)
.execute(new TableQueryCallback<ToDoItem>() {
public void onCompleted(List<ToDoItem> result, int count,
Exception exception, ServiceFilterResponse response) {
if (exception == null) {
mAdapter.clear();
for (ToDoItem item : result) {
mAdapter.add(item);
}
} else {
createAndShowDialog(exception, "Error");
}
}
});
} This method updates the query to also filter out items that do not have a timestamp value.
-
From the Run menu, then click Run to start the app.
Notice that all items created without timestamp value disappear from the UI.
You have completed this working with data tutorial.
Next steps
Now that you have completed this tutorial, consider continuing on with the final tutorial in the data series: Refine queries with paging.
Server scripts are also used when authorizing users and for sending push notifications. For more information see the following tutorials: