Triggering Azure Functions from Office 365 SharePoint using Microsoft Flow

We cannot use classic SharePoint workflows on Office 365 but using Microsoft Flow we can create new era 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 e-mail when new meeting is added to SharePoint list. The idea of this blog post is to illustrate how to create the flow where custom code takes over in some point.

NB! If you want to try these things out you need valid Office 365 and Microsoft Azure subscriptions.

Azure Functions

We start with greating default Azure Functions HTTP-function. It accepts name as parameter and returns 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 name as parameter and returns “Hello name” as output.

SharePoint

On SharePoint site we need list for meetings. Here is the example of list.

flow-sharepoint-meetings-list

This is simple list and it is more than enough for our simple example in this blog post.

Creating flow

We start creating a flow with registering our SharePoint site as new connection. For this we open settings menu from top of page, select Connections and click on Create connection link. Then we select SharePoint and log in to our SharePoint.

Connecting SharePoint to Microsoft Flow

NB! To send e-mail we need connection with Office 365 Outlook. I don’t go through these steps here because process is almost the same as with SharePoint.

Let’s create new flow now. First step in flow will be trigger for new item in our meetings list. This is the page we see when adding new step to flow.

Microsoft Flow: Add new trigger

For some reason if clicking on SharePoint we see only four file related triggers and not all triggers that Flow supports. Click on blue triggers link and scroll down where SharePoint triggers starts.

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 site address, select list name and click on New step button. We need at least one action to save our flow.

Type HTTP to actions search box and select HTTP.

Microsoft Flow: Select HTTP application

HTTP action needs some configuring. Insert your Azure Function URL here and in the end add name parameter that gets its value from creator display name of newly created list item.

Microsoft Flow: Configure HTTP action

Let’s add one additional step: e-mail to manager.

Microsoft Flow: Send mail action

After configuring mail action it’s time to save the flow by clicking on check mark on top of page.

Running meetings flow

Now let’s add new meeting to SharePoint meetings list. After waiting for few moments we get an e-mail that liiks 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 list of all flow runs available at Microsoft Flow site and it is easy to filter out also 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 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 SharePoint list on Office 365 and trigger function running on Azure when new item was added to SharePoint list. It is example of how it is possible to react to events on Office 365 SharePoint by using custom code.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    One thought on “Triggering Azure Functions from Office 365 SharePoint using Microsoft Flow

    Leave a Reply

    Your email address will not be published. Required fields are marked *