|
| 1 | +--- |
| 2 | +title: "Consuming an Azure Mobile App" |
| 3 | +description: "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." |
| 4 | +ms.prod: xamarin |
| 5 | +ms.assetid: 2B3EFD0A-2922-437D-B151-4B4DE46E2095 |
| 6 | +ms.technology: xamarin-forms |
| 7 | +author: davidbritch |
| 8 | +ms.author: dabritch |
| 9 | +ms.date: 09/20/2016 |
| 10 | +--- |
| 11 | + |
| 12 | +# Consuming an Azure Mobile App |
| 13 | + |
| 14 | +[ Download the sample](https://developer.xamarin.com/samples/xamarin-forms/WebServices/TodoAzure/) |
| 15 | + |
| 16 | +_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._ |
| 17 | + |
| 18 | +> [!NOTE] |
| 19 | +> Starting on June 30, all new Azure Mobile Apps will be created with TLS 1.2 by default. In addition, it's also recommended that existing Azure Mobile Apps be reconfigured to use TLS 1.2. For information on how to enforce TLS 1.2 in an Azure Mobile App, see [Enforce TLS versions](/azure/app-service/app-service-web-tutorial-custom-ssl#enforce-tls-versions). For information on how to configure Xamarin projects to use TLS 1.2, see [Transport Layer Security (TLS) 1.2](~/cross-platform/app-fundamentals/transport-layer-security.md). |
| 20 | +
|
| 21 | +For information on how to create an Azure Mobile Apps instance that can be consumed by Xamarin.Forms, see [Create a Xamarin.Forms app](https://azure.microsoft.com/documentation/articles/app-service-mobile-xamarin-forms-get-started/). 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: |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +Access to Azure Mobile Apps is through the [Azure Mobile Client SDK](https://www.nuget.org/packages/Microsoft.Azure.Mobile.Client/), and all connections from the Xamarin.Forms sample application to Azure are made over HTTPS. |
| 26 | + |
| 27 | +> [!NOTE] |
| 28 | +> In iOS 9 and greater, App Transport Security (ATS) enforces secure connections between internet resources (such as the app's back-end 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. |
| 29 | +> 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](~/ios/app-fundamentals/ats.md). |
| 30 | +
|
| 31 | +## Consuming an Azure Mobile App Instance |
| 32 | + |
| 33 | +The [Azure Mobile Client SDK](https://www.nuget.org/packages/Microsoft.Azure.Mobile.Client/) 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: |
| 34 | + |
| 35 | +```csharp |
| 36 | +IMobileServiceTable<TodoItem> todoTable; |
| 37 | +MobileServiceClient client; |
| 38 | + |
| 39 | +public TodoItemManager () |
| 40 | +{ |
| 41 | + client = new MobileServiceClient (Constants.ApplicationURL); |
| 42 | + todoTable = client.GetTable<TodoItem> (); |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +When the `MobileServiceClient` instance is created, an application URL must be specified to identify the Azure Mobile Apps instance. This value can be obtained from the dashboard for the mobile app in the [Microsoft Azure Portal](https://portal.azure.com/). |
| 47 | + |
| 48 | +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. |
| 49 | + |
| 50 | +### Querying Data |
| 51 | + |
| 52 | +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: |
| 53 | + |
| 54 | +```csharp |
| 55 | +public async Task<ObservableCollection<TodoItem>> GetTodoItemsAsync (bool syncItems = false) |
| 56 | +{ |
| 57 | + ... |
| 58 | + IEnumerable<TodoItem> items = await todoTable |
| 59 | + .Where (todoItem => !todoItem.Done) |
| 60 | + .ToEnumerableAsync (); |
| 61 | + |
| 62 | + return new ObservableCollection<TodoItem> (items); |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +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. |
| 67 | + |
| 68 | +### Inserting Data |
| 69 | + |
| 70 | +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: |
| 71 | + |
| 72 | +```csharp |
| 73 | +public async Task SaveTaskAsync (TodoItem item) |
| 74 | +{ |
| 75 | + ... |
| 76 | + await todoTable.InsertAsync (item); |
| 77 | + ... |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +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. |
| 82 | + |
| 83 | +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. |
| 84 | + |
| 85 | +### Updating Data |
| 86 | + |
| 87 | +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: |
| 88 | + |
| 89 | +```csharp |
| 90 | +public async Task SaveTaskAsync (TodoItem item) |
| 91 | +{ |
| 92 | + ... |
| 93 | + await todoTable.UpdateAsync (item); |
| 94 | + ... |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +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. |
| 99 | + |
| 100 | +### Deleting Data |
| 101 | + |
| 102 | +The `IMobileServiceTable.DeleteAsync` method is used to delete data from an Azure Mobile Apps table, as shown in the following code example: |
| 103 | + |
| 104 | +```csharp |
| 105 | +public async Task DeleteTaskAsync (TodoItem item) |
| 106 | +{ |
| 107 | + ... |
| 108 | + await todoTable.DeleteAsync(item); |
| 109 | + ... |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +When making a delete request, an ID must be specified so that the Azure Mob`ile 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. |
| 114 | + |
| 115 | +## Summary |
| 116 | + |
| 117 | +This article explained how to use the [Azure Mobile Client SDK](https://www.nuget.org/packages/Microsoft.Azure.Mobile.Client/) 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. |
| 118 | + |
| 119 | + |
| 120 | +## Related Links |
| 121 | + |
| 122 | +- [TodoAzure (sample)](https://developer.xamarin.com/samples/xamarin-forms/WebServices/TodoAzure/) |
| 123 | +- [Create a Xamarin.Forms app](https://azure.microsoft.com/documentation/articles/app-service-mobile-xamarin-forms-get-started/) |
| 124 | +- [Azure Mobile Client SDK](https://www.nuget.org/packages/Microsoft.Azure.Mobile.Client/) |
| 125 | +- [MobileServiceClient](https://msdn.microsoft.com/library/azure/microsoft.windowsazure.mobileservices.mobileserviceclient(v=azure.10).aspx) |
0 commit comments