Working With Azure Functions From SharePoint Components


In this article, you will learn how to trigger Azure functions from SharePoint pages.

Azure functions are the ways to run the solutions or piece of code over Cloud. It helps in processing the data or integrating other systems or even working with the Internet of Things.


Azure functions can run only on the admin context for SharePoint. The end users from SharePoint can trigger Azure functions , using the URL but the code execution will run under admin context.

In this sample, let us see how to trigger Azure functions, using C# from SharePoint Web parts. Consider a scenario where end users invoke the admin operations, using Azure function. For example, creating a list on another SharePoint site.

In my previous article, you can learn about creating Azure functions for SharePoint operations, using managed CSOM.

Functions can be triggered in multiple ways. Here, we will create HTTP trigger function. More details and pricing information about the Azure functions can be found here.


  • Microsoft Azure account with Azure functions app.
  • SharePoint online portal

How it works

Azure functions can be GET, POST, DELETE etc. In this approach, let us look at the GET method. The trigger happens, using the function app URL.

The data to the function can be passed in two ways. One using the query string parameter and another using the request body (POST method).

Let us see how the data can be passed, using query string parameter and the function is executed.

From Azure portal, create new function app, using HttpTrigger-CSharp component. My previous article explains in depth about creating Azure functions, using SharePoint CSOM.

To enable CORS, add the source site URL in the CORS section of Azure function app settings.


The piece of code given below creates a SharePoint list on Office 365 site from Azure function and returns the success/ failure response. The site details and admin credentials are embedded within the code. The same can be retrieved from the settings (not covered in this article).

  1. #r "Microsoft.SharePoint.Client.Runtime.dll"  
  2. #r "Microsoft.SharePoint.Client.dll"  
  4. using System.Net;  
  5. using Microsoft.SharePoint.Client;  
  7. public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)  
  8. {  
  9.     log.Info("C# HTTP trigger function processed a request.");  
  12.     // parse query parameter  
  13.     string name = req.GetQueryNameValuePairs()  
  14.         .FirstOrDefault(q => string.Compare(q.Key, "listname"true) == 0)  
  15.         .Value;  
  17.     if(name != null){  
  18.         // Input Parameters  
  19.         string siteUrl = "";  
  20.         string userName = "";  
  21.         string password = "***";  
  22.         System.Security.SecureString securestring = new System.Security.SecureString();  
  23.         foreach (char ch in password)  
  24.             securestring.AppendChar(ch);  
  25.         SharePointOnlineCredentials creds = new SharePointOnlineCredentials(userName, securestring);  
  26.         try{  
  27.             // Get and set the client context  
  28.             // Connects to SharePoint online site using inputs provided  
  29.             using (var clientContext = new ClientContext(siteUrl))  
  30.             {  
  31.                 clientContext.Credentials = creds;  
  32.                 Web web = clientContext.Site.RootWeb;  
  33.                 ListCreationInformation listInfo = new ListCreationInformation();  
  34.                 listInfo.Title = name;  
  35.                 listInfo.TemplateType = 100;  
  36.                 web.Lists.Add(listInfo);  
  37.                 clientContext.Load(web);  
  38.                 clientContext.ExecuteQuery();  
  39.                 log.Info($"The list {name} is created");  
  40.                 return req.CreateResponse(HttpStatusCode.OK, "List is created");  
  41.             }  
  42.         }  
  43.         catch(Exception ex){  
  44.             log.Info($"Exception: {ex.Message}");  
  45.             return req.CreateResponse(HttpStatusCode.BadRequest, "Error creating the list. Please reach out to admin");  
  46.         }  
  47.     }  
  48.     else{  
  49.         log.Info(name);  
  50.         return req.CreateResponse(HttpStatusCode.BadRequest, "Error creating the list. Please reach out to admin");  
  51.     }  
  53. }  

The piece of code given below has JavaScript triggers and Azure function created above use the function URL. Copy and paste the function URL from the above Azure function app (from portal) and append listname query string.

  1. $(document).ready(function(){  
  2.     var listName = "DevList";  
  3.     $.ajax({  
  4.         url: ""+listName,  
  5.         type: "GET",  
  6.         success: function (data){  
  7.             console.log("List is created");  
  8.         },  
  9.         error: function (data){  
  10.             console.log("Error Creating List");           
  11.         }  
  12.     });  
  13. });  

Execute AJAX call from SharePoint page, using CEWP on the page load. The result will be displayed on the console for the code given above.


In this article, you have learned about creating a list operation on the Azure functions app and invoking the function from SharePoint site.