Follow

API example: SharePoint newsfeed in announcements

Table of contents

 

Introduction

You can use Microsoft Power Automate to post information to the Announcement Centre in Workspace 365. In this article we will show you how to use a Power Automate flow to generate announcements from news posts in a SharePoint Newsfeed. Check out the Announcement API endpoint for more information. 

Power Automate can only access the shortened version of the news post contents. This is called the "Description" and contains the first 250 characters without any form of styling. We can however add a link to the full news post.

 

Prerequisites

  • An account with access to the SharePoint site.
  • A Power Automate Premium license (tip: Microsoft offers a free 90-day trial license). 
  • An enabled API key for the Announcements API. You can have multiple Announcement API keys enabled, so don’t hesitate to generate a key for each information source. This contributes to both security and manageability.

Back to top

 

Configuration

Create the Power Automate flow

  • Log in to Office 365 and click on the waffle button in the top left corner.
  • Open the app Power Automate.
  • Choose Create to create a new flow.
  • Choose Automated cloud flow.
  • Assign a flow name to your flow. Search for “created or modified properties” as SharePoint trigger to start your flow with and click on Create.

    trigger.png

  • Select the relevant site from the Site Address dropdown.
  • Enter the Library Name. If the Site Pages library doesn’t show up in the dropdown field, you need to enter a custom value and use the GUID of the Site Pages library. You can find this value by going to the Site contents and opening the Site Pages settings:

    sites_pages.png

    The GUID is now listed in the URL:
    url.png

  • Copy this value from the URL and paste it into the Library Name:
    library_name.png

Back to top

 

Add a Condition

To filter news posts that are published you can use the metadata field called “Promoted State”. This field has three possible values:

  • 0 = Site Page
  • 1 = News Post in Draft
  • 2 = News Post Published

We only want published news posts, so we set the condition to “is equal to” 2:

condition_2.png

Back to top

 

Posting to the Announcements API

  • In the "If yes" field, add an action and search for HTTP (premium).

    action.png
If you do not have a Power Automate Premium license, you will be offered a 90-day trial license.
  • As the Method, choose POST.
  • The URI is the endpoint URL for the Announcements API of your Workspace 365 environment, e.g:
    https://workspace365instance.url/environmentname/api/announcements.

    method_post.png

  • Fill in the Authorization header: "Workspace365" followed by the API key for the Announcements API.

headers.png

 

  • An example of the body

body.png

  • Or in code:
{
"Title": "@{triggerOutputs()?['body/Title']}",
"Description": "@{triggerOutputs()?['body/Description']} <br><br><a href=@{triggerOutputs()?['body/{Link}']}>Read more on Sharepoint</a>"
}
  • Save your flow.
  • Test it by creating a SharePoint news post and post it with the “Post news” button in the top right corner.

    save_and_test.png
  • If your flow succeeds you should now see the announcement in Workspace 365!

    announcements_test.png


Back to top

 

Background information on the JSON body

The body of the HTTP action is where we provide the announcement, but it needs to be formatted as a JSON object. JSON stands for JavaScript Object Notation and it adheres to a few simple syntax rules:

  • Curly braces hold objects
  • Data is in name/value pairs
  • Data is separated by commas

Whether you use the Workspace 365 Announcement Centre to write an announcement or the Announcements API, your announcement will always consist of a few basic elements. The most important ones being a title and content for the users to read. These items are submitted to the API as properties of the JSON object. The only property that is required by the Announcements API is the “Title” property, but an empty announcement is rather useless, so we want to add content as well. The content of the announcement is submitted in the object property called “Description”.

Unfortunately Microsoft doesn’t offer the full content of the news post to be included, but they do offer a shortened version, containing the first 250 characters without styling. The metadata field that contains this data is also called “Description” in SharePoint. Luckily, Workspace 365 accepts HTML code as content for the announcement, so we will complement this data with a clickable link to the full new post on SharePoint. For the URL of the news post, you can use the dynamic content “Link to item”. To make the announcement look a little prettier, add a space and two line break tags between the SharePoint Description and the HTML hyperlink element.

Back to top

 

Background information on flow run history 

Your Power Automate flow will be triggered by the creation of the post, so the flow will initially report back as Succeeded. However, since the post has only been created but not published, the Promoted State will not equal 2 and the condition we set in our flow will fail. This means the first test will not lead to an announcement. However, since we’ve posted the news once we were done editing, the flow will be triggered a second time shortly after the first time. You will see this in the flow run history. Click on the blue “Back” arrow in the top left corner of the Power Automate editor window and keep an eye on the runs. It may take a few minutes before the flow is run, but eventually you will see two runs:

runs.png

 

Back to top

 

Optional features

Configure a static Announcements category

If you want announcement to always be made in a specific category, you can define this in the body of the HTTP action. For example setting the Category to the specific value of “Sharepoint”:

{
"Title": "@{triggerOutputs()?['body/Title']}",
"Description": "@{triggerOutputs()?['body/Description']}<br> <br><a href=@{triggerOutputs()?['body/{Link}']}>Read more on Sharepoint</a>",
"Category": {
"Name": "Sharepoint"
}
}

Back to top

 

Setting an announcement option through a SharePoint column

If you are able to add columns to the SharePoint Site Pages form, you can use this as an easy way to control announcement options, such as the option to show the announcement as a popup. Here’s how this works.

The Announcement schema of the Announcements API contains a property called "ShowPopupDirectlyToUser". This option expects a value of “true” or “false”. If you add a column to the Site Pages form with a descriptive name and specify the values expected by the API as possible options, you can pass this information directly to the Announcements API.

create_column.png

This is a column of the type Choice. In this example we have added a description and edited the choices to match the values expected by the API. The default value is false, so we have also set the default value in SharePoint to false. By displaying the choices as a radio button, you enforce a single selection. If you want this option to be mandatory when a news post is made, set “Require that this column contains information” to Yes.

Once this column has been added, we can now add it to our flow as dynamic content. Make sure you choose the Value and not the column title:

value.png

Back to top

 

Adding extra conditions to filter news posts

If you want to filter news posts before they are submitted to the Announcements API, you can add extra criteria to the Condition. This can be based on a column value like in the previous example or another metadata field. To add an extra condition criterium, click on the Add button in the Condition and choose “Add row”:

condition2.png

The default operator for the Condition rows is And because you will most likely want to filter based on both criteria. Should you want to filter on either criteria, you can change this to Or. Next, you can specify a criterium in the second row. In this example, we added an extra column to the Site Pages view called “My Filter Column”. Make sure you choose the dynamic content which ends with “Value”, because you want to filter based on the value in the column.condition3.png

After choosing “My Filter Column Value”, complete the criterium as you see fit:condition4.png

Back to top