Popular SharePoint Content Email Notifications

By

Let’s build an automated email notification system that highlights the most popular content from all SharePoint sites using Microsoft Flow.

If you haven’t used it just yet, Microsoft Flow is pretty awesome. It unlocks an entirely new set of capabilities within any Office 365 environment, not all of which are limited to what you would normally define as workflow. As an example, you can feasibly create highly extensible and customizable cloud applications that interact with users through connected apps and services. It’s deceptively easy to use, but there’s a lot under the hood that makes Flow applicable in more complex use cases where traditional solution development may fall short.

As an example, we could configure a Flow that periodically sends email notifications to users with the most popular content across all teams and SharePoint sites. It’s actually easier than it sounds. We’ll accomplish this by making an HTTP request to the SharePoint search REST API from Microsoft Flow, and iterating over the response in order to generate an HTML-enabled email.


Get Started

To watch the video tutorial, just scroll to the bottom of this post. For more information or to copy and paste the solution into your own Flow, read on.


Begin with a new scheduled Flow. Give your Flow a name and set up a schedule. We’re running our Flow once per week.

Build a scheduled flow


Add a new action to your Flow. Choose the SharePoint category, and then select Send an HTTP request to SharePoint. We renamed this new action to Query.

New Query action


Configure the Query action by selecting a Site Address. Next, select POST as the Method for the request, and enter _api/search/postquery as the Uri. Finally, add two new Headers to the request with the following values:

  • content-type: application/json;odata=verbose;charset=utf-8
  • accept: application/json

Query action configuration


Next, we need to configure the Body to inform SharePoint’s search REST API of the configuration of our request. We do this by entering a JSON object that the REST API will use to return results from the search service. To learn more about the search REST API, see our YouTube playlist covering REST operations in SharePoint. In our example, we’ve entered the following JSON data:

{
  "request": {
    "Querytext": "*",
    "SortList": {
      "results": [{
        "Property": "ViewsRecent",
        "Direction": 1
      }]
    },
    "EnableSorting": true,
    "__metadata": {
      "type": "Microsoft.Office.Server.Search.REST.SearchRequest"
    }
  }
}

That takes care of our HTTP request. Next, add a new Initialize variable action to the end of Flow. Configure the name of the variable as ResultsHTML, and select String as the type. Leave the intial value empty.

New initialize variable action


To get our Flow to work properly, we’ll need to iterate over various data objects within the flow. We can do this by setting up two new Apply to each actions immediately following the ResultsHTML variable initialization. Name the first Apply to each action Rows, and enter an expression as the output:

body('Query')?['PrimaryQueryResult']?['RelevantResults']?['Table']?['Rows']

The second Apply to each action should be placed inside the Rows action. Name this new action Cells, and once again enter an expression for the output:

items('Rows')?['Cells']

The Apply to each actions should now look something like this:

Nested apply to each actions


With our iterator actions in place, we can add two final Condition actions within the Cells action. Each of these conditional actions will conditionally append HTML to our previously initialized ResultsHTML variable. Start with a new condition named Title. For the left-hand conditional value, enter an expression of items('Cells')?['Key']. For the right-hand conditional value, enter Title. Repeat this process, but use Path instead of Title in the second conditional action. The resulting actions should look something like this:

Conditional actions in Flow


Of course we must update the ResultsHTML variable in the Yes branch of each conditional action that we just added. In each Yes branch, add a new Append to string variable action. Configure each of these actions to update the ResultsHTML variable.

Set up the new action created within Title with the following Value. Note that underlined text denotes an expression.

<h3>items('Cells')?['Value']</h3>

New title append configuration

Similarly, set up the new action created within Path with the following Value.

<p><a href="items('Cells')?['Value']">items('Cells')?['Value']</a></p>

New path append configuration


Last but certainly not least, add a new Send an email action to the end of the Flow. Enter any email address in the To field, enter a Subject, and be sure to place the ResultsHTML dynamic content variable into the Body field. Click on Show advanced options, and set the Is HTML property to Yes to ensure HTML is enabled in sent emails.

Send an email action configuration


That was quite a lot of configuration, but hopefully upon testing your new Flow, you’ll get a dose of popular SharePoint content right in your email inbox. The great thing about this Flow is that by using the ViewsRecent search property, content will stay fresh and up to date even as new emails are sent each week.


This tutorial is also available in video format on our YouTube channel.


About Us

We’re a small team of skilled professionals who craft highly polished software applications. We leverage cutting-edge technology and thoughtful, detail-oriented design to create unique and impactful experiences.

What are you working on?

We can help. Contact us to learn more about our design and development services.

Contact Us