Triggering Azure Functions From SharePoint Using Microsoft Flow
If you miss SharePoint's workflows, then good news! You can create your own flows with custom code using Azure Functions and Microsoft Flow.
Join the DZone community and get the full member experience.
Join For FreeWe cannot use classic SharePoint workflows on Office 365, but using Microsoft Flow, we can create modern flow applications that are triggered by events that happen in different sources. This blog post shows how to use Microsoft Flow and Azure Functions to send out an e-mail when a new meeting is added to a SharePoint list. The idea is to illustrate how to create the flow where custom code takes over at some point.
If you want to try these things out, you need valid Office 365 and Microsoft Azure subscriptions
Azure Functions
We start with creating a default Azure Functions HTTP-function. It accepts name
as a parameter and returns a greeting. If you are not familiar with Azure Functions, then please see my blog post HTTP-Triggered Azure Functions for more details about the code below.
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");// parse query parameter
string name = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
.Value; // Get request body
dynamic data = await req.Content.ReadAsAsync<object>();// Set name to query string or body data
name = name ?? data?.name; return name == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
: req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}
This function accepts a name as a parameter and returns “Hello name” as the output.
SharePoint
On the SharePoint site, we need a list for meetings.
This is a simple list, and it is more than enough for our simple example in this blog post.
Creating the Flow
We start creating a flow by registering our SharePoint site as a new connection. For this, we open the settings menu at the top of the page, select Connections, and click "Create connection". Then, we select SharePoint and log into our SharePoint account.
To send an email, we a need connection to Office 365 Outlook. I don’t go through these steps here because the process is almost the same as with SharePoint.
Let’s create our new flow now. The first step in the flow will be a trigger for a new item in our meetings list. This is the page we see when adding a new step to the flow.
For some reason, when clicking on SharePoin,t we see only four file related triggers and not all the triggers that Flow supports. Click on the blue triggers link and scroll down to where the SharePoint triggers start.
Click on SharePoint – When a new item is created. We are asked for our list location.
Fill in the site address, select the list name, and click on the "+ New step" button. We need at least one action to save our flow.
Type HTTP in the actions search box and select HTTP.
The HTTP action needs some configuring. Insert your Azure Function URL here and, at the end, add the name parameter that gets its value from the creator display name of the newly created list item.
Let’s add one additional step: email to manager.
After configuring the mail action, it’s time to save the flow by clicking on the checkmark at the top of the page.
Running Meetings Flow
Now let’s add a new meeting to the SharePoint meetings list. After waiting for a few moments, we get an email that looks like this:
For every run of our meetings flow, we can also check how much time it took to process every single step in the flow.
There is a list of all flow runs available at the Microsoft Flow site, and it is easy to filter out the flows that failed.
Wrapping Up
Although we don’t have support for classic workflows available on Office 365, we can use Microsoft Flow to mimic workflows for SharePoint. Microsoft Flow is not just about SharePoint. It is a rich service that allows us to build automated flows for many other types of sources and targets. We are able to connect Microsoft Flow to a SharePoint list on Office 365 and trigger a function running on Azure when a new item is added to that SharePoint list. It is an example of how it is possible to react to events on Office 365 SharePoint by using custom code.
Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments