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 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, ability to bring your own dependencies, integrated security, simplified integration, flexible development, and open-source. Please refer Azure Functions for more details. Also, you can read my previous articles related to Azure Functions below:
In this article, you will see how to create an Azure Functions app to delete SharePoint Online list using CSOM that will run whenever an HTTP request is received.
Create Azure Functions app on the Azure portal
- Log in to the Azure Portal.
- Click New-> Compute -> Function App.
- Enter all the required details and click Enter.
- The Functions app will be provisioned within a few minutes.
- Click Function Apps->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".
- We need to add the dependencies to access the CSOM code. Click Azure Functions app and then click "Platform features".
- Click "Advanced tools (Kudu)" under development tools.
- Click "CMD" under Debug console.
- Click the folder named "site".
- Click wwwroot.
- Click the function folder.
- Create a new folder and name it as bin. Drag and drop the below mentioned DLL files.
- Navigate to the function and replace the code in csx with the below code. Save the changes.
- #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="*********";
-
-
- string title = req.GetQueryNameValuePairs()
- .FirstOrDefault(q => string.Compare(q.Key, "title", true) == 0)
- .Value;
-
- 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;
- List list=ctx.Web.Lists.GetByTitle(title);
- list.DeleteObject();
- ctx.ExecuteQuery();
- return list == null
- ? req.CreateResponse(HttpStatusCode.BadRequest, "Error retreiveing the list")
- : req.CreateResponse(HttpStatusCode.OK, "ListDeleted successfully " + title);
- }
- return null;
- }
Test the function
- Click Get function URL and copy the URL.
- Append “&title=<listname>” to the copied function URL and enter into the browser. SharePoint Online list will be deleted successfully.
For example
https://azurefunctionsexamples.azurewebsites.net/api/HttpTriggerCSharpDemo?code=ko6560WiKCioJVUaxSV9GAlbWJ0iWBhyPgcpbbfhYFmQJPLaGqGa6Q==&title=Logic Apps List
- Also, you could test it on the same page by entering the query parameter value and then by clicking Run. You could also trace the logs the output as shown in below screenshot.
Result
Thus, in this article, you saw how to create Azure Functions app to delete SharePoint Online list using CSOM.