Get started with data in Mobile Services
This topic shows you how to use Windows Azure Mobile Services to leverage data in an HTML app. In this tutorial, you will download an app that stores data in memory, create a new mobile service, integrate the mobile service with the app, and then login to the Windows Azure Management Portal to view changes to data made when running the app.
Note
This tutorial is intended to help you better understand how Mobile Services enables you to use Windows Azure to store and retrieve data from an HTML app. As such, this topic walks you through many of the steps that are completed for you in the Mobile Services quickstart. If this is your first experience with Mobile Services, consider first completing the tutorial Get started with Mobile Services.
This tutorial walks you through these basic steps:
- Download the HTML app project
- Create the mobile service
- Add a data table for storage
- Update the app to use Mobile Services
- Test the app against Mobile Services
Note To complete this tutorial, you need a Windows Azure account that has the Windows Azure Mobile Services feature enabled.
Additional requirements
You can host the GetStartedWithData app on any web server. However, for your convenience scripts have been provided that enable you to run the app on http://localhost:8000.
-
To use localhost, this tutorial requires that you have one of the following web servers running on your local computer:
- On Windows: IIS Express. IIS Express is installed by the [Microsoft Web Platform Installer].
- On MacOS X: Python, which should already be installed.
- On Linux: Python. You must install the [latest version of Python].
You can use any web server to host the app, but these are the web servers that are supported by the downloaded scripts.
-
A web browser that supports HTML5.
Download the GetStartedWithData project
This tutorial is built on the GetStartedWithData app, which is an HTML5 app. The UI for this app is identical to the app generated by the Mobile Services quickstart, except that added items are stored locally in memory.
-
Download the HTML app project files.
-
In an HTML editor, open the downloaded project and examine the app.js file.
Notice that added items are stored in an in-memory Array object (staticItems). Refresh the page, and the data disappears. It is not persisted.
-
Launch one of the following command files from the server subfolder.
- launch-windows (Windows computers)
- launch-mac.command (Mac OS X computers)
- launch-linux.sh (Linux computers)
Note
On a Windows computer, type `R` when PowerShell asks you to confirm that you want to run the script. Your web browser might warn you to not run the script because it was downloaded from the internet. When this happens, you must request that the browser proceed to load the script.
This starts a web server on your local computer to host the new app.
-
Open the URL http://localhost:8000/ in a web browser to start the app.
-
In the app, type meaningful text, such as Complete the tutorial, in Enter new task, and then click Add.

Notice that the saved text is added to the staticItems array, and the page is refreshed to display the new item.
Create a new mobile service in the Management Portal
Next, you will create a new mobile service to replace the in-memory list for data storage. Follow these steps to create a new mobile service.
- Log into the Windows Azure Management Portal.
-
At the bottom of the navigation pane, click +NEW.
![][1]
-
Expand Compute and Mobile Service, then click Create.
![][2]
This displays the New Mobile Service dialog.
-
In the Create a mobile service page, type a subdomain name for the new mobile service in the URL textbox and wait for name verification. Once name verification completes, click the right arrow button to go to the next page.
![][3]
This displays the Specify database settings page.
Note As part of this tutorial, you create a new SQL Database instance and server. You can reuse this new database and administer it as you would any other SQL Database instance. If you already have a database in the same region as the new mobile service, you can instead choose Use existing Database and then select that database. The use of a database in a different region is not recommended because of additional bandwidth costs and higher latencies.
-
In Name, type the name of the new database, then type Login name, which is the administrator login name for the new SQL Database server, type and confirm the password, and click the check button to complete the process.
![][4]
Note When the password that you supply does not meet the minimum requirements or when there is a mismatch, a warning is displayed.
We recommend that you make a note of the administrator login name and password that you specify; you will need this information to reuse the SQL Database instance or the server in the future.
You have now created a new mobile service that can be used by your mobile apps. Next, you will add a new table in which to store app data. This table will be used by the app in place of the in-memory collection.
[1]: ../../Shared/Media/plus-new.png [2]: ../Media/mobile-create.png [3]: ../Media/mobile-create-page1.png [4]: ../Media/mobile-create-page2.png
Add a new table to the mobile service
To be able to store app data in the new mobile service, you must first create a new table in the associated SQL Database instance.
-
In the Management Portal, click Mobile Services, and then click the mobile service that you just created.
-
Click the Data tab, then click +Create.

This displays the Create new table dialog.
-
In Table name type TodoItem, then click the check button.

This creates a new storage table TodoItem with the default permissions set, which means that any user of the app can access and change data in the table.
Note
The same table name is used in Mobile Services quickstart. However, each table is created in a schema that is specific to a given mobile service. This is to prevent data collisions when multiple mobile services use the same database.
-
Click the new TodoItem table and verify that there are no data rows.
-
Click the Columns tab and verify that there is only a single id column, which is automatically created for you.
This is the minimum requirement for a table in Mobile Services.
Note
When dynamic schema is enabled on your mobile service, new columns are created automatically when JSON objects are sent to the mobile service by an insert or update operation.
You are now ready to use the new mobile service as data storage for the app.
Update the app to use the mobile service for data access
Now that your mobile service is ready, you can update the app to store items in Mobile Services instead of the local collection.
-
In the Management Portal, click Mobile Services, and then click the mobile service you just created.
-
Click the Dashboard tab and make a note of the Site URL, then click Manage keys and make a note of the Application key.

You will need these values when accessing the mobile service from your app code.
-
In your web editor, open the index.html project file and add the following to the script references for the page:
<script src='//**APP_URL**/client/MobileServices.Web-1.0.0.min.js'></script>
You must replace the placeholder **APP_URL** with the URL of your mobile service.
-
In the editor, open the file app.js, uncomment the following code that defines the MobileServiceClient variable, and supply the URL and application key from the mobile service in the MobileServiceClient constructor, in that order:
var MobileServiceClient = WindowsAzure.MobileServiceClient,
client = new MobileServiceClient('AppUrl', 'AppKey'), This creates a new instance of MobileServiceClient that is used to access your mobile service.
-
Comment-out the following lines of code:
var itemCount = 0;
var staticItems = [];
Data will be stored in the mobile service and not in an in-memory array.
-
Uncomment the following line of code:
todoItemTable = MobileServiceClient.getTable('todoitem'); This code creates a proxy object (todoItemTable) for the SQL Database TodoItem.
-
Replace the $('#add-item').submit event handler with the following code:
$('#add-item').submit(function(evt) {
var textbox = $('#new-item-text'),
itemText = textbox.val();
if (itemText !== '') {
todoItemTable.insert({ text: itemText, complete: false })
.then(refreshTodoItems);
}
textbox.val('').focus();
evt.preventDefault();
}); This code inserts a new item into the table.
-
Replace the refreshTodoItems method with the following code:
function refreshTodoItems() {
var query = todoItemTable;
query.read().then(function(todoItems) {
listItems = $.map(todoItems, function(item) {
return $('<li>')
.attr('data-todoitem-id', item.id)
.append($('<button class="item-delete">Delete</button>'))
.append($('<input type="checkbox" class="item-complete">').prop('checked', item.complete))
.append($('<div>').append($('<input class="item-text">').val(item.text)));
});
$('#todo-items').empty().append(listItems).toggle(listItems.length > 0);
$('#summary').html('<strong>' + todoItems.length + '</strong> item(s)');
});
} This sends a query to the mobile service that returns all items. The results is iterated over and data is displayed on the page.
-
Replace the $(document.body).on('change', '.item-text') and $(document.body).on('change', '.item-complete') event handlers with the following code:
$(document.body).on('change', '.item-text', function() {
var newText = $(this).val();
todoItemTable.update({ id: getTodoItemId(this), text: newText });
});
$(document.body).on('change', '.item-complete', function() {
var isComplete = $(this).prop('checked');
todoItemTable.update({ id: getTodoItemId(this), complete: isComplete })
.then(refreshTodoItems);
}); This sends an item update to the mobile service when text is changed or when the box is checked.
-
Replace the $(document.body).on('click', '.item-delete') event handler with the following code:
$(document.body).on('click', '.item-delete', function () {
todoItemTable.del({ id: getTodoItemId(this) }).then(refreshTodoItems);
}); This sends a delete to the mobile service when the Delete button is clicked.
Now that the app has been updated to use Mobile Services for backend storage, it's time to test the app against Mobile Services.
Test the app against your new mobile service
-
Reload the URL http://localhost:8000/ in a web browser start the app.
Note
If you need to restart the web server, repeat the steps in the first section.
-
As before, type text in Enter new task, and then click Add.
This sends a new item as an insert to the mobile service.
-
In the Management Portal, click Mobile Services, and then click your mobile service.
-
Click the Data tab, then click Browse.

Notice that the TodoItem table now contains data, with id values generated by Mobile Services, and that columns have been automatically added to the table to match the TodoItem class in the app.
-
In the app, check one of the items in the list, then go back to the Browse tab in the portal and click Refresh.
Notice that the complete value has changed from false to true.
-
In the app.js project file, locate the RefreshTodoItems method and replace the line of code that defines query with the following:
var query = todoItemTable.where({ complete: false }); -
Load the page again, check another one of the items in the list.
Notice that the checked item now disappears from the list. Each update results in a round-trip to the mobile service, which now returns filtered data.
This concludes the Get started with data tutorial.
Next steps
This tutorial demonstrated the basics of enabling an HTML app to work with data in Mobile Services. Next, consider completing one of the following tutorials that is based on the GetStartedWithData app that you created in this tutorial:
Once you have completed the data series, learn how to authenticate users of your app try one of these other tutorials by completing Get started with authentication.