• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Tachytelic.net

  • Get in Touch
  • About Me

Power Platform

How to Print from PowerApps to on-premise printers

April 7, 2020 by Paulie Leave a Comment

PowerApps is a marvellous product, but printing support is quite weak. I am currently in the process of developing a PowerApp and printing support to on-premise printers is a must. YouTube video that demos the functionality:

PrintNode works really well with Microsoft Power Automate and Power Apps. After only a few minutes of setup, I was able to print PDF files directly from PowerApps to my on-premise printers. In my case the PDF files were stored in a SharePoint list.

The printing is performed with a HTTP interaction between Power Automate and PrintNode, so these instructions would work equally well from any flow, but my use case was PowerApps.

The high level steps are as follows:

  • Register on PrintNode and setup the client.
  • Select which Printers you want to print to and gather their unique ids.
  • Create a flow which uses the HTTP Connector to send print jobs to Printnode.

Setup PrintNode Client

After you have registered on printnode.com, install the client on a machine that has local access to the printers you want to print. Then take a note of the printers that you want to print to. Mine look like this:

From the screenshot above, you can see I have two printers enabled with the unique IDs of 69363024 and 68663025.

Setup Printer Definitions in a SharePoint list

A simple SharePoint list with four fields works well to drive a printer selection drop down in PowerApps. My list looks like this:

Image showing list of printers in SharePoint

And the resulting drop down looks like this:

Image of printer selection screen from PowerApps

Or you could set up the printer definitions directly in your PowerApp by using the Table Function. This code would produce the same result as the SharePoint list:

Table(
{printNodeID:"69363025", Description:"HP Laserjet M506", Location: "Pauls Office", Image: "https://www.tachytelic.net/wp-content/uploads/HP-LaserJet-Enterprise-M506-Printer.jpg" },
{printNodeID:"69363024", Description:"HP PageWide Pro 477DW", Location: "Merrians Office", Image: "https://www.tachytelic.net/wp-content/uploads/HP-PageWide-Pro-477dn-web.jpg" }
)

Create a flow to receive print request from PowerApps and submit to PrintNode

The flow is simple to create. Here is a high level image of my flow:

An Image showing the steps of a Flow in Microsoft Power Automate used to print from PowerApps to On-Premise printers.

Define the required variables for the Flow from PowerApps

The flow starts with the creation of the three variables that are needed to complete the flow. The three variables are

  • printNodeID
  • pdfPath
  • documentName

This step isn’t strictly required, you can simply use “Ask in PowerApps” to fill the required values further down the flow. I prefer to do it this way as it makes the flow easier to comprehend when you are calling it from PowerApps. Each variable is initialized like this:

Image of a flow variable being initialized to be a parameter for a PowerApp

Retrieve the PDF that you want to print

In my case, the document I want to print is already in PDF format, if your’s is not, then you will need to add some steps to your flow to get it into PDF format.

I am using the flow action “Get file content using path” to get the PDF file from a SharePoint list.

Compose your JSON and submit to PrintNode

The PrintNode API is extensive and I have only scratched the surface. The code I am using simply submits a PDF for printing to a particular printer. I am using the “Compose” action to create the required JSON.

{
  "printerId": "@{variables('printNodeID')}",
  "title": "@{variables('documentName')}",
  "contentType": "pdf_base64",
  "content": "@{base64(body('Get_file_content_using_path'))}",
  "source": "PowerApps"
}

Note that the PDF file needs to be Base64 encoded, otherwise the submission to PrintNode will fail.

Image of the compose action in Power Automate being used to generate JSON for submission to PrintNode

The final step is to use the HTTP action to submit the job to PrintNode:

This step is simple. You need the URI, which is: https://api.printnode.com/printjobs

The Authorization header. I used a concat expression for the value of:

concat('Basic ', base64('apiKeyFromPrintNode:'))  

and the body of the HTTP request is the output of the compose step.

Connect your new PrintNode flow to your PowerApp

The final step is to connect your PowerApp to your newly created PrintNode flow. I have connected mine to a button control which initiates the flow with the following code:

printViaPrintNode.Run(dropDownSelectPrinter.Selected.printerID, pdfURL, pdfName)

Obviously I named the flow “printViaPrintNode” and the values being passed in are associated to the variables assigned in the flow.

Image showing expression used to execute a flow from PowerApps to print a PDF to an on-premise printer.

This is the Print screen withing my PowerApp. PDF Preview on the right hand side with the printer options on the left and an image of the printer that is going to process the job.

Image of a screen from PowerApps which is used to preview a PDF document and then print it to an on-premise printer.

It’s also interesting to note that PrintNode do have a “Raw” printing option which can be used to drive Label printers, but I can think of some other uses for that option to, which I am going to play with.

So overall, although PowerApps does not have great native printing support, PrintNode goes a long way to solving the problem!

Filed Under: Power Platform Tagged With: Power Automate, PowerApps

How to trigger a Power Automate Flow with a HTTP Request

March 12, 2020 by Paulie Leave a Comment

Being able to trigger a flow in Power Automate with a simple HTTP request opens the door to so many possibilities. I love it! With some imagination you can integrate anything with Power Automate.

If you want an in-depth explanation of how to call Flow via HTTP take a look at this blog post on the Power Automate blog.

This post provide examples of some of the different ways that the trigger “When a HTTP request is received” can be executed:

  • PowerShell
  • curl on Windows
  • curl on Linux or Unix
  • vbscript
  • vba
  • jquery

The same flow will be executed with different tools or languages and each of them will submit this JSON:

{
	"MessageSubject": "Testing HTTP",
	"MessageBody": "Flow execution has been triggered"
}

The flow only has two steps, it receives the JSON payload and then sends me a message on Teams from the Flow bot:

Image of Flow in Microsoft Power Automate which will be triggered by the event "When a HTTP request is received"
Image of Flow Bot sending a Teams message using a flow Microsoft Power Automate

Invoke a Flow with PowerShell

It’s super easy to invoke a flow with PowerShell. Example:

$flowURI = "https://prod-118.westeurope.logic.azure.com:443/workflows/..."
$messageSubject = "Testing HTTP"
$messageBody = "Execution test from Powershell"
$params = @{"messageSubject"="$messageSubject";"messageBody"="$messageBody"}
Invoke-WebRequest -Uri $flowURI -Method POST -ContentType "application/json" -Body ($params|ConvertTo-Json)

Invoke a HTTP Flow Trigger with curl on Windows Command Line

Because of the way Windows command line interprets double quotes, they need to be escaped. So if you want to include the JSON data on the command line it gets ugly:

curl -H "Content-Type: application/json" -d "{\"messageSubject\": \"Test\",\"messageBody\": \"Executing Flow from curl in Windows Command Line\"}" "https://prod-118.westeurope.logic.azure.com:443/workflows/..."

It’s much easier to work with if you put the contents of the JSON in a file and use the following syntax:

curl -H "Content-Type: application/json" -d @c:\temp\data.json "https://prod-118.westeurope.logic.azure.com:443/workflows/..."

Invoke a HTTP Flow Trigger with curl on Linux or Unix

Things are a bit easier in Linux because there is no need to escape double quotes. So you can simply execute a flow like this:

curl -H "Content-Type: application/json" -d '{ "MessageSubject": "Testing HTTP", "MessageBody": "Flow execution has been triggered from Linux" }' 'https://prod-118.westeurope.logic.azure.com:443/workflows/...'

Invoke a HTTP Flow using VBScript

You can use this code to invoke a HTTP flow in a VBScript:

Option Explicit

Dim FlowURI, JSON, objHTTP, httpCode
FlowURI = "https://prod-118.westeurope.logic.azure.com:443/workflows/..."
JSON = Quote("{ `MessageSubject`: `Testing HTTP`, `MessageBody`: `Flow executed from VBScript` }")

set objHttp = wscript.Createobject("Msxml2.ServerXMLHTTP")
objHTTP.Open "POST",FlowURI,false
objHTTP.setRequestHeader "Content-Type", "application/json; charset=UTF-8"
objHTTP.setRequestHeader "CharSet", "charset=UTF-8"
objHTTP.setRequestHeader "Accept", "application/json"
objHTTP.setRequestHeader "Content-Length", Len(JSON)
objHTTP.send JSON
httpCode = objHTTP.Status
Set objHTTP = nothing

Function Quote(stringToQuote)
	'Small Function to replace backticks with Double Quotes
	Quote=Replace(stringToQuote, "`", chr(34))
End Function

Invoke a HTTP flow using VBA

Almost the same as the VBScript above, you can easily execute a flow using VBA from any of the Microsoft Office Suite:

Option Explicit
Sub ExecuteFlow()

    Dim FlowURI As String, JSON As String, objHTTP As Object, httpCode As String
    FlowURI = "https://prod-118.westeurope.logic.azure.com:443/workflows/..."
    JSON = Quote("{ `MessageSubject`: `Testing HTTP`, `MessageBody`: `Flow executed from VBA` }")
    
    Set objHTTP = CreateObject("Msxml2.ServerXMLHTTP")
    objHTTP.Open "POST", FlowURI, False
    objHTTP.setRequestHeader "Content-Type", "application/json; charset=UTF-8"
    objHTTP.setRequestHeader "CharSet", "charset=UTF-8"
    objHTTP.setRequestHeader "Accept", "application/json"
    objHTTP.setRequestHeader "Content-Length", Len(JSON)
    objHTTP.send JSON
    httpCode = objHTTP.Status
    Set objHTTP = Nothing

End Sub

Function Quote(stringToQuote)
    'Small Function to replace backticks with Double Quotes
    Quote = Replace(stringToQuote, "`", Chr(34))
End Function

Invoke a HTTP flow with jQuery

This is an example of how to execute the same flow with jQuery. I’ve also included the required HTML. Full source code on this page, send me a message!

<!DOCTYPE html>
<head>
<title>Execute Flow</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
flowURI = "https://prod-118.westeurope.logic.azure.com:443/workflows/..."
$(document).ready(function(){
    $("button").click(function(){
        flowData = {
           MessageSubject: "Testing HTTP",
           MessageBody: $("#teamsMessage").val()
        }
        $.ajax(
            {
                url: flowURI,
                data: JSON.stringify(flowData),
                processData: false,
                contentType: "application/json",
                dataType: "json",
                type: 'POST',
                complete: function(xhr, textStatus) 
                {
                    if (xhr.status == '202')
                    {
                        $("#flowStatus").append("Flow Executed");  
                        console.log(xhr.status);                                                  
                    } else 
                    {
                        $("#flowStatus").append("Flow Execution Failed");                                                                                
                        console.log(xhr.status);
                    }  
                }
            });
    });
});
</script>
</head>
<body>
    <p>
        <label>Message</label>
        <input type = "text" id ="teamsMessage" value="Send a message on teams to Paulie" />
    </p>
    <p>
        <button>Invoke Flow</button>
    </p>
<p id="flowStatus"></p>
</body>
</html>

I will keep adding more examples as I create them, but if you have one, feel free to share and I will add it to the post.

Now secure your HTTP Request Trigger with the instructions here.

Filed Under: Power Platform Tagged With: Power Automate, Powershell, VBA, VBScript

Change column headings for an Entity List in Power Apps Portal

January 31, 2020 by Paulie 1 Comment

When you build an entity list for display in a Power Portal the column headings may not suit your requirements. For example:

The column headings are too long for the data and will waste valuable screen estate on a mobile device. I renamed the columns to look like this:

This is an improvement, but the column widths still aren’t right. The Invoice Number column is too wide and the customer name and reference could do with being wider to prevent wrapping. Fortunately you can make both changes in the same place.

Change column display names and widths for an Entity List on a Power Apps Portal

  1. Go into Portals Management App.
  2. Navigate Entity Lists.
  3. Choose the Entity List that you want to modify.
  4. Click on the Options tab.
  5. Enable “Advanced Settings” by clicking the tick box at the top grid configuration:
    Image showing how to enabled advanced options for a PowerApps Portal Entity List
  6. Scroll Down to the Section labelled “Override Column Attributes”
  7. Add an entry for each column that you want to rename:
    Image showing how to change the display names of Entity Lists in a PowerApps Portal
  8. Put in a value for the width. You can choose between percentage or pixels widths.
  9. Scroll to the bottom of the grid configuration and choose between Percent or Pixels in the Grid Column Width Style drop-down.

I was quite pleased with how much better the Entity list looked after these small modifications:

Filed Under: Power Platform

Power Automate: Get a single record from CDS List Records action

January 30, 2020 by Paulie 2 Comments

You can easily retrieve the unique identifier of a record in CDS within flow using the List Records action. But this approach will create an “Apply to each” loop, even if there is only one possible result.

Image showing unwanted "Apply to Each" loop when querying a single record from Common Data Service.

I needed to get the accountid from the Account entity to create a new record in a related table. My list records action looks like this:

Image showing "List Records" action in Microsoft Power Automate.

Note that “Top Count” is set to 1.

The next step of the flow where the new record is created. You can stop the Apply to Each loop being created by using the “first” function. Example code:

first(body('List_records')?['value'])?['accountid']
Image showing how to create a new record in Common Data Service using only a single result from a "List Records" action in Microsoft Power Automate.
Image of Microsoft Flow Expression and the use of the "First" function which can give you the first result from a record set.

Then you can continue to your next action without having to deal with a for each loop. It makes the flow neater and easier to comprehend.

Filed Under: Power Platform Tagged With: Power Automate

Power Apps: Recover accidentally deleted Portal Management App

January 24, 2020 by Paulie Leave a Comment

If you’ve accidentally deleted your Power Apps Portal Management Application you can get it back pretty easily as it is just a model driven app. Here is how to do it:

  1. Login to the Power Apps Portal.
  2. Click on New App and Select “Model-driven”:
  3. Enter the details of the app and ensure you select “Use existing solution to create the App”
  4. Click Next
  5. Select “Dynamics 365 Portals – Portal Base” as the solution and “Dynamics 365 Portals” for the site map:
  6. Click Done
  7. Click Publish on the next screen.

Filed Under: Power Platform

  • « Go to Previous Page
  • Go to page 1
  • Interim pages omitted …
  • Go to page 4
  • Go to page 5
  • Go to page 6

Primary Sidebar

Link to my LinkedIn Profile

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 107 other subscribers.

Go to mobile version