Serverless — Azure Function and SharePoint CSOM

Sumit Agrawal
ng-sp
Published in
2 min readMay 26, 2018
Image Courtesy : https://azure.microsoft.com/

Azure Functions are compute resources that are also referred as serverless.(of course they run on server but we never have to worry about it )
Azure Functions have triggers that define when these functions should be executed. One of the trigger type is HttpTrigger. You can invoke Azure Function by calling an endpoint this endpoint is capable of sending you back a response, how cool it that!

Possibilities with Azure Function and CSOM

  • Run code with elevated privilages — This is one of the biggest benefits you can get out of Azure Function using CSOM. You can call this function from JavaScript directly to perform elevated operations.
  • Build Custom API Wrapper on top of SharePoint API — Azure Function can greatly simplify API calls by providing a proxy behavior.
  • Easy integration of non-Dot Net Applications — Your Dot Net Application will be Azure Function that can server any client.
  • CAS protection to resources — You do not share access to your SharePoint resources, you can build your own CAS model as per your business need as Azure Function.
  • And many more…

Create New Function App and function

Login to Azure Portal at https://portal.azure.com and create new Function App instance.
Select Consumption Plan and fill out other details.
Once Function App is created, Create new Function inside Function App. Provide proper name and for simplicity, select HttpTrigger-CSharp as template.

When this function is ready, create a new project.json file. This is the way to tell Azure Function to go and get nuget package which has been added as a dependency.
SharePointPnPCoreOnline is nuget package that we will be using for authenticating with SharePoint Online.

{
"frameworks": {
"net46": {
"dependencies": {
"SharePointPnPCoreOnline": "2.15.1705"
}
}
}
}

Replace the content of run.csx file with below mentioned code. Replace url, username and password with acutal values and run the code.

using System;  
using OfficeDevPnP.Core;
using Microsoft.SharePoint.Client;
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.Online.SharePoint.TenantManagement;
public static void Run(TimerInfo myTimer, TraceWriter log)
{
AuthenticationManager authManager = new AuthenticationManager();
ClientContext ctx = authManager
.GetSharePointOnlineAuthenticatedContextTenant("url",
"username", "passowrd");
Web web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQueryRetry();
log.Info(web.Title);
}

You just called SharePoint CSOM API using Azure Function with just 4 lines of code!
We will see how to build production quality Azure Function using other features like Application settings, Access Code and CORS implementation next.

--

--