Consuming an Azure Mobile App

Using the Azure Mobile Client SDK in a Xamarin.Forms application

PDF for offline use
Sample Code:
Related Articles:
Related APIs:
Related SDKs:

Let us know how you feel about this

Translation Quality


0/250

last updated: 2016-09

Azure Mobile Apps allow you to develop apps with scalable backends hosted in Azure App Service, with support for mobile authentication, offline sync, and push notifications. This article, which is only applicable to Azure Mobile Apps that use a Node.js backend, explains how to query, insert, update, and delete data stored in a table in an Azure Mobile Apps instance.

For information on how to create an Azure Mobile Apps instance that can be consumed by Xamarin.Forms, see Create a Xamarin.Forms app. After following these instructions, the downloadable sample application can be configured to consume the Azure Mobile Apps instance by setting the Constants.ApplicationURL to the URL of the Azure Mobile Apps instance. Then, when the sample application is run it will connect to the Azure Mobile Apps instance, as shown in the following screenshot:

Access to Azure Mobile Apps is through the Azure Mobile Client SDK, and all connections from the Xamarin.Forms sample application to Azure are made over HTTPS.

In iOS 9 and greater, App Transport Security (ATS) enforces secure connections between internet resources (such as the app's backend server) and the app, thereby preventing accidental disclosure of sensitive information. Since ATS is enabled by default in apps built for iOS 9, all connections will be subject to ATS security requirements. If connections do not meet these requirements, they will fail with an exception.

ATS can be opted out of if it is not possible to use the HTTPS protocol and secure communication for internet resources. This can be achieved by updating the app's Info.plist file. For more information see App Transport Security.

Consuming an Azure Mobile App Instance

The Azure Mobile Client SDK provides the MobileServiceClient class, which is used by a Xamarin.Forms application to access the Azure Mobile Apps instance, as shown in the following code example:

IMobileServiceTable<TodoItem> todoTable;
MobileServiceClient client;

public TodoItemManager ()
{
  client = new MobileServiceClient (Constants.ApplicationURL);
  todoTable = client.GetTable<TodoItem> ();
}

When the MobileServiceClient instance is created, an application URL must be specified in order to identify the Azure Mobile Apps instance. This value can be obtained from the dashboard for the mobile app in the Microsoft Azure Portal.

A reference to the TodoItem table stored in the Azure Mobile Apps instance must be obtained before operations can be performed on that table. This is achieved by calling the GetTable method on the MobileServiceClient instance, which returns a IMobileServiceTable<TodoItem> reference.

Querying Data

The contents of a table can be retrieved by calling the IMobileServiceTable.ToEnumerableAsync method that asynchronously evaluates the query and returns the results. Data can also be filtered server side by including a Where clause in the query. The Where clause applies a row filtering predicate to the query against the table, as shown in the following code example:

public async Task<ObservableCollection<TodoItem>> GetTodoItemsAsync (bool syncItems = false)
{
  ...
  IEnumerable<TodoItem> items = await todoTable
              .Where (todoItem => !todoItem.Done)
              .ToEnumerableAsync ();

  return new ObservableCollection<TodoItem> (items);
}

This query returns all the items from the TodoItem table whose Done property is equal to false. The query results are then placed in an ObservableCollection for display.

Inserting Data

When inserting data in the Azure Mobile Apps instance, new columns will automatically be generated in the table as required, provided that dynamic schema is enabled in the Azure Mobile Apps instance. The IMobileServiceTable.InsertAsync method is used to insert a new row of data into the specified table, as shown in the following code example:

public async Task SaveTaskAsync (TodoItem item)
{
  ...
  await todoTable.InsertAsync (item);
  ...
}

When making an insert request, an ID must not be specified in the data being passed to the Azure Mobile Apps instance. If the insert request contains an ID a MobileServiceInvalidOperationException will be thrown.

After the InsertAsync method completes, the ID of the data in the Azure Mobile Apps instance will be populated in the TodoItem instance in the Xamarin.Forms application.

Updating Data

When updating data in the Azure Mobile Apps instance, new columns will automatically be generated in the table as required, provided that dynamic schema is enabled in the Azure Mobile Apps instance. The IMobileServiceTable.UpdateAsync method is used to update existing data with new information, as shown in the following code example:

public async Task SaveTaskAsync (TodoItem item)
{
  ...
  await todoTable.UpdateAsync (item);
  ...
}

When making an update request, an ID must be specified so that the Azure Mobile Apps instance can identify the data to be updated. This ID value is stored in the TodoItem.ID property. If the update request doesn't contain an ID there is no way for the Azure Mobile Apps instance to determine the data to be updated, and so a MobileServiceInvalidOperationException will be thrown.

Deleting Data

The IMobileServiceTable.DeleteAsync method is used to delete data from an Azure Mobile Apps table, as shown in the following code example:

public async Task DeleteTaskAsync (TodoItem item)
{
  ...
  await todoTable.DeleteAsync(item);
  ...
}

When making a delete request, an ID must be specified so that the Azure Mobile App sinstance can identify the data to be deleted. This ID value is stored in the TodoItem.ID property. If the delete request doesn't contain an ID, there is no way for the Azure Mobile Apps instance to determine the data to be deleted, and so a MobileServiceInvalidOperationException will be thrown.

Summary

This article explained how to use the Azure Mobile Client SDK to query, insert, update, and delete data stored in a table in an Azure Mobile apps instance. The SDK provides the MobileServiceClient class that is used by a Xamarin.Forms application to access the Azure Mobile Apps instance.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.