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

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.

    Azure Functions

  1. Enter all the required details and click Enter.

    Azure Functions

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

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

    Azure Functions

  1. Click Custom Function.

    Azure Functions

  1. Select HTTP Trigger -> C#.

    Azure Functions

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

    Azure Functions

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

    Azure Functions

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

    Azure Functions

  1. Click "CMD" under Debug console.

    Azure Functions

  1. Click the folder named "site".

    Azure Functions

  1. Click wwwroot.

    Azure Functions

  1. Click the function folder.

    Azure Functions

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

    Azure Functions

    Azure Functions

  1. Navigate to the function and replace the code in csx with the below code. Save the changes.

    Azure Functions
    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. public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)  
    8. {  
    9.     string siteURL="https://c986.sharepoint.com/sites/Vijai/Subsite";  
    10.     string userName="[email protected]";  
    11.     string password="*********";  
    12.   
    13.       // parse query parameter  
    14.     string title = req.GetQueryNameValuePairs()  
    15.         .FirstOrDefault(q => string.Compare(q.Key, "title"true) == 0)  
    16.         .Value;  
    17.   
    18.     System.Security.SecureString secureString=new System.Security.SecureString();  
    19.     foreach(char ch in password)  
    20.     {  
    21.         secureString.AppendChar(ch);          
    22.     }  
    23.   
    24.     SharePointOnlineCredentials creds=new SharePointOnlineCredentials(userName, secureString);  
    25.   
    26.     using(var ctx=new ClientContext(siteURL))  
    27.     {  
    28.         ctx.Credentials=creds;  
    29.         List list=ctx.Web.Lists.GetByTitle(title);  
    30.         list.DeleteObject();  
    31.         ctx.ExecuteQuery();  
    32.         return list == null  
    33.         ? req.CreateResponse(HttpStatusCode.BadRequest, "Error retreiveing the list")  
    34.         : req.CreateResponse(HttpStatusCode.OK, "ListDeleted successfully " + title);  
    35.     }  
    36.     return null;  
    37. }  

Test the function

  1. Click Get function URL and copy the URL.

    Azure Functions

    Azure Functions

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

    Azure Functions

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

    Azure Functions

Result

Thus, in this article, you saw how to create Azure Functions app to delete SharePoint Online list using CSOM.

Up Next
    Ebook Download
    View all
    Learn
    View all