Tuesday, January 12, 2021

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

 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:

  • How to create a simple Azure Functions App using C#

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

How To Send Reminder Emails For Overdue Tasks In SharePoint Using Azure Logic App

 Azure Logic Apps is a fully managed integration platform as a service which provide a way to automate the workflows and business process. You could easily integrate across different services in cloud and on-premise through connectors. In my previous article you saw how to create

  • Create Blank Logic App
  • Create Azure Logic App from template
  • Add a condition in Azure Logic App.
  • Add parameters in Azure Logic App

This is a common requirement in SharePoint where we need to send reminder emails for overdue tasks and we would have achieved this using any of the following approaches:

  • SharePoint designer workflow
  • Scheduling console application or PowerShell script
  • Configuring retention policy or any other approach.

In this blog you will see how to send reminder emails for overdue tasks in SharePoint using Azure Logic App with simple configuration steps. I have created a tasks list in SharePoint Online site as shown below.

Logic App Design

This Logic App is scheduled to run on a daily basis.

Recurrence Trigger

Used to trigger an event at a regular time interval.

SharePoint – Get Items action

Used to get all items from Tasks SharePoint online list with filter condition as Due Date less than current date (DueDate le datetime'@{utcNow()}')

Office 365 Outlook – Send an email action

Sends an email to the users who have not completed the task on due date.

When you select output from previous step “Get Items” such as Assigned To or Task Name, automatically For Each will be included and we don't need to do anything from our side. You could also check the code view for the same.

Save and run the logic app to test it immediately. Emails are sent for overdue tasks as shown below.


Result

Thus in this blog you saw how to send reminder emails for overdue tasks in SharePoint using Azure Logic App and schedule it on daily basis.

How To Add Parameters In Azure Logic App

 Azure Logic Apps is a fully managed integration Platform as a Service which provides a way to automate the workflows and business process. You could easily integrate across different services in cloud and on-premise through connectors. In my previous articles, you saw how to create -

  • blank logic app
  • logic app from template
  • add a condition in logic app

In this blog, you will see how to add parameters which can be reused throughout the logic app. I have already created an app which sends email to my Gmail account (hardcoded the email address) when an item is created in SharePoint Online list.

Add a condition to the logic app

Log in to the Azure Portal.

Click Resource Groups-> Resource (in which you have created the logic app) -> click on the respective logic app.

Click Edit. Click Code view.

Under parameters, add a new parameter.

Under Send_email action, update the To address with the newly added parameter value.

Save the changes.

Result

Thus, in this blog, you saw how to add parameters which can be reused throughout the logic app in Azure.

How To Add A Condition In Azure Logic App

 Azure Logic Apps is a fully managed integration Platform as a service which provide a way to automate the workflows and business process. You could easily integrate across different services in cloud and on-premise through connectors. In my previous article you saw how to create a blank logic app and logic app from template. In this blog you will see how to add a condition to run your logic apps based on specific criteria. I have already created an app which send email to my Gmail account when an item is created in SharePoint Online list.

Add a condition to logic app:

Log in to the Azure Portal.

Click Resource Groups-> Resource (in which you have created the logic app) -> click on the respective logic app.

Click Edit to edit the logic app.

Click “+” sign and then click Add a condition.

You could add a condition in basic mode as shown below.

In order to add “OR” or “AND” condition, click Edit in advanced mode and update the condition as shown below:

@or(contains(triggerBody()?['Title'], 'SharePoint'),contains(triggerBody()?['Title'], 'Microsoft'))

Drag and drop the send email to Gmail account to “If true” as shown below.

Result

Thus in this blog you saw how to add a condition to run your logic apps based on specific criteria in Azure.

How To Create Logic App From Template In Azure

 Azure Logic Apps is a fully managed integration of Platform as a Service which provides a way to automate the workflows and business processes. You could easily integrate across different services in the cloud and on-premise through connectors. In this article, you will see how to create a logic app from the template which will create a new item in SharePoint Online list when a new tweet appears with text as “SharePoint”, #SharePoint, O365, or Azure.

Create logic app from template

  1. Log in to the Azure Portal.
  2. Click New-> Enterprise Integration -> Logic App.

    Azure Logic Apps
  1. Enter Name, Subscription, Resource Group, and Location, as shown in the screenshot. Click Create.

    Azure Logic Apps
  1. It will take a few minutes to complete the deployment.

    Azure Logic Apps
  1. Once the deployment is completed, navigate to the respective resource group and then click Overview. Click MyFirstDemoLogicApp.

    Azure Logic Apps
  1. Select Social from the Category drop-down. Select “Save tweets to a SharePoint list”.

    Azure Logic Apps
  1. Click Use this template.

    Azure Logic Apps
  1. Sign in to create a connection with Twitter and authorize Azure Logic Apps to access your account.

    Azure Logic Apps

    Azure Logic Apps
  1. Sign in to create a connection with SharePoint Online site.

    Azure Logic Apps
  1. Once you are successfully connected, click Continue.

    Azure Logic Apps
  1. Update the values, as shown in the screenshot.

    Azure Logic Apps
  1. Save the app. Click Run to execute the logic app manually. 

    Azure Logic Apps

    Azure Logic Apps
  1. The item is created successfully in SharePoint Online list.

    Azure Logic Apps
  1. You can manage the logic app by navigating to the respective resource group and by clicking Overview.

    Azure Logic Apps

Result

Thus, in this article, you saw how to create a logic app from a template in Azure.

No String Argument Constructor/Factory Method to Deserialize From String Value

  In this short article, we will cover in-depth the   JsonMappingException: no String-argument constructor/factory method to deserialize fro...