Intro

In previous post I showed how to create Azure Active Directory app and set up required permissions. Also, I provided the example of code that can be used to interact with SharePoint Online REST API using SharePoint CSOM. In this post I am going to apply a similar approach in order to provision SharePoint Online artifacts to the target site using Azure Functions.

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

Microsoft advertises Azure Functions as a solution for easily running small pieces of code ("functions") in the cloud. You can write the code you need for the problem at hand, without worrying about a whole application of the infrastructure to run it. You can find more information about Azure Functions at Azure Functions Overview

Usually Azure Function is triggered by some event, processes event and outputs result to a destination. Our Azure function are going to be triggered by HTTP request, it will receive SharePoint site URL, and a list name as parameters. The function will create a new list on the site with given URL. I will also create a second function to provision one more list that I will use in one of the next posts.

Create new or use the existing resource group and search for Function App:

https://raw.githubusercontent.com/boades/boades-blog-content/master/03-azure/02-azure-o365-serverless-integration/02-how-to-create-azure-function-sharepoint-csom-rest/01-search-function-app.png

Then create a new function app:

https://raw.githubusercontent.com/boades/boades-blog-content/master/03-azure/02-azure-o365-serverless-integration/02-how-to-create-azure-function-sharepoint-csom-rest/02-function-app-create.png

Now create a new function called ProvisionSourceList:

https://raw.githubusercontent.com/boades/boades-blog-content/master/03-azure/02-azure-o365-serverless-integration/02-how-to-create-azure-function-sharepoint-csom-rest/03-new-function.png

Also we will need another function called ProvisionDestinationList to provision the destination list, use the same function type.

Create a new file called project.json and specify packages required by the function:

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "Microsoft.IdentityModel.Clients.ActiveDirectory" : "3.13.7",
        "Microsoft.SharePointOnline.CSOM": "16.1.5813.1200"
      }
    }
   }
}

The next step is to upload the certificate that has been generated on the previous step, you should have the following files:

https://raw.githubusercontent.com/boades/boades-blog-content/master/03-azure/02-azure-o365-serverless-integration/02-how-to-create-azure-function-sharepoint-csom-rest/04-upload-certificate.png

I am not going to copy-paste whole code here for both functions, because it is a bit lengthy. I published source code for these two functions to GitHub:

Make sure you uploaded .pfx certificate and updated the following parameters:

https://raw.githubusercontent.com/boades/boades-blog-content/master/03-azure/02-azure-o365-serverless-integration/02-how-to-create-azure-function-sharepoint-csom-rest/05-parameters.png

Make sure that you included all required packages. It is possible to test a function directly from the editor, just click on Test and specify required parameters:

https://raw.githubusercontent.com/boades/boades-blog-content/master/03-azure/02-azure-o365-serverless-integration/02-how-to-create-azure-function-sharepoint-csom-rest/06-test-function.png

It is also possible to test a function by invoking it by URL from browser, just copy paste a function URL and add required parameters to the query string, it should looks like this:

https://office365apps.azurewebsites.net/api/ProvisionSourceList?code={YOUR_AUTH_CODE_HERE}&siteUrl=https://oleksiionsoftware.sharepoint.com/sites/dev&listTitle=Source%20List%201
https://office365apps.azurewebsites.net/api/ProvisionDestinationList?code={YOUR_AUTH_CODE_HERE}&siteUrl=https://oleksiionsoftware.sharepoint.com/sites/dev&listTitle=Destination%20List%201

As a result following lists has been created:

Source List 1:

https://raw.githubusercontent.com/boades/boades-blog-content/master/03-azure/02-azure-o365-serverless-integration/02-how-to-create-azure-function-sharepoint-csom-rest/07-src-list.png

Destination List 1:

https://raw.githubusercontent.com/boades/boades-blog-content/master/03-azure/02-azure-o365-serverless-integration/02-how-to-create-azure-function-sharepoint-csom-rest/08-dest-list.png

Summary

In this post I have described how to create simple Azure Functions. Also, I showed how to work with NuGet packages from Azure Functions and how to use ADAL.NET to get access token from Azure Active Directory in order to use SharePoint Online CSOM. In the next post I am going to create another function that will be used for data transformation.


;