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.
- #r "Microsoft.SharePoint.Client.dll"
- #r "Microsoft.SharePoint.Client.Runtime.dll"
-
- using System.Net;
- using Microsoft.SharePoint.Client;
-
-
- public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
- {
- string siteURL="https://c986.sharepoint.com/sites/Vijai/Subsite";
- string userName="[email protected]";
- string password="*********";
-
- log.Info("C# HTTP trigger function processed a request.");
-
-
- string listName = req.GetQueryNameValuePairs()
- .FirstOrDefault(q => string.Compare(q.Key, "listName", true) == 0)
- .Value;
-
- string description = req.GetQueryNameValuePairs()
- .FirstOrDefault(q => string.Compare(q.Key, "description", true) == 0)
- .Value;
-
-
- dynamic data = await req.Content.ReadAsAsync<object>();
-
-
- listName = listName ?? data?.name.listName;
- description = description ?? data?.name.description;
-
- System.Security.SecureString secureString=new System.Security.SecureString();
- foreach(char ch in password)
- {
- secureString.AppendChar(ch);
- }
-
- SharePointOnlineCredentials creds=new SharePointOnlineCredentials(userName, secureString);
-
- using(var ctx=new ClientContext(siteURL))
- {
- ctx.Credentials=creds;
- ListCreationInformation creationInfo=new ListCreationInformation();
- creationInfo.Title=listName;
- creationInfo.Description=description;
- creationInfo.TemplateType = (int)ListTemplateType.GenericList;
- List newList=ctx.Web.Lists.Add(creationInfo);
- ctx.Load(newList);
- ctx.ExecuteQuery();
- return data.name == null
- ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
- : req.CreateResponse(HttpStatusCode.OK, "List created successfully. Lits Name: " + listName+" Description: "+description);
- }
- }
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.