# Azure Functions

This article provides a step-by-step tutorial on how to configure [Azure Functions](https://docs.microsoft.com/en-us/azure/azure-functions/) to serve data for a [Kendo UI Grid](https://www.telerik.com/kendo-jquery-ui/documentation/controls/grid/overview).

The complete implementation of the sample project is available in the [Kendo UI Cloud Integration](https://github.com/telerik/kendo-cloud-integration/tree/master/AzureFunctions) repository on GitHub.

## Prerequisites

Basic knowledge on the organization of [Azure Portal](https://docs.microsoft.com/en-us/azure/azure-portal/).

## Creating Azure Functions Applications

1. Follow the steps from the [Create your first function in the Azure portal](https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-azure-function) > [Create a function app](https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-azure-function#create-a-function-app) quickstart.
2. Provide the `kendo-grid-functions` name to the application and set the name of the storage account to `kendogridfunctions`.
3. In [Azure Portal](https://portal.azure.com/), go to the newly created **kendo-grid-functions** application.

## Creating HTTP-Triggered Functions for CRUD Operations

You have to apply the following steps individually to set each function (read, create, destroy, and update).

1. On the left-side panel and under the application name, click the **+** (plus) symbol which, when the **Functions** section is hovered, appears to the right.

   **Figure 1: Overview of the application functions**

   ![Kendo UI for jQuery Functions application overview](https://www.telerik.com/kendo-jquery-ui/documentation/assets/7599b88c59ee8bc81f38a70adfb4e406/create-new-function.png)

2. If the **Get started quickly with a premade function** screen appears, click the **Custom function** link at the bottom.

   **Figure 2: The Get started quickly with a premade function window**

   ![Kendo UI for jQuery Get started quickly with a premade function](https://www.telerik.com/kendo-jquery-ui/documentation/assets/ecc77ba0e4ca56f1ab19c0175150b9e9/new-function-quick-start.png)

3. Click the **HTTP trigger** option. On the panel that appears to the right, select the language and fill in a meaningful name for each function. Later on, the tutorial will demonstrate how to implement the Azure Functions in C#—therefore, select that language—and will use `Read`, `Create`, `Update`, and `Destroy` as names for the four functions.

   **Figure 3: Configuring a new HTTP trigger function**

   ![Kendo UI for jQuery New HTTP trigger function configuration](https://www.telerik.com/kendo-jquery-ui/documentation/assets/160645ca526cb0d53502caaeb1485b0e/new-function-configuration.png)

## Integrating Input for the Read Function

1. Expand the `Read` function and, under the function name on the left navigation panel, click the **Integrate** section.

2. In the **Inputs** section, click the **New Input** button.

3. Select **Azure Table Storage** as the input storage that will be integrated and click **Select**.

   **Figure 4: Integrating the new input for the function**

   ![Kendo UI for jQuery Integrate new input for the function](https://www.telerik.com/kendo-jquery-ui/documentation/assets/f376db19a3672919cf95be97c096b929/new-input-click.png)

4. Type **Product** for the partition key of the table.

5. Chose the maximum number of records to read. In this case, the default value of 50 will be preserved.

6. In **Storage account connection** to the right of the field, click the **new** link.

7. Select the **kendogridfunctions** connection that was created during the initial setup of the application.

8. Change **Table name** to **Products**.

9. Click **Save** to save the newly integrated input table.

   **Figure 5: Configuring the new input**

   ![Kendo UI for jQuery New input configuration](https://www.telerik.com/kendo-jquery-ui/documentation/assets/c323d17f40601abad9dde8dbed6eeeeb/new-input-configuration.png)

## Integrating Output for the Create, Destroy, and Update Functions

Configure an output integration for each of the other three functions (create, destroy, and update):

1. Click **New Output**.

2. Select **Azure Table Storage** and click **Select**.

   **Figure 6: Integrating the new output for the function**

   ![Kendo UI for jQuery Integrate new output for the function](https://www.telerik.com/kendo-jquery-ui/documentation/assets/bd8c3be7c05a70fce49f7a11a1f26983/new-output-click.png)

3. Select **kendogridfunctions_STORAGE** for the storage account connection.

4. Change **Table name** to **Products**.

5. Click **Save** to save the newly integrated output table.

   **Figure 7: Configuring the new output**

   ![Kendo UI for jQuery New output configuration](https://www.telerik.com/kendo-jquery-ui/documentation/assets/f4fb67db723b6e5c750d357f51fe5b25/new-output-configuration.png)

## Implementing the Model

The actual implementation requires you to first create a definition for the `Product` class:

1. Select the `Read` function.

2. On the right side, click **View files**.

   **Figure 8: Opening the function files**

   ![Kendo UI for jQuery Open the function files](https://www.telerik.com/kendo-jquery-ui/documentation/assets/d0916a740194b41590527b22bbec4cbf/open-function-files.png)

3. Click the **Add** button and provide the `product.csx` name to the new file.

   **Figure 9: Creating a new function file**

   ![Kendo UI for jQuery Create new function file](https://www.telerik.com/kendo-jquery-ui/documentation/assets/aaa10cdf4936acf22abc0e5f47e7c12f/add-product-file.png)

4. Place the following class definition in the file:

   ```C#
   using Microsoft.WindowsAzure.Storage.Table;

   public class Product :TableEntity
   {
   	public string ProductName { get; set; }

   	public double UnitPrice { get; set; }

   	public int UnitsInStock { get; set; }

   	public bool Discontinued { get; set; }

   	public Product ToEntity()
   	{
   		return new Product
   		{
   			PartitionKey = "Product",
   			RowKey = this?.RowKey,
   			ProductName = this?.ProductName,
   			UnitPrice = this.UnitPrice,
   			UnitsInStock = this.UnitsInStock,
   			Discontinued = this.Discontinued,
   			ETag = "*"
   		};
   	}
   }
   ```

## Implementing the Read Function

1. Under the `Read` function, open the `run.csx` file.

2. Before the initial use, include the following `load` directive that allows you to use the `Model` class definition in the actual function.

   ```C#
   #load "product.csx"
   ```

3. Include a reference to the `Microsoft.WindowsAzure.Storage` and a `using` configuration for the `Table` library.

   ```C#
   #r "Microsoft.WindowsAzure.Storage"
   …
   using Microsoft.WindowsAzure.Storage.Table;
   ```

4. Modify the definition of the `Run` function method. The newly added `inputTable` parameter allows you to get and return the products that are stored in the integrated table storage.

   ```C#
   public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, IQueryable<Product> inputTable, TraceWriter log)
   {
   	if (req.Method == HttpMethod.Get)
   	{
   		// Return the Products table as list
   		return req.CreateResponse(HttpStatusCode.OK, inputTable.ToList(), "application/json");
   	}
   	else
   	{
   		return req.CreateResponse(HttpStatusCode.BadRequest, "This route accepts only GET requests.");
   	}
   }
   ```

## Implementing the Create, Destroy, and Update Functions

Now you can proceed with the implementation of the other three functions. Make all three of them load the `Product` class and refer the `Microsoft.WindowsAzure.Storage` and `Newtonsoft.Json` assemblies. Add the respective `using` configurations.

```C#
#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"
#load "..\Read\product.csx"

using System.Net;
using Microsoft.WindowsAzure.Storage.Table;
using Newtonsoft.Json;
```

As a result, the `Run` methods for each function differ.

The following example demonstrates the `Run` method for the `Create` function.

```C#
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, CloudTable outputTable, TraceWriter log)
{
	dynamic body = await req.Content.ReadAsStringAsync();
	Product data = JsonConvert.DeserializeObject<Product>(body as string);
	Product entity = data.ToEntity();
	string newKey = Guid.NewGuid().ToString();

	entity.RowKey = newKey;
	var operation = TableOperation.Insert(entity);
	await outputTable.ExecuteAsync(operation);

	return req.CreateResponse(HttpStatusCode.OK, entity, "application/json");
}
```

The following example demonstrates the `Run` method for the `Destroy` function.

```C#
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, CloudTable outputTable, TraceWriter log)
{
	dynamic body = await req.Content.ReadAsStringAsync();
	Product data = JsonConvert.DeserializeObject<Product>(body as string);
	Product entity = data.ToEntity();

	var operation = TableOperation.Delete(entity);
	await outputTable.ExecuteAsync(operation);

	return req.CreateResponse(HttpStatusCode.OK, entity, "application/json");
}
```

The following example demonstrates the `Run` method for the `Update` function.

```C#
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, CloudTable outputTable, TraceWriter log)
{
	dynamic body = await req.Content.ReadAsStringAsync();
	Product data = JsonConvert.DeserializeObject<Product>(body as string);
	Product entity = data.ToEntity();

	var operation = TableOperation.Replace(entity);
	await outputTable.ExecuteAsync(operation);

	return req.CreateResponse(HttpStatusCode.OK, entity, "application/json");
}
```

## Configuring the Application

As the implementation is already in place, now you need to add specific configurations to the application and for each of the four functions.

1. Click the application name and select **Platform features**.

2. Under the **API** section, click **CORS**.

   **Figure 10: The platform features of the application**

   ![Kendo UI for jQuery Application platform features](https://www.telerik.com/kendo-jquery-ui/documentation/assets/420f08ce1d81c5b0616ff76487984b40/platform-features.png)

3. Add the domain origin of the client-side application that will consume the functions data and click **Save**. In this case, the client-side application will be located in the kendo UI Dojo. Therefore, the `https://runner.telerik.io` origin is available.

   **Figure 11: Configuration of the CORS options**

   ![Kendo UI for jQuery CORS configuration](https://www.telerik.com/kendo-jquery-ui/documentation/assets/d86ae140b61bad834368cae15c26250c/cors-save.png)

4. Go to the `Read` function and open the `function.json` file.

5. In the **bindings / methods** section, remove **post** as an option.

6. Open the same file for the other three functions but remove the `get` method.

## Consuming the Implemented CRUD Endpoints on the Client

Get the unique URL for each of the functions by clicking the **Get Function URL** link that is available for each of them. Now you need to implement the client that will consume the data from the functions application. The following example demonstrates how to implement the Kendo UI Grid and place the proper endpoints of the CRUD operations in the transport configuration of the data source. As a result, the Grid will be able to consume and edit the data from the Azure Functions application.

```HTML
<div id="grid"></div>
<script>
  $(document).ready(function () {
    $("#grid").kendoGrid({
      toolbar: ["create"],
      columns: [
        { field:"ProductName", title: "Product Name", width: "150px" },
        { field: "UnitPrice", title:"Unit Price", format: "{0:c}", width: "110px" },
        { field: "UnitsInStock", title:"Units In Stock", width: "110px" },
        { field: "Discontinued", width: "110px" },
        { command: ["edit", "destroy"], title: "&nbsp;", width: "190px" }],
      editable: "popup",
      dataSource: {
        batch: false,
        transport: {
          read: {
            url: "[Read Function URL]",
            method: "GET"
          },
          update: {
            url: "[Update Function URL]",
            method: "POST"
          },
          destroy: {
            url: "[Destroy Function URL]",
            method: "POST"
          },
          create: {
            url: "[Create Function URL]",
            method: "POST"
          },
          parameterMap: function(options, operation) {
            if (operation !== "read" && options) {
              return kendo.stringify(options);
            }
          }
        },
        schema: {
          model: {
            id: "RowKey",
            fields: {
              RowKey: { editable: false, nullable: true },
              ProductName: { validation: { required: true } },
              UnitPrice: { type: "number", validation: { required: true, min: 1} },
              Discontinued: { type: "boolean" },
              UnitsInStock: { type: "number", validation: { min: 0, required: true } }
            }
          }
        }
      }
    });
  });
</script>
```

## See Also

* [Kendo UI Grid Overview](https://www.telerik.com/kendo-jquery-ui/documentation/controls/grid/overview)
* [Binding to Azure Cosmos DB](https://www.telerik.com/kendo-jquery-ui/documentation/kendo-in-the-cloud/azure-cosmos-db)
* [Integrating Kendo UI with Azure Face API](https://www.telerik.com/kendo-jquery-ui/documentation/kendo-in-the-cloud/azure-faceapi)
* [Consuming Data from Amazon DynamoDB](https://www.telerik.com/kendo-jquery-ui/documentation/kendo-in-the-cloud/aws-dynamodb)
