Power Automate

Create a PDF file from a SharePoint list item using the no-code Microsoft Power Automate Platform.

Introduction

In March 2018, Microsoft announced the Microsoft Power Platform. According to Microsoft, the world of business applications is changing. It is a so-called no / low-code platform that anyone with knowledge of Excel and PowerPoint can use to create business applications.

But is this really the whole story? Is the Microsoft Power Platform always adequate for every business solution or are there situations in which it is best to combine the Power Platform with a low-code solution?

This video shows the result of the conversion we are going to create, so lets start some PowerAutomate!

The Process

In order to convert a SharePoint list item into a PDF document we first need to convert it into a HTML document. By using a HTML template we convert the SharePoint list-item into a HTML document that is then stored on OneDrive. This step is needed because we want to use an existing PowerAutomate action to convert the HTML document to PDF. Once we created the PDF we can store it back into a SharePoint document library.

Step 1

First create a SharePoint custom list called ‘ConvertToPDF’. Add two ‘single-line of text’ columns: ‘ExampleColumnOne’ and ‘ExampleColumnTwo’.

Step 2

Next create a text file called ‘template.html’ with the following content and upload it to the ‘Documents’ library in SharePoint.

Content of the HTML template.

 1<html>
 2    <head>
 3        <meta name="generator" content="Microsoft Flow">
 4        <title>Convert SharePoint List items to PDF files</title>
 5    </head>
 6    <body>
 7        <p>{ExampleColumnOne}</p>
 8        <p>{ExampleColumnTwo}</p>
 9    </body>
10</html>

open raw file

Please notice that the template contains two placeholders {ExampleColumnOne} and {ExampleColumnTwo}. Step 5 will replace these placeholders with real data.

Step 3

Open PowerAutomate in your Office365 subscription and create a new Flow, select ‘Instant-from blank’.

shadow shadow

Step 4

Configure the steps as listed below (of course your site address will be different)

  1. ‘For a selected item’: This is the starting point of the flow. It specifies from which SharePoint list the user is able to start the conversion.
  2. ‘Get Item’: gets the listitem data the user selected for conversion.
  3. ‘Get the current time’: We will use that for the filename of the PDF.
  4. ‘Get the HTML template’ so that we can convert the list-item into a HTML document.

shadow

Step 5

This is the most tricky step, since we need to replace the contents of the HTML template with data from the list-item. For this we use a compose step and the ‘replace()’ function.

So first add two compose actions:

And type in the replace() function. The first argument ‘text’ will be the HTML template.

So switch back from ‘Expression’ to ‘Dynamic content’ and select the ‘File Content’ of the HTML template.

This will give you the following:

replace(outputs(‘HTML_Template’)?[‘body’])

Place a comma just before the closing ‘)’ like this

and add type: {ExampleColumnOne},

next select ExampleColumnOne from the row marked below

The final first compose step should look like this:

replace(outputs(‘HTML_Template’)?[‘body’], ‘{ExampleColumnOne}’, outputs(‘Get_item’)?[‘body/ExampleColumnOne’])

For the second compose step will will not use the template as the ‘text’ but the output of the first replace. So the second compose step should look like this:

replace(outputs(‘Compose_-_Replace_ExampleColumnOne’), ‘{ExampleColumnTwo}',outputs(‘Get_item’)?[‘body/ExampleColumnTwo’])

Step 6

Lucky for us the remaining step is quite easy. It consists of the following:

  1. ‘Compose-filename’: construct a filename of the PDF file by using the list-item Title and the current-time created in step 3.
  2. ‘Create file-HTML temp’: create a temporary file on OneDrive (first field is the ‘Compose-filename’ and the second field is the ‘Compose - Replace ExampleColumnTwo’)
  3. ‘Convert file-to PDF’: convert the temporary file into a PDF document
  4. ‘Create file-Enquete PDF’: copy back the PDF into the specified SharePoint document library

Conclusion

In this demo, I have shown you how to implement a conversion of a SharePoint list-item to a PDF document using not a single line of code.

I can imagine that this can be used to generate PDF documents that are then sent by email. Although making this flow is doable, it does show its limitations. Because what if we now want to not only use two- but twenty columns? In that case we will also need 20 compose steps. You understand that in that case this solution is difficult to expand. This is often the problem with no-code solutions. In my next blog post I will show you how to overcome this problem by using Azure functions. Click here to read all about it.