cancel
Showing results for 
Search instead for 
Did you mean: 

Extending Flow with Functions - Part 1

Ever needed to do something in Flow which there isn't already provided in an existing action?

Very often there are packages out there, that we could use to extend our flow with more functionality.

 

In this article I will show you how to extend flow, using Azure Functions. The technique I'm going to use is:

 

  1. Create an Azure Function that take some input, does some processing, and returns a result;
  2. Test the function to show how it works as an API;
  3. Write a custom connector, so we can integrate with Flow (and PowerApps);
  4. Use it in Flow

In the first Part of this series, we will create the Azure Function, and in the second part, we will write the custom connector and use it in Flow.

 

Give me example of what's missing from flow?

 

A common senario in our flow is we have data - either from our trigger or another action - such as a SharePoint List Item. We then want to transform this into something  else - like the markdown for the body an email, or HTML for web page, or maybe even some json that we going to send into a HTTP action.

 

So we need something that takes a template, and transform the input to the output - we don't actually have anything in flow.

 

We could use a compose action, or set variable action, but it is cumbersome, and usually take multiple actions to achieve our output. Even grouping these actions together inside a scope, still just hides the fact that we have to do alot of work inside our flow to transform our data.

 

But there is very nice little tool - which we use everywhere else - including APIM in Azure - called Liquid Templates!

 

So in this article, I am going to show, with only a few lines of code, how to get Liquid Template working inside your flow. 

 

Where did the idea come from? 

 

A short distraction - OK - I'll be honest, I was watching a video from Ignite on the difference between Logic Apps and Microsoft Flow, and one of the big selling points of Logic Apps is - Liquid templates.

 

But wait - I use Liquid templates in my Flow's all the time - it one of the most useful actions I have - so I though I would share with you how I got Liquid templates working in my Flow environment, and show you some examples of why it's so useful.

 

Lets get started - Our Azure Function

 

The easiest way to get Liquid Templates is use a package from a package library - such as npm. There are number of liquid template packages, the one I'm going to use is liquid-node

 

 

This technique will work with most npm modules, or a module from other package managers.

 

Create our Azure Function

 

Lets start by creating our Azure Function - login to the Azure Portal, and create a Function App
liquid.PNG

Create a new function, with the "HTTP trigger" template
webhook.PNG

 

Install npm packages

Now we need to get the npm library into the function, so launch kudu on your function from the platform features tab

plaform.png

From Kudu - launch a CMD shell

cmd.png

Change directory to your function app, inside the site\wwwroot directory

cd.PNG

 

First - initialise the npm package.json file, with:

npm init

 

then, install the npm package
npm install liquid-node

 

Add Javascript code

 

Return to your function, and now insert the following JavaScript code:

 

var Liquid = require('liquid-node');
var engine = new Liquid.Engine;

module.exports = function (context, req) {
    var template = req.body.liquidtemplate;     
    var data = JSON.stringify(req.body.data).replace(/\*/g, '¬');      

    engine.parseAndRender(template, JSON.parse(data)).then(
        function(result){            
            context.log(result);
            context.res = {            
                status: 200,                   
                isRaw: true,
                body: result
            };
            context.done();
        }
    );
};

Here an explaination of the code:

 

  • require('liquid-node') - tells nodejs to use this module;
  • new Liquid.Engine - creates an instance of the Liquid engine;
  • req.body.liquidtemplate - in the json we receive, the liquid template will be in the liquidtemplateobject;
  • req.body.data - in the json we receive, the data we want to transform is inside the data object; 
  • engine.parseAndRender - this is the actual work - it takes the template and the data, and transforms it to the output;
  • context.res - this sends the response back.

 

 Let's test

 

Lets do a quick test to see if our liquid templates work. So lets take a simple bit of JSON with a person's name it:

 

{
  "name": "Mr PowerApp"
}

Lets change this a html heading

<h1>{{name}}</h1>

Click on Test and put in our request body, which is the template in liquidtemplate and our info in data, such as:

{ 
	"data":{
  		"name": "Mr PowerApp"
	},
	"liquidtemplate":"<h1>{{name}}</h1>"	
}

 Put this into the test area:
run.PNG

Click Run, and you should see the transformed output:
ran.PNG

Great - so now you have seen how to select a library from npm, install it on an Azure Function, add the code to execute the package, and test it.

Lets now integrate into our Power Platform - so our Flow and PowerApps can use it. In the next part of this series, we will create a custom connection, and use it in Flow.

Meet Our Blog Authors
  • 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
  • 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)
>