cancel
Showing results for 
Search instead for 
Did you mean: 

Guide: Working with SharePoint Listitem Attachments, JSON Arrays, HTML Fragments + more

We've been helping lots of folk recently to build Flow's which extract SharePoint list item attachments and perform some form of processing, typically to convert to a single PDF document and share with a third party, or add to a records repository.

The following tutorial is built around a very simple scenario but should serve as a good guide for:

  • How to extract ‘X’ number of SharePoint list item attachments and process
  • How to use Flow variables to iteratively build a JSON array
  • How to merge 'X' number of documents (any format) into a single PDF document
  • How to dynamically create an HTML fragment and insert into a PDF document

Scenario:

A SharePoint list is used to store and track orders, containing all relevant order data and document attachments. When an order is completed, a summary sheet of the order should be compiled and added to a PDF document formed from the document attachments associated with the order, finally the completed PDF document should be sent to the customer via email.

Flow Creation Steps:

1 - Setup a SharePoint list to store order information, an example configuration is detailed below:1.png

2 - Create a new Flow from a blank template2.png

3 - Add the ‘When an item is created or modified’ SharePoint trigger and associate to the list created in step #13.png

4 - We need to create a few variables which we will use to store data used later within the flow. Firstly, to store the HTML summary of the order and secondly, to store an array of the attachments which will be merged into a PDF document.

Create a new variable using the ‘Initialize variable’ action, applying the following configuration:4.png

Note: I recommend re-naming the actions post creation to something meaningful… helps Flow authoring as the Flow becomes more complex.5.png

5 - Create another new variable using the ‘Initialize variable’ action, applying the following configuration:6.png

6 - Add a ‘Condition’ action, applying logic to determine whether the Flow should continue to execute. I’ve set the condition to check whether the ‘Order Status’ value is equal to ‘Paid’ for the current SharePoint item.7.png

7 - Checkpoint: Your new Flow should look similar to the following:8.png

8 - The next objective is to obtain all of the attachments attached to the current SharePoint list item and merge them into a single PDF document.

Add a ‘Get attachments’ SharePoint action applying the following configuration:

  • Site Address: Set as per the ‘Site Address’ value set in #3
  • List Name: Set as per the ‘List Name’ value set in step #3
  • ID: Insert the ‘ID’ property from the ‘When an item is created or modified’ action result9.png

    Note: If you cannot see the ID field, click ‘See more'

    10.png

9 - Add an ‘Apply to each’ control action, applying the following configuration:

 

  • Select an output from previous steps: Insert the ‘Body’ property from the ‘Apply to each’ action result.11.png

 

10 - Inside the ‘Apply to each’ loop, add a ‘Get attachment content’ action, applying the following configuration:

  • Site Address: Set as per the ‘Site Address’ value set in #3
  • List Name: Set as per the ‘List Name’ value set in step #3
  • Id: Insert the ‘ID’ property from the ‘When an item is created or modified’ trigger.12.png

 

  • File Identifier: Insert the ‘Id’ property from the ‘Get attachments’ action result13.png

11 - Checkpoint: Your new Flow should look similar to the following:15.png

12 - The next step is append the attachment information to the previously created ‘FilesForMerge’ array property which will later be used to send the files for PDF conversion and merging.

Add an ‘Append to array variable’ action, applying the following configuration:

  • Name: Select the ‘FilesForMerge’ variable
  • Value: Enter the following JSON:
{
  "fileName": "@{items('Apply_to_each')['DisplayName']}",
  "fileContent": "@{body('Get_attachment_content')['$content']}"
}

14.png

Note: If you have changed the default names of the actions, you will need to amend the expressions accordingly.

13 - Underneath the ‘Apply to each’ action, add a ‘Merge document array to PDF’ action, applying the following configuration:

Detailed guidance: Merge document array to PDF

  • PDF Filename: Enter ‘Order confirmation.pdf
  • Click the ‘Switch to input array icon16.png

 

  •  Documents: Insert the ‘FilesForMerge’ variable.17.png

14 - The next step is to compile the HTML summary which should be inserted into the merged PDF document.

Add a ‘Set variable’ action, and configure as follows:

  • Name: Select the ‘HtmlSummary’ variable
  • Value: Enter the following sample HTML fragment or please create your own as required
<div>
      <img src="https://www.encodian.com/assets/img/Encodian_Logo.png">
</div>
<div>
      <ul>
            <li><b>Order Number:</b> @{triggerBody()?['Title']}</li>
            <li><b>Customer Name:</b> @{triggerBody()?['Customer_x0020_Name']}</li>
            <li><b>Customer Address:</b> @{triggerBody()?['Customer_x0020_Address']}</li>
            <li><b>Purchase Order:</b> @{triggerBody()?['Purchase_x0020_Order']}</li>
            <li><b>Order Details:</b> @{triggerBody()?['Order_x0020_Details']}</li>
            <li><b>Order Placed:</b> @{triggerBody()?['Created']}</li>
            <li><b>Order Paid:</b> @{triggerBody()?['Modified']}</li>         
      </ul>
</div>

18.png

Note: The HTML fragment contains expressions to inject properties from the ‘When an item is created or modified’ trigger, simply update as required by selecting different properties.

15 - The next step is to insert the HTML fragment contained within the ‘HtmlSummary’ variable into the PDF document generated by the ‘Merge document array to PDF’ action (Step #13).

Add an ‘Insert HTML to PDF’ action, applying the following configuration:

Detailed guidance: Insert HTML to PDF

 

  • HTML Data: Select the ‘HtmlSummary’ variable19.png
  • Filename: Insert the ‘Filename’ property from the ‘Merge document array to PDF’ action result.20.png
  • File Content: Insert the ‘File Content’ property from the ‘Merge document array to PDF’ action result21.png
  • Click ‘Show advanced options
  • HTML Insert Location: Select the ‘Prepend’ option, this will set the HTML fragment to be added to the start of the PDF document.22.png

16 - Add a ‘Send an email’ action, applying the following configuration:

 

 

  • To: Insert the ‘Customer Email Address’ property from the ‘When an item is created or modified’ trigger, or manually enter an email address.22a.png

Note: You will only see properties for list columns present on the source SharePoint list.

  • Subject: Enter ‘Order Confirmation’
  • Body: Enter ‘Order Confirmation’
  • Click ‘Show advanced options
  • Attachments Name - 1: Insert the ‘Filename’ property from the ‘Insert HTML to PDF’ action result.23.png
  • Attachments Content - 1: Insert the ‘File Content’ property from the ‘‘Insert HTML to PDF’ action result 24.png

17 - Complete: Your flow is complete and now ready for testing!

18 - I’ve added the following list item to the SharePoint list for testing, upon setting the ‘Order Status’ field to ‘Paid’, the Flow will be fully executed!26.png

19- The executed flow and relevant results are depicted below:27.png

I hope you’ve found this short(ish) tutorial useful, please let me know if there are any specific scenarios you would like to see documented solutions for!

All the best

Jay – Encodian

@encodian

Meet Our Blog Authors
  • Working daily with Microsoft Cloud to deliver the needs of my company, my customers and various Microsoft communities and forums. | Office 365 | Flow | PowerShell | PowerApps | SharePoint |
  • Co-founder of https://plumsail.com, Office 365 and SharePoint expert. Passionate about design and development of easy to use, convenient and flexible products.
  • Microsoft Business Apps MVP. Owner of ThriveFast, an Office 365 consulting company.
  • 7x Microsoft Business Solutions MVP (CRM)
  • Solution Architect with Slalom, and organizer of the Boston Office 365 User Group, and long term SharePoint/Office 365 veteren. Find more at http://www.davidlozzi.com. Follow @DavidLozzi
  • I'm keen in MS technologies, SharePoint, Office 365 and development for them
  • Daniel is a Business Productivity Consultant & Microsoft Business Solutions MVP who is very enthusiastic about all things Office 365, Microsoft Flow, PowerApps, Azure & SharePoint (Online). Since the preview, Daniel has been working with Microsoft Flow and later on with Microsoft PowerApps. That led to him being awarded an MVP Award for Business Solutions. He loves to blog, present and evangelize about improving productivity in the modern workspace with these amazing tools!
  • Michelle is an Office 365 solution architect in Twin Cities, MN. She has been delivering business collaboration solutions for years with her focus on SharePoint and Office 365. Michelle is a recent board member of the Minnesota Office 365 User Group and has been a member of the SharePoint community since 2009. She is a frequent speaker at MNSPUG and SharePoint Saturday and co-chaired the Legal SharePoint User Group for 4 years. Her most frequent projects have involved rolling out a large deployment of Office 365, SharePoint Online intranet, build of a "CHAMPS" Office 365 user adoption program and most recently, SharePoint On-Premise to Online Migration. Michelle is very excited about cloud technology as it is shifting her IT Pro focus to collaboration strategy and technical adoption.
  • I'm a Microsoft Office Servers and Services MVP with a special interest in SharePoint, Office 365, Microsoft Flow, Microsoft Teams and PowerApps. I work at Triad Group Plc ( https://triad.co.uk)
>