Thursday, March 9, 2023

How to Deploy your API as a web app or API app

 Before you can call your custom API from a logic app workflow, deploy your API as a web app or API app to Azure App Service. To make your Swagger document readable by your workflow, set the API definition properties and turn your web app or API app.

1.      In the Azure portal, select your web app or API app.

2.      In the app menu that opens, under API, select API definition. Set the API definition location to the URL for your swagger.json file.

Usually, the URL appears in this format: https://{name}.azurewebsites.net/swagger/docs/v1)


3.      Under API, select CORS. Set the CORS policy for Allowed origins to '*' (allow all).

This setting permits requests from the workflow designer.


Call your custom API from logic app workflows

After you set up the API definition properties and CORS, your custom API's triggers and actions should be available for you to include in your logic app workflow.

·       To view websites that have OpenAPI URLs, you can browse your subscription websites in the workflow designer.

·       To view available actions and inputs by pointing at a Swagger document, use the HTTP + Swagger action.

·       To call any API, including APIs that don't have or expose a Swagger document, you can always create a request with the HTTP action.

 


How to call Call REST endpoints from workflows in Azure Logic Apps

 

  1. Prerequisites
  2. Add an HTTP + Swagger trigger
  3. Add an HTTP + Swagger action
  4. Host Swagger in Azure Storage


Add an HTTP + Swagger trigger

This built-in trigger sends an HTTP request to a URL for a Swagger file that describes a REST API. The trigger then returns a response that contains that file's content.

  1. Sign in to the Azure portal. Open your blank logic app workflow in the designer.

  2. On the designer, in the search box, enter swagger. From the Triggers list, select the HTTP + Swagger trigger.

    Select HTTP + Swagger trigger

  3. In the SWAGGER ENDPOINT URL box, enter the URL for the Swagger file that you want, and select Next.

    Make sure to use or create your own endpoint. As an example only, these steps use the following Cognitive Services Face API Swagger URL located in the West US region and might not work in your specific trigger:

    https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/export?DocumentFormat=Swagger&ApiName=Face%20API%20-%20V1.0

    Screenshot that shows the workflow designer with the "H T T P + Swagger" trigger and the "Swagger Endpoint U R L" property set to a U R L value.

  4. When the designer shows the operations described by the Swagger file, select the operation that you want to use.

    Screenshot that shows the workflow designer with the "H T T P + Swagger" trigger and a list that displays Swagger operations.

  5. Provide the values for the trigger parameters, which vary based on the selected operation, that you want to include in the endpoint call. Set up the recurrence for how often you want the trigger to call the endpoint.

    This example renames the trigger to "HTTP + Swagger trigger: Face - Detect" so that the step has a more descriptive name.

    Screenshot that shows the workflow designer with the "H T T P + Swagger" trigger that displays the "Face - Detect" operation.

  6. To add other available parameters, open the Add new parameter list, and select the parameters that you want.

    For more information about authentication types available for HTTP + Swagger, review Add authentication to outbound calls.

  7. Continue building your workflow with actions that run when the trigger fires.

  8. When you're finished, remember to save your workflow. On the designer toolbar, select Save.

Add an HTTP + Swagger action

This built-in action sends an HTTP request to the URL for the Swagger file that describes a REST API. The action then returns a response that contains that file's content.

  1. Sign in to the Azure portal. Open your logic app workflow in designer.

  2. Under the step where you want to add the HTTP + Swagger action, select New step.

    To add an action between steps, move your pointer over the arrow between steps. Select the plus sign (+) that appears, and then select Add an action.

  3. On the designer, in the search box, enter swagger. From the Actions list, select the HTTP + Swagger action.

    Select HTTP + Swagger action

  4. In the SWAGGER ENDPOINT URL box, enter the URL for the Swagger file that you want, and select Next.

    Make sure to use or create your own endpoint. As an example only, these steps use the following Cognitive Services Face API Swagger URL located in the West US region and might not work in your specific action:

    https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/export?DocumentFormat=Swagger&ApiName=Face%20API%20-%20V1.0

    Enter URL for Swagger endpoint

  5. When the designer shows the operations described by the Swagger file, select the operation that you want to use.

    Operations in Swagger file

  6. Provide the values for the action parameters, which vary based on the selected operation, that you want to include in the endpoint call.

    This example has no parameters, but renames the action to "HTTP + Swagger action: Face - Identify" so that the step has a more descriptive name.

    Operation details

  7. To add other available parameters, open the Add new parameter list, and select the parameters that you want.

    For more information about authentication types available for HTTP + Swagger, review Add authentication to outbound calls.

  8. When you're finished, remember to save your logic app workflow. On the designer toolbar, select Save.

Host Swagger in Azure Storage

You can still reference a Swagger file that's not hosted or that doesn't meet the security and cross-origin requirements. Upload the Swagger file to blob container in an Azure storage account and enable CORS on that storage account. To create, set up, and store Swagger files in Azure Storage, follow these steps:

  1. Create an Azure storage account.

  2. Now enable CORS for the blob. On your storage account's menu, select CORS. On the Blob service tab, specify these values, and then select Save.

    PropertyValue
    Allowed origins*
    Allowed methodsGETHEADPUT
    Allowed headers*
    Exposed headers*
    Max age (in seconds)200

    Although this example uses the Azure portal, you can use a tool such as Azure Storage Explorer, or automatically configure this setting by using this sample PowerShell script.

  3. Create a blob container. On the container's Overview pane, select Change access level. From the Public access level list, select Blob (anonymous read access for blobs only), and select OK.

  4. Upload the Swagger file to the blob container, either through the Azure portal or Azure Storage Explorer.

  5. To reference the file in the blob container, get the HTTPS URL that follows this format, which is case-sensitive, from Azure Storage Explorer:

    https://<storage-account-name>.blob.core.windows.net/<blob-container-name>/<complete-swagger-file-name>?<query-parameters>

Connector reference

This section provides more information about the outputs from an HTTP + Swagger trigger or action. The HTTP + Swagger call returns this information:

Property nameTypeDescription
headersObjectThe headers from the request
bodyObjectThe object with the body content from the request
status codeIntegerThe status code from the request

Status codeDescription
200OK
202Accepted
400Bad request
401Unauthorized
403Forbidden
404Not Found
500Internal server error. Unknown error occurred.

Tuesday, March 7, 2023

How To Call A instant Power Automate Flow From PowerApps

 

1. How to trigger a Power Automate flow from PowerApps

Overview

  1. Create a canvas app and add a button
  2. Create an instant flow that has the trigger as PowerApps button. This flow will send an email to a fixed person. 
  3. Call this instant flow from the button

Implementation

  1. Create an instant flow


     
  2. Give some name to the flow and choose PowerApps as a trigger as shown below:

     
  3. Add the steps required as per your business logic. In this case, I have added an action which is sending an email to one of the users.


     
  4. Save the flow
  5. Create a canvas app and add a button or icon on the screen
  6. Select the button and navigate to the Action tab and you will find the option to add a Power Automate
  7. From the list of available instant flows, click on the recently created flow, i.e. "Send Email"


     
  8. Once the flow gets added, just close the brackets and we are good to go.
  9. Now when you click on a button, a flow will be triggered and it will send an email to the specified user as shown below:

2. How to pass the parameters to Power Automate from PowerApps

Overview

  1. Create a canvas app, add a button, and two text boxes.
  2. Create an instant flow that has the trigger as PowerApps button. This flow will send an email to the user's emails specified in the text boxes.
  3. Call this instant flow from the button

Implementation

  1. Create an instant flow


     
  2. Give some name to the flow and choose PowerApps as a trigger as shown below:


     
  3. Now we want two parameters from PowerApps text boxes, so we will initialize two string variables.
  4. Initialize the first variable with the name varEmail1 and choose the type as a string. Also, rename the action name to "email1". Renaming the action name will create a parameter with that name.



    Once you click on "Ask in PowerApps", it will create a parameter with that name. See the below screenshot:


     
  5. Similarly, initialize the second variable with the name varEmail2 and choose the type as a string. Also, rename the action name to "email2".



    Once you click on "Ask in PowerApps", it will create a parameter with that name. See the below screenshot:


     
  6. Save the flow.
  7. Create a canvas app and add two text boxes and a button on the screen
  8. Select the button and navigate to the Action tab and you will find the option to add a Power Automate
  9. From the list of available instant flows, click on the recently created flow, i.e. "Send Email using Parameters"


     
  10. Once the flow is added, it will ask you to pass the parameters that we created in the power automate.


     
  11. In this case, we will pass the two email addresses from two text boxes as shown below:
    SendEmailusingParameters.Run(TextInput1.Text,TextInput2.Text)
  12. Now when you click on the button, these parameters will be passed to the Power Automate and flow will send email to those email addresses. 

3. How to respond to PowerApps from Power Automate flow

Overview

  1. Create a canvas app, add a button, and two text boxes. We need to multiply the values from these two text boxes using flow.
  2. Create an instant flow that has the trigger as PowerApps button. This flow will calculate the multiplication of these two values and respond to the answer to the PowerApps label.
  3. Call this instant flow from the button

Implementation

  1. As explained in an earlier section, create an instant flow (I have used flow name as "Flow to Respond PowerApp"s) and initialize two Integer variables (varNum1, varNum2). These variables will take values from PowerApps.


     
  2. Add an action called Respond to PowerApps, Use Type of Output as Number


     
  3. Give some name to the output parameter (Say "MyAnswer"). Multiple the two variables as shown below:

    int(mul(variables('varNum1'),variables('varNum2')))


     
  4. Save the flow
  5. Now, as explained in the earlier section, on the button add the recently created flow and pass the values of two text boxes as shown below. This time instead of calling a flow directly, we will use one variable to save the response of a flow. Here I have used a variable named varFlowOutput to save the multiplication of the numbers:


     
  6. Add one label, which will show the output of the flow. Set the text property of the label to varFlowOutput.myAnswer. myAnswer is the output parameter name that we have created in step 3.


     
  7. Now run the flow by passing the numbers. You can see below that the flow ran successfully and the label is showing the answer:

In this way we have learned below topics:

  1. How to call a Power Automate flow from PowerApps
  2. How to pass the parameters to Power Automate from PowerApps
  3. How to respond to PowerApps from Power Automate flow

Wednesday, March 1, 2023

How to call Acction using Javascript in dynamics CRM

 

Step: 1 – Configure Custom Action in Power App Solution

Open Power Apps Solution and Add a Custom Action process with a Input Parameter and Output parameter both string type parameters. Activate the Custom Action.


Step: 2 – Create a JavaScript Web Resource and Put the below code

use the below code in the JavaScript Web resource and Publish it.

JavaScript
//Call Global Custom Action from Javascript using Web API function CallCustomActionFromJavaScript() { //get the current organization url var globalContext = Xrm.Utility.getGlobalContext(); var serverURL = globalContext.getClientUrl(); //query to send the request to the global Action // Global Action Unique Name - this name is Case Sensitive var actionName = "soft_MyCustomAction"; //set the current loggedin userid in to _inputParameter of the var InputParamValue = globalContext.userSettings.userId; //Pass the input parameters to action var data = { "MyInputParam": InputParamValue }; //Create the HttpRequestObject to send WEB API Request var req = new XMLHttpRequest(); req.open("POST", serverURL + "/api/data/v9.2/" + actionName, true); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.onreadystatechange = function () { if (this.readyState == 4 /* complete */) { req.onreadystatechange = null; if (this.status == 200 || this.status == 204) { alert("Action Called Successfully..."); //Get the output parameter of the action (if any) result = JSON.parse(this.response); alert(result.MyOutputParam); } else { var error = JSON.parse(this.response).error; alert("Error in Action: "+error.message); } } }; //Execute request passing the input parameter of the action req.send(window.JSON.stringify(data)); }

How to Deploy your API as a web app or API app

  Before you can call your custom API from a logic app workflow, deploy your API as a web app or API app to Azure App Service. To make your ...