Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Hosted-services / Azure

How to Call SharePoint from Microsoft Flow with the Help of an Azure Function

5.00/5 (2 votes)
25 May 2017CPOL4 min read 4.9K  
How to call SharePoint from Microsoft Flow with the help of an Azure function

Microsoft Flow team improves their product continuously, as well as other third party vendors. Unfortunately, there are still some gaps in its functionality, especially SharePoint related. That is why I thought it would be a good idea to cover some functionality by Azure Functions and make it easy to use and extend.

Image 1

As an example, I have implemented a function that allows you to create a new SharePoint folder from your Microsoft Flow.

At the end of this post, you will have Azure Function that is hosted in your Azure and a new Microsoft Flow action. It doesn’t require any programming skills. At the same time, if you are a programmer, you can still customize and extend it. All sources will be hosted in your GitHub repository. Feel free to add new functions and submit a pull request.

Azure Functions can pull source code from a GitHub repository. Thus, you can easily reuse Azure Function from my repository in your Azure environment. I will not describe details of implementation of the Azure Function here. It is a topic for another post. I will show how to use my SharePoint folder creation Azure Function below. Just take it and use.

Steps to use Azure Function in your Microsoft Flow:

  1. Fork my repository on GitHub
  2. Configure new Azure Function to pull code from your GitHub repository
  3. Register custom connector in Microsoft Flow
  4. Use Azure Function in your Flow as you would use any other action

Fork repository on GitHub

A fork is a copy of a repository. Forking a repository allows you to freely experiment with changes without affecting the original project.

The only prerequisite here is the creation of a GitHub account if you don’t have it yet. Then navigate to my repository https://github.com/plumsail/plumsail-sp-functions and click a “Fork” button at the right top corner.

Image 2

Now you have your personal copy of Azure Functions repository

Configure New Azure Function to Pull Code from a GitHub Repository

Open your Azure portal (https://portal.azure.com) and create Function App according to Azure documentation.

Open your Function and navigate to Platform features -> Deployment options.

Image 3

Click ‘Setup’ and choose GitHub as a source. Sign in to your GitHub account and pick ‘sp-azure-functions’ repository. Click OK:

Image 4

Now you need to wait for a minute. Code from GitHub repository will be published to your Function App.

Once it happens, you will be able to see new Function under ‘Functions’ section:

Image 5

Then you need to enable CORS (cross-domain requests) for https://flow.microsoft.com. It will allow Microsoft Flow to access the definition of our Function and call it.

Navigate to Platform features -> CORS:

Image 6

Add https://flow.microsoft.com to the list of allowed origins and click ‘Save’.

Then you need to add your SharePoint Online account name and password to application settings. The function will use them to create a new folder.

Image 7

Scroll down, add ‘SharePointUser’ and ‘SharePointPassword’ properties and save the settings:

Image 8

Register Connector in Microsoft Flow

Now we need to let Microsoft Flow know about our Function. Azure Function has a Swagger definition that describes API of a function. You can feed this definition to a Microsoft Flow connector. Thus, it will know how to call our function from a Flow.

First of all, we need to prepare swagger definition. Navigate to ‘API definition’ section of your Function App. You will see an XML structure representing swagger definition. You need to replace one word in your definition, it is an address of your Flow App:

Image 9

In the picture above, I replaced spfunction with sharepoint-function(name of my new Function App).

Click ‘Save’ and copy API definition URL somewhere.

Navigate to your Microsoft Flow account (https://flow.microsoft.com). Open Connections:

Image 10

Create a new Custom Connector using the API definition URL that you copied earlier:

Image 11

Click ‘Continue’ and fill in Label property. Label is a display name of an API key that will be displayed in Microsoft Flow Connection. I called it Key:

Image 12

Click Continue and Create connector buttons.

Once you created custom connector, you can use a function in your Microsoft Flow.

Use Azure Function in your Microsoft Flow as You Would Use Any Other Action

Create a new Flow and search for ‘SharePoint Create Folder’. You will see a new action available:

Image 13

Once you added it to your flow, you will be prompted for an API key:

Image 14

An API key can be copied from your Azure Function settings like this:

Image 15

Now you can specify parameters for the action:

  • sharePointSiteUrl — SharePoint site URL
  • baseFolderServerRelativeUrl — Server relative URL of a folder where you want to create a sub-folder
  • newFolderName — Name for a new sub-folder

    Image 16

 

Run your Flow:

Image 17

Conclusion

That is all! Now you have a new action in your armory. It may seem a bit tricky initially. But it has a huge potential for extension of your flows. Now you know how to use Azure Functions to communicate with SharePoint.

Azure Function is hosted in your Azure. It is completely under your control. Code of the function is in your GitHub repository.

If you are an IT Pro, take and use it without modifications. If you are a developer, feel free to contribute and add other functions and share them with the world!

This post was originally published in Plumsail blog.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)