2017-12-04

In the previous post, we saw how to get the instances of the flow attached to a list. Noticed that we can  call a flow for a list item programmatically using the rest api as mentioned below.

Setup the Flow to “Post a message to Teams for the selected item”:  Get the Flow details:Use the CSOM / REST api to get the attached Flow instance details like parameters etc as mentioned in the previous post.

 

"triggers": {"manual": {"type": "Request","kind": "Http","inputs": {"schema": {"type": "object","properties": {"ID": {"type": "integer"},"itemURL": {"type": "string"},"fileName": {"type": "string"}}}}}}&nbsp;&nbsp;Get the call back url for a flow using the "SyncFlowCallbackUrl":List document = context.Web.GetList("Lists/flowtest");context.Load(document);context.ExecuteQuery();var flowCallBackUrl = document.SyncFlowCallbackUrl("<flow instance guid>");context.Load(flowCallBackUrl);context.ExecuteQuery();&nbsp;The above code returns a JSON data containing the url with the unique signature which can be used to trigger the flow.{"response": {"value": "https://prod-31.westeurope.logic.azure.com:443/workflows/<guid>/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=pyVm1NcbtX_4-oE8gXO9jSaluniquerMpOOY","method": "POST","basePath": "https://prod-31.westeurope.logic.azure.com/workflows/<guid>/triggers/manual/paths/invoke","queries": {"api-version": "2016-06-01","sp": "/triggers/manual/run","sv": "1.0","sig": "pyVm1NcbtX_4-oE8gXO9jSaluniquerMpOOY"}},"httpStatusCode": "OK"}

Call the Flow url with the right parameters:

JToken data = JToken.Parse(flowCallBackUrl.SynchronizationData);
string callBackUrl = (data["response"]["value"]).ToString();
Uri uri = new Uri(callBackUrl);
RestClient client = new RestClient(uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port);
RestRequest request = new RestRequest(uri.PathAndQuery, Method.POST);request.AddJsonBody(new { ID = "1", fileName = "test", itemURL = "https://<tenant>.sharepoint.com/_layouts/15/listform.aspx?PageType=4&ListId=%7B68B00455-AB06-47A9-9428-27D718C71803%7D&ID=1&ContentTypeId=0x01004E534F102E621041A23CA93C7BD7A9F4" });
var response = client.Execute(request);

Here we have used the RestSharp api’s to call the Flow url’s by passing the list Item Id, file / Item name and the Url as a JSON body and posts the message in the Team’s conversation.

(Visited 237 times, 1 visits today)

About the author 

Balamurugan Kailasam

Download 6 Free Ebooks (No email needed)

There are Beginner Ebooks on Power BI, Power Automate and Teams. 2 Microsoft Teams Governance Ebooks and a very popular Power Users Guide to SharePoint!