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

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.