Tuesday, January 12, 2021

How To Call Azure Function In Logic Apps

 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 your 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. Refer to my previous articles below:

  • How to create a simple Azure Function App using C#
  • How to create Azure Function app to delete SharePoint Online list using CSOM

 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. Refer to my previous articles mentioned below:

  • Create Blank Logic App
  • Create Azure Logic App from template
  • Add a condition to Azure Logic App.
  • Add parameters to Azure Logic App
  • How to send reminder emails for overdue tasks in SharePoint using Azure Logic App

In this article, you will see how to call Azure Functions in Logic Apps. I have created a custom list named “List Info” with “Title” field. Title field will contain the list name which will be deleted by the Azure Functions. I have already created an Azure function to delete SharePoint list. When a new item is created in List Info, Logic App will get triggered and execute the Azure function which will get the list name from the “List Info” list Title field and delete the respective list.

Azure Function Code

  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 name = req.GetQueryNameValuePairs()  
  15.         .FirstOrDefault(q => string.Compare(q.Key, "name"true) == 0)  
  16.         .Value;  
  17.   
  18.     // Get request body  
  19.     dynamic data = await req.Content.ReadAsAsync<object>();  
  20.   
  21.     // Set name to query string or body data  
  22.     name = name ?? data?.name;  
  23.   
  24.     System.Security.SecureString secureString=new System.Security.SecureString();  
  25.     foreach(char ch in password)  
  26.     {  
  27.         secureString.AppendChar(ch);          
  28.     }  
  29.   
  30.     SharePointOnlineCredentials creds=new SharePointOnlineCredentials(userName, secureString);  
  31.   
  32.     using(var ctx=new ClientContext(siteURL))  
  33.     {  
  34.         ctx.Credentials=creds;  
  35.         List list=ctx.Web.Lists.GetByTitle(name);  
  36.         list.DeleteObject();  
  37.         ctx.ExecuteQuery();  
  38.         return list == null  
  39.         ? req.CreateResponse(HttpStatusCode.BadRequest, "Error retreiveing the list")  
  40.         : req.CreateResponse(HttpStatusCode.OK, "ListDeleted successfully " + name);  
  41.     }  
  42.     return null;  
  43. }  

Logic App design

Azure Functions

When an item is created

Used to trigger an event at regular time interval whenever a new item is created in the SharePoint Online list.

Add Azure Function

Once the above trigger is configured, click "Next Step" and then click "Add an action".

Azure Functions

Click "Azure Functions".

Azure Functions

Select the Azure Functions which was created to delete the SharePoint Online List.

Azure Functions

Azure Functions

Enter the Request body as shown below.

Azure Functions

Save and run the logic app to test it immediately.

The list names mentioned in the newly created items in “List info” will be deleted. Example: Documents, Configuration List & Test List A will be deleted from the site.

Azure Functions
Result

In this article, you saw how to call Azure Functions in Logic Apps.

No comments:

Post a Comment

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