Office 365 has allowed many organizations to offload the burden of infrastructure management to Microsoft. This frees up time and resources that can be used to focus on projects that have a greater impact on your business. However, migrating to the cloud can come with some challenges. One of the challenges with Office 365 SharePoint sites is integrating with third party services. When you need to create apps, remote triggers, event receivers, or web service that integrate with SharePoint, there’s no place to house these solutions. Until recently, you would have had to put a server in place somewhere to support this functionality.
Azure Functions can eliminate the need to have a server to host these solutions. Here’s an overview on how it’s done.
Azure Functions can assist in multiple ways, including:
- An HTTP Trigger
- A Timer Job
- A Blob Trigger
- Many More
In this article we’ll review how to implement an Azure function with SharePoint using C#. Below is a summary of the common setup steps in order to communicate with SharePoint.
- Create the Azure function
- Create an HTTP triggered function using C#
- Add a project.json file to the function
- Generate a certificate
- Register the application in Azure Active Directory
- Upload the certificate
- Update run.csx
Create the Azure Function
Follow the steps in this article to create an Azure function.
Create an HTTP triggered function using C#
Upon creation of the function app, the app will open.
From the left navigation, click on the “+” sign to add the new function. You can select from the “Quickstart” option or click on “Create custom function” option listed below.
If you select the “Custom function” option it will give you a list of all the function options.
Filter the language to “C#” and select “HttpTrigger-Csharp” function type.
Add project.json file to the function
Create a new file called project.json and specify the packages required by the function:
“Microsoft.IdentityModel.Clients.ActiveDirectory” : “3.13.7”,
Generate the Certificate
In order to communicate with SharePoint you will have to generate a certification. Please follow the steps mentioned in this article (under the “Generate certificate” section).
Register the application in Azure Active Directory
Use the article referenced above to register your application in Azure Active Directory (under the “Register the application in Azure Active Directory” section).
This process will provide a Client Id which is used by your Azure function to authenticate to the SharePoint site.
Upload the certificate
Upload the certificate you created in the “Generate Certificate” step (you will need the .pfx file). Once the file is uploaded, you’ll see the following files in the function:
All the background work has been done to communicate with SharePoint site from your new Azure function. The next step is to add a connection string in run.csx and define the “clientcontext”.
Add the following lines of code before the “run” function:
private static string ClientId = “<Guid we got when application is registered with Azure AD>”;
private static string Cert = “O365S2S.pfx”; // The certificate we added to the function
private static string CertPassword = “O365S2S”; //the password we set during the export
private static string Authority = “https://login.windows.net/<domain>.onmicrosoft.com/”;
private static string Resource = “https://<domain>.sharepoint.com/”;
Following code will create the clientcontext object
private async static Task<ClientContext> GetClientContext(string siteUrl)
var authenticationContext = new AuthenticationContext(Authority, false);
var certPath = Path.Combine(Environment.GetEnvironmentVariable(“HOME”), “site\\wwwroot\\<Function App Name>\\”, Cert);
var cert = new X509Certificate2(System.IO.File.ReadAllBytes(certPath),
var authenticationResult = await authenticationContext.AcquireTokenAsync(Resource, new ClientAssertionCertificate(ClientId, cert));
var token = authenticationResult.AccessToken;
var ctx = new ClientContext(siteUrl);
ctx.ExecutingWebRequest += (s, e) =>
e.WebRequestExecutor.RequestHeaders[“Authorization”] = “Bearer ” + authenticationResult.AccessToken;
Now you can use the “clientcontext” object to manipulate the SharePoint site, list and libraries.
Hosting and integrating components like these has been an obstacle for many organizations migrating an on-premises SharePoint site collection to SharePoint Online. Azure Functions can help to bridge the capability gap.
Do you have questions about how or when to leverage Azure Functions? We’re happy to help out. Email us at email@example.com to get started.