Trigger Flow using Column Formatter and share document to Microsoft Teams

In this blog post we are combining the functionalities of three Office 365 products – SharePoint, Teams and Flow. We are creating a Flow that will be triggered for selected item on click of button created using column formatting. This flow will fetch Teams and corresponding Channels associated to current user and post a message. If user is not associated to any Team, then an email is received by the user with message “No Teams Found”. Lets start by following the below steps –

Create Document Library

Navigate to the SharePoint Site and create a new document library “Team Documents” or an existing library can also be used. Just make sure that this should be Modern document library because we have to use column formatting.

In this library, create a new “Single Line of Text” column and name it “Sharing”. Now go to Column settings of this column and select “Format this column”.

Format

On “Format Column” screen, paste below JSON and click “Save” button.

{
     "$schema": "http://columnformatting.sharepointpnp.com/columnFormattingSchema.json",
     "elmType": "span",
     "style": {
        "color": "#0078d7"
     },
     "children": [
      {
        "elmType": "span",
        "attributes": {
            "iconName": "Flow"
        }
      },
      {
        "elmType": "button",
        "style": {
            "border": "none",
            "background-color": "transparent", 
            "color": "#0078d7", 
            "cursor": "pointer"
        },
        "txtContent": "Share with Teams",
        "customRowAction": {
              "action": "executeFlow",
              "actionParams": "{\"id\": \"183bedd4-6f2b-4264-855c-9dc7617b4dbe\"}"
        } 
      } 
   ]
}

Now lets first understand the concept of Column Formatting and above code.

Column Formatting

This allows us to customize how fields are displayed in list or libraries. It is done by constructing a JSON object which defines the elements used to display the field and the styles applied to these elements. This allows only to apply some pre-defined elements and styles, that is no custom code is allowed.

Lets analyse the above code-

“schema” – describes the schema location, inclusion of this implements the validation and auto-complete for JSON objetct.

“elmType” – type of element that will be created i.e. span in current scenario

“style” – optional property that defines css that will be applied to the element i.e. color in current scenario

“children” – optional property that defines any children to the element specified i.e. icon and text in current scenario

“attributes” – optional property that defines any additional attributes for the element i.e. iconName for the span element in current scenario. List if attributes that can be used is pre-defined and only those should be used.

“txtContent” – optional property that specifies any text for the element i.e “Share with Teams” for the button element.

“customRowAction” – optional property that defines any action associated to the element along with any action parameters i.e. executeFlow along with Flow Id in the current scenario. This defines that a Flow will be triggered on click of the button based on the Flow Id passed. We will update this Id later after creation of the Flow.

Create Flow

Navigate to “https://flow.microsoft.com” and sign in with your Office 365 credentials. Under “My Flows”, click on “Create from blank”, on the next screen again click on “Create from blank”.

BlankFlow

blankflow2

You will be presented with below screen. In the search box, type “selected item”, it will display trigger “SharePoint – For a selected item”, select this and provide details as in below screenshot.

(Add an Output : Add a text box with name “Message” and description “please enter your message here”. This message will be provided by user on trigger of Flow.)

SelectedItem.PNG

Click “New Step” and Add new action “SharePoint – Get File Properties” and fill details as in below screenshot. This will get the properties of the file like Name, Link etc. based on Id of the selected item.

Click “New Step” and Add new action “Microsoft Teams – List Teams”. This will fetch the list Teams associated to the current user.

ListTeams

Click “New Step” and Add new action “Initialize Variable” and provide details as in below screenshot. Here we are declaring a variable that we will use later to send email notification accordingly.

InitializeVariable

Click “New Step” -> “More” and select “Add an apply to each”. This will be used to iterate the list of Teams fetched in the earlier step.

ApplyEach

Click on text box and select “Teams List” from Dynamic Content window.

SelectTeamsList

TeamSelecetd

Inside “Apply to each” and new action “Microsoft Teams – List Channels”. Select “Team Id” from dynamic content. This will fetch the list of channels based on the selected Team Id as part of the each loop.

ListChannelsSelectChannels

Click on “More” and select “Add an apply to each”. This will be used to iterate the list of channels fetched in earlier step.

ApplyEach2

Click on text box and select “Channels List” from Dynamic Content window.

SelectChannelsListChannelsListSelected

Inside “Apply to each 2”, add new action “Microsoft Teams – Post Message” and select the details as in below screenshot. This will be used to post message on Teams by providing the Team Id, Channel Id as per the loops and the message that was input by user. We are also adding a link to the file in the message.

PostMessage

Below the “Microsoft Teams – Post Message”, add action “Variables – Set variable” and provide details as in below screenshot. This will confirm that message is posted successfully, hence set the value to true.

SetVariable

After “Apply to each” step, click new step and action “Get my profile (V2)”. In the select fields, type “Mail”. This will help in getting the profile of the user who triggered the Flow. Multiple properties can be defined in textbox separated by comma like Mail,Birthday etc.

GetProfile

Click new step and add condition. Set the condition as in below screenshot. Here we are checking if the message was posted successfully to Teams and based on that we will send email to current user.

ConditionVariable

Inside “If yes” block, add new action “Office 365 Outlook – Send Email” and provide details as in below screenshot.

IfYes

Inside “If no” block, add new action “Office 365 Outlook – Send Email” and provide details as in below screenshot.

Ifno

Complete condition should like in below screenshot.

CompleteCondition

Now save the flow and leave the edit mode. From the URL, copy the flow id as shown in below screenshot.

FlowID

Go back to document library -> “Sharing” column and update the Flow Id in “Format Column”.

Trigger the Flow

Now everything is setup. Go to document library and upload a document, a link/button will be generated under Sharing column as in below screenshot.

DocumentAdded

There are two ways to trigger the flow-

First is to select the item and then from Flows menu, select the flow to trigger. (see below screenshot).

SelectFlow

Second is to click on the button “Share with Teams”

Either way, a new overlay window will open which will ask for connections permission and after that a message box will appear to input message to be shared.

Message

Provide the message and click “Run Flow”. This will trigger the flow and message will be posted to all Channels and Teams associated to the user.

Go to Microsoft Teams, either by launching the desktop app or navigating to URL https://teams.microsoft.com and then to the channel. (see below screenshot)

PostedMEssage

Also an email notification be received by user.

Email

That’s all.

Thanks for reading. Please share your feedback.

4 thoughts on “Trigger Flow using Column Formatter and share document to Microsoft Teams

  1. I have configured a button in a column to run the flow instead of a link (code below). The flow runs fine from the Flow drop down menu but the button is not launching the flow dialog. The button is not doing anything. have you ran into this?

    {
    “elmType”: “button”,
    “txtContent”: “Get Approval”,
    “customRowAction”: {
    “action”: “executeFlow”,
    “actionParams”: “{\”id\”: \”e7ee4711-c0b1-4311-9999-b80d89e5b298\”}”
    },
    “style”: {
    “background-color”: “blue”,
    “color”: “white”,
    “visibility”: {
    “operator”: “?”,
    “operands”: [
    {
    “operator”: “==”,
    “operands”: [
    “[$_ModerationStatus]”,
    “Pending”
    ]
    },
    “”,
    “hidden”
    ]
    }
    }
    }

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s