-->

26/09/2018

Microsoft Flow Basic Sharepoint Interactions

Objective: In this post we will look at basics of Microsoft Flow performing Sharepoint list operations.

We always drive our discussion based on a use-case. Below are the two scenarios we will consider for this post. 

1. Action based on a item creation or update
2. Action on list of items filtered from a list based on a condition. 


As i mentioned earlier, this is for Flow newbies with basic  Sharepoint interactions.

1. Action based on a item creation or update

For this i have created a list in a SharePoint online site. In there Due Date is a date field and the Customer Manager is a person field.


Now i open O365 home portal and open Flow portal. Though there are many pre-existing templates, for ease of understanding always start with a blank flow template.


I created one and name it as "Notification on Item Creation or Update".

The minute you create a new flow this is what you see. There is Flow support for many products, This means you can run Flow workflows based on triggers and actions from all these products.

For our purposes we concentrate on SharePoint events. Instead of me blabbing about every step this picture speaks volumes of each event and i will explain why we did it.

First we select a SharePoint List trigger "Item Created or Updated". In this trigger i select the SharePoint Online sites available in my tenant. Then i will select on which list i want this trigger to operate. 

If you are dealing with SP on-Premises sites or if you are dealing with random SP Online site based on user inputs, you need to use a http call event for querying SharePoint. I will cover "How to deal with dynamic sites or On-Premises SharePoint sites" in my next post.

For this post we are using static info, i.e., we know which site and list we are going to configure our flow before hand. 

Now we use a SharePoint action GetItem. Please don't get confused with GetItems. When i associate GetItem() action after a Trigger, it deals with the current item which generated the action. So based on the ID parameter GetItem() fetches the current item and pass it to next action.

Lastly we choose Outlook action "Send Email". You can see the composition of the email from above snapshot.

Save it and lets put it for a test. When you save the Flow and click on Test, you have 2 options. As we haven't done any testing yet, lets proceed with first option. Now i click "Test" button and then add a new item in my list.

Once the item is added, the flow will automatically triggered and will show the results. 
In this case it ran successfully. This is how it looks.
Lets verify our mail box. Here is the email generated by our flow.


Now i intentionally sabotage our flow to see how it shows in test if it fails.
I am messing the ID part in second step. Here it is and now lets see how our test goes.

Here is our test output. This time instead of creating a new item or updating existing one, i will choose to use earlier test data.


Here is the error and the reason why it failed. Its quite easy to debug and correct or mistakes.



2. Action on list of items filtered from a list based on a condition.

Now using the same list, i would like to select all customers whose due dates are expired and send a notification to their managers.
But i am the only SP User in my DEV tenant with mailbox to verify, so i will get all the emails.  Though i have 5 customers who are due for renewal, i want only one email reminding me that, not 5 emails. We don't want to clog everyone's mailboxes.

Lets see how we can handle all the conditions mentioned above, step by step.


First i want this flow to run automatically once every 6 months. So we choose a recurrence trigger and set the time accordingly.

Next step is GetItems(). Now you see the difference, get items will have various properties apart from site address and list name. in query i do put "Due Date" as filter field.

Please replace empty space in the field name with "_x0020_" and please put single quotes around the dynamic formula values like i did above. I am not looking for any orderby , so its empty.

Now sometimes the lists you use may brow beyond 5000 items, so its important to use TopCount to a required value. I choose 50 items at a a time.

We got the list of customer who are due for remnewal. I want to capture all the email addresses of their managers. For that we initialize a Array "NotificationEmails".



Here is the next step. Looping through every item, i will check if the manager email id is in the array. If not then add it, thus we will get unique email addresses. An array with unique manager email ids is the output of this step.


In this final step we are looping through email array and sending the emails. '
Now lets test the flow.


Here is the output.

You can see that there are 5 items were fetched which were due.  Then it populated the array and you can see it got unique values instead of duplicates. Thus there is only 1 item in last foreach.
I am verifying my inbox and want to see only 1 email instead of 5.


With this we have achieved our objective of learning how to use flow for basic SharePoint list operations.

In next post we will see how to use rest calls to query information from a on-premises or a dynamic SP Online site.

Hope this helps and happy coding !


No comments:

Post a Comment