How to call SharePoint from Microsoft Flow with the help of an Azure Function

by antonkhrit on ‎06-14-2017 12:57 PM

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.

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 other 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.

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.

 

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

 

 

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:

 

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:

 

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.

 

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

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:

 

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:

 

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

 

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’:

 

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:

 

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

 

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

 

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

 

Run your Flow:

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 you 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!

Please share ideas for new actions in comments. This post was originally published in Plumsail blog.

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 other 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.

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.

 

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

 

 

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:

 

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:

 

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.

 

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

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:

 

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:

 

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

 

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’:

 

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:

 

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

 

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

 

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

 

Run your Flow:

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 you 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!

 

Please share ideas for new actions in comments. This post was originally published in Plumsail blog.

Comments
by aegt1510
on ‎08-02-2017 10:18 AM

Hello, I hope you can help me a little bit on this. I`m doing exactly what you put on this thread, but I`m getting this error:

 

{
"id": "39c45f95-5a73-4dab-a7f0-895971287d41",
"requestId": "95e3d8bd-77eb-4e10-a46e-fbafb7cd426d",
"statusCode": 500,
"errorCode": 0,
"messsage": "Exception while executing function: Functions.CreateFolder -> The partner returned a bad sign-in name or password error. For more information, see Federation Error-handling Scenarios."
}

 

Any additional configuration on Azure or SharePoint???

 

Thanks

 

 

Do you know what is causing it? maybe my SharePoint Site doesn`t allow an azure service to connect, any additional configuration on Azure or SharePoint side that you think I could be missing?

 

Thanks for your help

by antonkhrit
on ‎08-03-2017 12:08 AM

Hi,

 

Did you specify your SharePointUser and SharePoint password in application settings?

 

If you use federation of domains, try to use original Office 365 account, which looks like accountName@yourdomain.onmicrosoft.com instead of an account within your custom domain. Sometimes there are issues related to configuration of federation. If you use this kind of account it is not affected by federation.

by aegt1510
on ‎08-03-2017 07:53 AM

Hello,

I already tried your advice with no sucess (actually it throws an error saying it didn`t find the user account (using domain.onmicrosoft.com)

 

Do you think that this is due to I need to be tenant administrator or something like that? Or maybe my authentication should be OAuth or something like that?,

 

Thanks for your help!

by antonkhrit
on ‎09-07-2017 12:19 AM

I think there is really no account that you use. You can't just take your domain account and replace your domain by domain.onmicrosoft.com. Those are different domains and differen accounts. You should create a new Office 365 account (domain.onmicrosoft.com) and use it. Or you can try to use your tenant admin account just to check if the issue is really related to domain federation.

 

Regarding your question about OAuth. Yes, changing authentication from login\password to OAuth (App only) will solve the issue, but I didn't use it in my article to simplify it.