How To Create A Custom SharePoint Online List Using Azure Function

Introduction

Azure Functions is used for executing a small piece of code or “function” in a cloud and you pay only for the time your code executes. You can use a development language of choice, such as C#, F#, PHP, Java etc. Some of the key features of Functions are Choice of language, Pay-per-use pricing model, bring your own dependencies, integrated security, simplified integration, flexible development, and open-source. Please refer Azure Functions for more details. Also, refer my previous articles.

In this blog, you will see how to create a custom SharePoint Online list using Azure Functions.

Schedule an Azure function

Log in to the Azure Portal.

Click New-> Compute -> Function App.

Enter all the required details and click Enter.

Functions app will be provisioned within a few minutes.

Click Functions App->AzureFunctionsExamples (which you have created) -> Functions -> “+” to create a new function.

Click Custom Function.

Select HTTP Trigger -> C#.

Enter the name of the new function and click "Create".

Refer to my previous article to add CSOM dependencies. Replace the code in run.csx with the following code.

  1. #r "Microsoft.SharePoint.Client.dll"  
  2. #r "Microsoft.SharePoint.Client.Runtime.dll"  
  3.   
  4. using System.Net;  
  5. using Microsoft.SharePoint.Client;  
  6.   
  7.   
  8. public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)  
  9. {  
  10.     string siteURL="https://c986.sharepoint.com/sites/Vijai/Subsite";  
  11.     string userName="[email protected]";  
  12.     string password="*********";  
  13.   
  14.     log.Info("C# HTTP trigger function processed a request.");  
  15.   
  16.     // parse query parameter  
  17.     string listName = req.GetQueryNameValuePairs()  
  18.         .FirstOrDefault(q => string.Compare(q.Key, "listName"true) == 0)  
  19.         .Value;  
  20.   
  21.     string description = req.GetQueryNameValuePairs()  
  22.         .FirstOrDefault(q => string.Compare(q.Key, "description"true) == 0)  
  23.         .Value;  
  24.   
  25.     // Get request body  
  26.     dynamic data = await req.Content.ReadAsAsync<object>();  
  27.   
  28.     // Set name to query string or body data  
  29.     listName = listName ?? data?.name.listName;  
  30.     description = description ?? data?.name.description;  
  31.   
  32.     System.Security.SecureString secureString=new System.Security.SecureString();  
  33.     foreach(char ch in password)  
  34.     {  
  35.         secureString.AppendChar(ch);          
  36.     }  
  37.   
  38.     SharePointOnlineCredentials creds=new SharePointOnlineCredentials(userName, secureString);  
  39.   
  40.     using(var ctx=new ClientContext(siteURL))  
  41.     {  
  42.         ctx.Credentials=creds;  
  43.         ListCreationInformation creationInfo=new ListCreationInformation();  
  44.         creationInfo.Title=listName;  
  45.         creationInfo.Description=description;  
  46.         creationInfo.TemplateType = (int)ListTemplateType.GenericList;  
  47.         List newList=ctx.Web.Lists.Add(creationInfo);  
  48.         ctx.Load(newList);  
  49.         ctx.ExecuteQuery();  
  50.         return data.name == null  
  51.             ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")  
  52.             : req.CreateResponse(HttpStatusCode.OK, "List created successfully. Lits Name: " + listName+" Description: "+description);  
  53.     }  
  54. }  

Test the function

Update the request body. Save and run the function.

You could check the logs to verify the Azure Functions execution. A new SharePoint Online custom list is created successfully.

Result

Thus, in this blog, you saw how to create a custom SharePoint Online list using Azure Functions.

Ebook Download
View all
Learn
View all