Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Triggering Azure Functions From SharePoint Using Microsoft Flow

DZone's Guide to

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.

· Cloud Zone
Free Resource

MongoDB Atlas is a database as a service that makes it easy to deploy, manage, and scale MongoDB. So you can focus on innovation, not operations. Brought to you in partnership with MongoDB.

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.

flow-sharepoint-meetings-list

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.

Connecting SharePoint to Microsoft Flow

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.

Microsoft Flow: Add new trigger

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.

Microsoft Flow: Select trigger

Click on SharePoint – When a new item is created. We are asked for our list location.

Microsoft Flow: SharePoint 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.

Microsoft Flow: Select HTTP application

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.

Microsoft Flow: Configure HTTP action

Let’s add one additional step: email to manager.

Microsoft Flow: Send mail action

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:

Microsoft Flow: E-mail from flow

For every run of our meetings flow, we can also check how much time it took to process every single step in the flow.

Microsoft Flow: Flow log

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.

MongoDB Atlas is the best way to run MongoDB on AWS — highly secure by default, highly available, and fully elastic. Get started free. Brought to you in partnership with MongoDB.

Topics:
cloud ,azure functions ,microsoft sharepoint ,microsoft flow

Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}