Logic App Run API

Azure Logic App

Azure Logic Apps makes it easy to create and deploy scalable cloud connections and processes. In the Logic App Designer, you may graphically represent and automate your process as a set of phases known as a workflow. You can also add a variety of connectors to your logic app to easily interconnect services and protocols across the cloud and on-premises. A logic app starts with a trigger, such as 'When you have to call up third party API and send result to ADF,' and may then start a variety of actions, conversions, and condition logic once it fires.

Resource Groups Description
Workflow Run Action This section contains a list of workflow run actions.
Workflow Run This function allows you to list and cancel workflow runs.
Workflow Triggers Histories The history of workflow triggers is covered.
Workflow Triggers This function allows you to list and perform workflow triggers.
Workflow Versions Lists workflow versions.
Workflows Create and manage workflows using this application.
public class LogicAppRun {
    #region API variables
    static HttpClient client = new HttpClient();
    static string baseUrl = "https://management.azure.com/subscriptions/";
    static string subscriptionKey = "XXXXXXXXX";
    static string resourceGroupName = "XXXX";
    #endregion
    #region access token variables
    static string tenantId = "XXXXXXX";
    static string clienID = "XXXXXXXXX";
    static string clientSecret = "XXXXXXXX";
    #endregion
    #region Trigger WorkFlow, Get WorkFlow Run Name, Get Run Status
    /// <summary>
    /// Run the trigger or workflow
    /// {subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Logic/workflows/{workflowName}/triggers/{triggerName}/run?api-version=2016-06-01
    /// </summary>
    /// <returns></returns>
    private static async Task < bool > RunWorkflowTrigger(string workFlowName, string triggerName) {
        string remaingPath = ReplacePlaceHolder("{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Logic/workflows/{workflowName}/triggers/{triggerName}/run?api-version=2016-06-01", string.Empty, workFlowName, String.Empty, triggerName);
        string path = LogicAppRun.baseUrl + remaingPath;
        HttpResponseMessage response = await client.PostAsync(path, null);
        return response.IsSuccessStatusCode;
    }
    /// <summary>
    /// Get Last run or workflow name
    /// {subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Logic/workflows/{workflowName}/runs?api-version=2016-06-01
    /// </summary>
    /// <returns></returns>
    private static async Task < string > GetWorkFlowRunName(string workFlowName) {
        string remaingPath = ReplacePlaceHolder("{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Logic/workflows/{workflowName}/runs?api-version=2016-06-01", string.Empty, workFlowName, String.Empty, string.Empty);
        string path = LogicAppRun.baseUrl + remaingPath;
        string RunName = String.Empty;
        HttpResponseMessage response = await client.GetAsync(path);
        if (response.IsSuccessStatusCode) {
            var listOfRun = await response.Content.ReadAsStringAsync();
            var data = JsonConvert.DeserializeObject < AzureLogicApplication.Root > (listOfRun).value;
            foreach(var item in data) {
                RunName = item.name;
                break;
            }
        }
        return RunName;
    }
    /// <summary>
    /// Check status of last run whehter it run sucessfully or not
    /// {subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Logic/workflows/{workflowName}/runs/{runName}?api-version=2016-06-01
    /// </summary>
    /// <returns></returns>
    private static async Task < string > GetWorkFlowRunStatus(string workFlowName, string runName) {
        string remaingPath = ReplacePlaceHolder("{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Logic/workflows/{workflowName}/runs/{runName}?api-version=2016-06-01", runName, workFlowName, String.Empty, string.Empty);
        string path = LogicAppRun.baseUrl + remaingPath;
        string StatusOfTrigger = String.Empty;
        HttpResponseMessage response = await client.GetAsync(path);
        if (response.IsSuccessStatusCode) {
            var listOfRun = await response.Content.ReadAsStringAsync();
            var data = JsonConvert.DeserializeObject < AzureLogicApplication.Value > (listOfRun);
            if (data != null) {
                StatusOfTrigger = data.properties.status;
            }
        }
        return StatusOfTrigger;
    }
    #endregion
    static void Main() {
        var token = AccessTokenHelper.GetLogicManagementClient(subscriptionKey, tenantId, clienID, clientSecret).GetAwaiter().GetResult();
        //Passing logic app name and token.
        RunWorkFlowAsync("logicappname", token).GetAwaiter().GetResult();
    }
    #region API common call
    /// <summary>
    /// Gets a list of workflow run actions and Gets a workflow run action.
    /// </summary>
    /// <returns></returns>
    private static async Task RunWorkFlowAsync(string workFlowName, string token) {
        // Update port # in the following line.
        token = "Bearer " + token;
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Add("Authorization", token);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        try {
            var statusOfWorkflowTrigger = await RunWorkflowTrigger(workFlowName, "http");
            if (statusOfWorkflowTrigger) {
                var getRunName = await GetWorkFlowRunName(workFlowName);
                if (getRunName != String.Empty) {
                    var getActionName = await GetWorkFlowRunActionName(getRunName, workFlowName);
                    //var getWorkFlowRunStatus = await GetWorkFlowRunStatus(workFlowName, RunName);
                    if (getActionName != String.Empty) {
                        var getActionStatus = await GetWorkFlowRunActionStatus(getRunName, workFlowName, getActionName);
                    }
                }
            } else Console.WriteLine("No Run executed");
        } catch (Exception e) {
            Console.WriteLine(e.Message);
        }
        Console.ReadLine();
    }
    #endregion
    #region Run ActionName, Get Action Status
    /// <summary>
    /// Get All Action names inside the workflow
    /// "{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Logic/workflows/{workflowName}/runs/{runName}/actions?api-version=2016-06-01";
    /// </summary>
    /// <param name="getWorkFlowRunName"></param>
    /// <returns></returns>
    private static async Task < string > GetWorkFlowRunActionName(string runName, string workflowName) {
        string remaingPath = ReplacePlaceHolder("{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Logic/workflows/{workflowName}/runs/{runName}/actions?api-version=2016-06-01", runName, workflowName, string.Empty, string.Empty);
        string path = LogicAppRun.baseUrl + remaingPath;
        string RunName = String.Empty;
        HttpResponseMessage response = await client.GetAsync(path);
        if (response.IsSuccessStatusCode) {
            var listOfRun = await response.Content.ReadAsStringAsync();
            var data = JsonConvert.DeserializeObject < AzureLogicApplication.Root > (listOfRun).value;
            foreach(var item in data) {
                RunName = item.name;
                break;
            }
        }
        return RunName;
    }
    /// <summary>
    /// Check status of Action on given action names from above Get All Action
    /// {subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Logic/workflows/{workflowName}/runs/{runName}/actions/{actionName}?api-version=2016-06-01
    /// </summary>
    /// <returns></returns>
    private static async Task < string > GetWorkFlowRunActionStatus(string runName, string workFlowName, string actionName) {
        string remaingPath = ReplacePlaceHolder("{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Logic/workflows/{workflowName}/runs/{runName}/actions/{actionName}?api-version=2016-06-01", runName, workFlowName, actionName, string.Empty);
        string path = LogicAppRun.baseUrl + remaingPath;
        string StatusOfTrigger = String.Empty;
        HttpResponseMessage response = await client.GetAsync(path);
        if (response.IsSuccessStatusCode) {
            var listOfRun = await response.Content.ReadAsStringAsync();
            var data = JsonConvert.DeserializeObject < AzureLogicApplication.Value > (listOfRun);
            StatusOfTrigger = data.properties.status;
        }
        return StatusOfTrigger;
    }
    #endregion
    #region ReplacePlaceholder from URL
    private static List < KeyValue > GetPlaceHolderList(string runName, string workflowName, string actionName, string triggerName) {
        List < KeyValue > tokens = new List < KeyValue > {
            new KeyValue() {
                key = "{subscriptionId}", value = subscriptionKey
            },
            new KeyValue() {
                key = "{resourceGroupName}", value = resourceGroupName
            },
            new KeyValue() {
                key = "{workflowName}", value = workflowName
            },
            new KeyValue() {
                key = "{actionName}", value = actionName
            },
            new KeyValue() {
                key = "{triggerName}", value = triggerName
            },
            new KeyValue() {
                key = "{runName}", value = runName
            }
        };
        return tokens;
    }
    private static string ReplacePlaceHolder(string StringValue, string runName, string workflowName, string actionName, string triggerName) {
        List < KeyValue > keyValues = GetPlaceHolderList(runName, workflowName, actionName, triggerName);
        foreach(var keyValue in keyValues) {
            StringValue = StringValue.Replace(keyValue.key, keyValue.value);
        }
        return StringValue;
    }
    #endregion
}

Below is the Github URL.

Next Recommended Reading How To Add Parameters In Azure Logic App