How To Create Azure Function App To Delete SharePoint Online List Using CSOM

Updated: Mar 18, 2019

Azure Functions are used for executing a small piece of code or “functions” in a cloud and you will 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, bring your own dependencies, integrated security, simplified integration, flexible development, and open-source. Please refer Azure Functions for more details.


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

1. Log in to the Azure Portal.

2. Click New-> Compute -> Function App.


3. Enter all the required details and click Enter.


4. The Functions app will be provisioned within a few minutes.

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


6. Click Custom Function.


7. Select HTTP Trigger -> C#.


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


9. We need to add the dependencies to access the CSOM code. Click Azure Functions app and then click "Platform features".


10. Click "Advanced tools (Kudu)" under development tools.


11. Click "CMD" under Debug console.


12. Click the folder named "site".


13. Click wwwroot.


14.. Click the function folder.


15. Create a new folder and name it as bin. Drag and drop the below mentioned DLL files.



16. 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="vijaianand@c986.onmicrosoft.com";      

string password="*********";  

// parse query parameter     

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

1. Click Get function URL and copy the URL.


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


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

Recent Posts

See All