Sunday, January 14, 2018

Send Email using SharePoint and MS Flow without Exchange

In this post I will (concisely :-) describe how to send a mail to a SharePoint user from Microsoft Flow in case the standard MS Flow send mail action doesn't work for you. This can happen if you don't use Exchange Online yet. Not a very normal case probably, but hey, maybe it'll help someone.
This also works for external accounts.

First you need to add the users you want to send mail to in a user group on a SharePoint site. This is required as the webservice we'll be using gets the user account info from a hidden list on the site that should include the user and his or her email address. So add the account to the site. The user doesnt actually need any permissions on the site, but the SharePoint site needs to 'know' the user.

Now before we can create the Microsoft Flow, you'll need to setup a SharePoint App on the site that we'll use the credentials of to send the email from Flow. Because we're going to call a SharePoint API from outside SharePoint we need a valid access token to be able to do so.

The method to get a valid token in a Flow described in an older post of mine: Call any SharePoint REST API from Microsoft Flow. It is based on the article Access SharePoint Online using Postman that clearly explains how to register an App in SharePoint to get a client id and client secret.

So see those two articles on how to get the token. You will need to call a specific URL with the client id and client secret passed to it in order to get an access token back. If you follow the previous post you will end up with a Compose action that contains the token string.

The final action is a HTTP Post to:
https://yourtenant.sharepoint.com/sites/siteyouregisteredyourapp/_api/SP.Utilities.Utility.SendEmail

with headers:
Accept: application/json;odata=verbose
content-type: application/json;odata=verbose
Authorization: Bearer <add the output of the compose action that holds the token here>

and the body:
{
                "properties": {
                    "__metadata": { "type": "SP.Utilities.EmailProperties" },
                    "From": "jurgen.wiersema@domain.nl",
                    "To": { "results": ["jurgen.wiersema1@domain.nl"] },
                    "Body": "Boe!",
                    "Subject": "Test flow email"
                }

}

In order to mail external users, you need to add the User Principal Name to the To property in the JSON, e.g. "i:0#.f|membership|jurgen.wiersema_gmail.com#ext#@jwiersem.onmicrosoft.com". This account needs to have the external mail address set in its profile of course.

Additional considerations:
- The user needs to have been added to the SharePoint Site you call the SendEmail webservice on. You can automate this within MS Flow by calling a different REST API method, which is the EnsureUser method.
- If you want to re-use this Email users Flow functionality, then you can build the Flow as HTTP triggered. If you pass the JSON above into it, which you then use in the action to post to the webservice, then you can re-use this Flow in all your other Flows by using a HTTP Post action to the Re-usable Flow URL.

Get token HTTP call

Send Email through a REST call - 1

Send Email through a REST call - 2


Email received by external user





Thursday, January 11, 2018

Store link to executed Flow in SharePoint

What if you had a SharePoint list and you trigger a Microsoft Flow when an item is created in the list.
This might happen hundreds or thousands of times a day/week.
But how do you find out if the Flow did what it had to when it ran on the item? You go to flow.microsoft.com of course. Then you find out which Flow it was by looking through the list of crypticly named Flows you have access to. Finally you find it and you start browsing through all the Flow executions. You open them one by one until you find the Flow execution that handles the specific newly created item you are interested in.

This is not a very user friendly way of integrating MS Flow in SharePoint you might say! Wouldn't you want a link to the Flow execution log right from the SharePoint item that triggered the Flow? (hint: yes, you want this).

With this MS Flow expression you can get a URL to the executed Flow:

concat('https://emea.flow.microsoft.com/manage/environments/Default-0e0c2c6b-835a-4d45-8a92-4fac0d3be692/flows/', workflow().name, '/runs/', workflow().run.name)

The first part is the URL to your MS Flow tenant, so this is different for you. You need to specifiy your own values for both the region (in this case EMEA) and the Default-GUID, but you can easily get this from the browser address bar when you are inside MS Flow. The workflow() expression holds some metadata of the currently running workflow, and it turns out workflow().name is the guid of the Flow, while workflow().run.name is the ID that identifies a single execution of the flow. See the Logic App Workflow Definition Language for more info.

If you create a SharePoint list, and then create a Flow triggered on item creation in that list, then you can use this expression to save back the link to the executed Flow to the SharePoint list item.
In this way you can go to the exact executed Flow from the SharePoint list item itself.

The Flow that is triggered by creating an item in SharePoint. It can then do a bunch of stuff but in the end you want to store a link to the executed Flow back to the list item.


The Flow has run and the link is stored, in this case in the title field.

When you follow the link you end up in MS Flow on the executed Flow that you're interested in.