Intro

In the previous post I described how to implement Azure Function for data transformation. In this post I am going to show how to use Azure Logic Apps in order to invoke a function when a new item is added to the list.

Prerequisites

You need to have Azure Subscription and Office 365 Dev Tenant. Refer to the previous posts in this category to get started with Azure Active Directory apps.

Code

In on-prem versions of SharePoint it is possible to implement a server-side event handler in order to react to changes in a list. Like we all know, in SharePoint Online this option is unavailable. Another option is to implement WCF service on a back-end and register it as a remote event receiver of the source list. This option is available for SharePoint 2013, SharePoint 2016 and SharePoint Online. Unfortunately, to implement this solution, you will have to make significant investment into development and deployment of a custom WCF service. Also, it will cost around 50$ per month to keep it running on Azure.

Recently Microsoft announced webhooks for SharePoint Online lists. This is a great feature that suits our needs. I am not going to describe how webhooks work, you can find more information at Overview of SharePoint webhooks. Very important thing to know is that Azure Logic Apps use webhooks to receive notifications about changes in the list, we will use this awesome feature.

Create a new Azure Logic App using Azure Portal:

https://raw.githubusercontent.com/boades/boades-blog-content/master/03-azure/02-azure-o365-serverless-integration/04-how-to-use-logic-apps-to-invoke-azure-functions/01-create-logic-app.png

Add a new trigger SharePoint - When a new list item is created, provide credentials:

https://raw.githubusercontent.com/boades/boades-blog-content/master/03-azure/02-azure-o365-serverless-integration/04-how-to-use-logic-apps-to-invoke-azure-functions/02-add-trigger.png

And specify required parameters as well as action that needs to be executed when a new item is added to the list, it should look like this:

https://raw.githubusercontent.com/boades/boades-blog-content/master/03-azure/02-azure-o365-serverless-integration/04-how-to-use-logic-apps-to-invoke-azure-functions/03-logic-app.png

I use the following URL to invoke the Azure Function:

https://office365apps.azurewebsites.net/api/TransformListData?code={YOUR_AUTH_CODE}&siteUrl=https://oleksiionsoftware.sharepoint.com/sites/dev&sourceListTitle=Source%20List&destListTitle=Destination%20List

This Logic App subscribes to all changes in Source List and invokes TransformListData Azure Function, which does the actual job. You can see that the subscription has been attached to the Source List:

https://raw.githubusercontent.com/boades/boades-blog-content/master/03-azure/02-azure-o365-serverless-integration/04-how-to-use-logic-apps-to-invoke-azure-functions/04-subscription.png

So when new item is added to list the following URL will be invoked:

https://logic-apis-australiaeast.azure-apim.net/apim/sharepointonline/ontableupdatedhook?r=https%253a%252f%252fprod-03.australiaeast.logic.azure.com%252fworkflows%252f55c4d4631435450bb240f8df4f39ed76%252ftriggers%252fWhen_a_new_item_is_created%252frun%253fapi-version%253d2016-06-01%2526sp%253d%25252Ftriggers%25252FWhen_a_new_item_is_created%25252Frun%25252C%25252Ftriggers%25252FWhen_a_new_item_is_created%25252Fread%2526sv%253d1.0%2526sig%253d_NV4wqF3_QwZKv_OOS8L-5QQL0EeDSsYXhOENc7Trf8

You can also add webhook manually, but in this case you will have to write custom code to get changes from the list, so Azure Logic Apps make our life easier. In any case, you can use the following snippets to manualy work with subscriptions:

Get list of subscriptions:

$.ajax({
    url: "/sites/dev/_api/Web/Lists(guid'a8ed4058-4e4c-464a-b997-41d46c25f1ef')/subscriptions",
    headers: {
        "Content-Type" : "application/json",
        "Accept": "application/json;odata=nometadata"
    }
}).done(function (data) { 
    console.log(data); 
});

Add subscription:

$.ajax({
    type: "POST",
    url: "/sites/dev/_api/Web/Lists(guid'a8ed4058-4e4c-464a-b997-41d46c25f1ef')/subscriptions",
    headers: {
        "Content-Type" : "application/json",
        "Accept": "application/json;odata=nometadata",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    data: JSON.stringify({
        "resource": "https://oleksiionsoftware.sharepoint.com/sites/dev/_api/Web/Lists(guid'a8ed4058-4e4c-464a-b997-41d46c25f1ef')", 
        "notificationUrl":"https://f6e11ccc.ngrok.io", 
        "expirationDateTime": "2017-01-01T16:00:00+00:00",   
        "clientState": "A0A354EC-97D4-4D83-9DDB-144077ADB440" 
    })
}).done(function (data) {
    console.log(data);
});

Remove subscription:

$.ajax({
    type: "DELETE",
    url: "/sites/dev/_api/Web/Lists(guid'a8ed4058-4e4c-464a-b997-41d46c25f1ef')/subscriptions('b2992b05-9703-441c-8420-14ddf7ba9caf')",
    headers: {
        "Content-Type" : "application/json",
        "Accept": "application/json;odata=nometadata",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    }
}).done(function (data) {
    console.log(data);
});

And you should have Azure Function (or anything that can handle requests) on the receiving side. SharePoint Online will send you a request similar to this one:

{"value":[{"subscriptionId":"1ccea031-1017-40c5-9795-7e0e2b26164c","clientState":"A0A354EC-97D4-4D83-9DDB-144077ADB440","expirationDateTime":"2017-01-01T16:00:00.0000000Z","resource":"a8ed4058-4e4c-464a-b997-41d46c25f1ef","tenantId":"25a57393-26b8-4c17-a34e-17f17519e51a","siteUrl":"/sites/dev","webId":"ae4a582f-10d6-4d00-91d9-1e73cde86a42"}]}

You can find Azure Function sample on GitHub

Summary

In this post I have described how to create a simple Azure Logic App that subscribes to changes in SharePoint Online List using webhooks and invokes TransformListData function. In the next post I am going to show how we can use data from Destination List to visualize data on the page. Also I will provide source code of couple useful SPFx web parts.


;