Using Azure Functions within SharePoint
By now you should have heard about Azure Function and how powerful they can be, not just for SharePoint. In case you haven’t heard about them let’s review what they are and how to use them in general. What are Azure Functions?
June 16, 2017
![Using Azure Functions within SharePoint Using Azure Functions within SharePoint](https://eu-images.contentstack.com/v3/assets/blt07f68461ccd75245/bltd8e49e1603eb4997/6619583127f964eab99671cb/azurelogo_0_4.jpg?width=1280&auto=webp&quality=95&format=jpg&disable=upscale)
By now you should have heard about Azure Function and how powerful they can be, not just for SharePoint. In case you haven’t heard about them let’s review what they are and how to use them in general.
What are Azure Functions?
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. You can write just the code you need for the problem at hand, without worrying about a whole application or the infrastructure to run it. Functions can make development even more productive, and you can use your development language of choice, such as C#, F#, Node.js, Python or PHP. Pay only for the time your code runs and trust Azure to scale as needed. Azure Functions lets you develop serverless applications on Microsoft Azure.
Azure functions is the almost perfect solution for processing data, integrating systems, working with Internet-of-things (IoT) and for building simple APIs. Any type of scheduled task, or process that runs from an event can be built using Azure Functions.
Azure Functions can be created using base templates for all kinds of scenarios. Microsoft have provided some core templates that can be used:
Blob Trigger – process Azure Storage blobs
EventHub Trigger – respond to Azure Event Hub
Generic Webhook – process Webhook HTTP requests
GitHub Webhook – respond to GitHub events
HTTP Trigger – trigger execution of code using HTTP request
Queue Trigger – respond to messages from the Azure Storage Queue
ServiceBus Topic Trigger – process messages from queue and execute either Azure or On-premises services
ServiceBus Topic Trigger – process messages from queue and execute either Azure or On-premises services suing topics
Timer Trigger – execute cleanup or batch tasks on a schedule
How do you use an Azure Function with SharePoint?
Azure Functions don’t get connected directly to SharePoint as such, due to them being a different service within Azure. Instead you can utilize hook in requests and provide a HTTP callable URL that can act as the initiator, which will execute whatever code you have written. What’s great is that by converting your code into Azure Functions it can then be called using whichever coding language or client side framework you use.
How to create an Azure Function?
The first step you'll need to do is create an Azure Function App, which is a special kind of Azure Web App focused on hosting Azure Functions. Navigate to https://portal.azure.com, click on the "New" and search for "Function app".
Once sent to the core list of functional apps, select “Function App”, then press the “Create” button.
Once the property options you will need to populate the values as needed. For me I used my DEV/TEST subscription.
The process for creating the function app can take a few minutes but once done you will be notified. Clicking the notification upon completion will display the core settings for the function.
To add an actual function, click the “+” button by the “Functions” left navigation link. You will then be prompted to select the type you wish to create.
For now, we will select the “Webhook + API” option and choose “CSharp” as the language, then select the “Create this function” link. Once created you will be presented with the sample code below.
To see what this core code does you can press the “Run” option which will give you an idea of how these functions work. When you do this, you will see that you can change the type of interaction from POST, to GET to a whole host of other options. You can then also submit the body request.
This is simply going to set “name” equal to “Liam”, and when it runs, it should return that as the outcome.
In reality we will be calling this URL from somewhere else so we will need a functional URL to connect to. At the top of the screen click the link called “Get function URL” which will then display the actual URL to call this code. Calling this directly in the browser will have no affect at all, in fact it will simply return the “Please pass a name on the query string or in the request body” message. To get around this, add this to the URL: “&name=James$20%Brown”.
https://site-af.azurewebsites.net/api/HttpTriggerCSharp1?code=MYyccnLn88e4RJkiTyukfZGufQQxTByp8ATyty3rcTrnwOZj/4EriQ==&name=James%20%Brown
This will then return the welcome message with the “James Brown” text appended to it.
Now that we have a basic function, we can now configure SharePoint to utilize this (of course this is not a real useful function). To utilize it the function must be granted access through Azure Active Directory, then you can utilize SharePoint Webhooks, by adding this new function to a SharePoint list using the subscription model. A SharePoint list Webhook can handle events corresponding to list item changes for the following events:
ItemAdded
ItemUpdated
ItemDeleted
ItemCheckedOut
ItemCheckedIn
ItemUncheckedOut
ItemAttachmentAdded
ItemAttachmentDeleted
ItemFileMoved
ItemVersionDeleted
ItemFileConverted
SharePoint Webhooks provide a simple notification pipeline so your application can be aware of changes to a SharePoint list without polling the service. Also by writing code that simply receives the notification, stores what is needs to a queue and processes later you can create very complex solutions.
Subscriptions can be created using the core SharePoint API and posting a set values to it. For this you can following the examples found here on the Microsoft Office Dev Center.
Create a Subscription
https://dev.office.com/sharepoint/docs/apis/webhooks/lists/create-subscription
Update a Subscription
https://dev.office.com/sharepoint/docs/apis/webhooks/lists/update-subscription
Delete a Subscription
https://dev.office.com/sharepoint/docs/apis/webhooks/lists/delete-subscription
Get Subscriptions
https://dev.office.com/sharepoint/docs/apis/webhooks/lists/get-subscription
There are so many use cases for using Azure Function, moving your code into the serverless notification type development approach. However, be advised that Azure Functions using Webhooks should not be these super heavy coding mechanisms, you need to remember the end use experience, it is all about it being responsive.
About the Author
You May Also Like