top of page

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"

  3. using System.Net;  

  4. using Microsoft.SharePoint.Client;  

  5. public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)  

  6. {     

  7.  log.Info("C# HTTP trigger function processed a request.");  

  8. // parse query parameter

  9. string name = req.GetQueryNameValuePairs()          

  10. .FirstOrDefault(q => string. Compare(q.Key, "listname", true) == 0)         

  11.  .Value;  

  12. if(name != null){  

  13. // Input Parameters

  14. string siteUrl = "";  

  15. string userName = "";  

  16. string password = "***";          

  17. System.Security.SecureString securestring = new System.Security.SecureString();  

  18. foreach (char ch in password)              

  19. securestring.AppendChar(ch);          

  20. SharePointOnlineCredentials creds = new SharePointOnlineCredentials(userName, securestring  

  21. try

  22. {  

  23. // Get and set the client context

  24. // Connects to SharePoint online site using inputs provided

  25. using (var clientContext = new ClientContext(siteUrl))              

  26. {                  

  27. clientContext.Credentials = creds;                  

  28. Web web = clientContext.Site.RootWeb;                  

  29. ListCreationInformation listInfo = new ListCreationInformation();                  

  30. listInfo.Title = name;                  

  31. listInfo.TemplateType = 100;                  

  32. web.Lists.Add(listInfo);                  

  33. clientContext.Load(web);                  

  34. clientContext.ExecuteQuery();                  

  35. log.Info($"The list {name} is created");  

  36. return req.CreateResponse(HttpStatusCode.OK, "List is created");              

  37. }          

  38. }  

  39. catch(Exception ex){              

  40. log.Info($"Exception: {ex.Message}");  

  41. return req.CreateResponse(HttpStatusCode.BadRequest, "Error creating the list. Please reach out to admin");          

  42. }      

  43. }  

  44. else{          

  45. log.Info(name);  

  46. return req.CreateResponse(HttpStatusCode.BadRequest, "Error creating the list. Please reach out to admin");      

  47. }   }  

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. {              

  8. console.log("List is created");          

  9. },          

  10. error: function (data)

  11. {              

  12. console.log("Error Creating List");                   

  13. }      

  14. });  

  15. });  

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.



bottom of page