Monday, 8 May 2017

Create a simple timer job for SharePoint online using Microsoft Flow

Creating scheduled jobs for SharePoint on-premise environment was pretty straight forward. Develop a timer job and deploy it to the farm and you are done. With SharePoint online, there are a few different ways to develop scheduled jobs, one of which is to create a Azure web job which talks to SharePoint data and runs the required tasks.

However, some organizations/small businesses do not have Azure subscription or the technical ability/developers to develop and deploy an Azure web job. Sometimes, the required functionality of the scheduled job is not too complex to create a Azure web job.

With Microsoft flow, we can easily create timer job-like features that run on a schedule and carry out a few simple tasks.I say simple because when I set out to explore several different scenarios where a Microsoft flow timer jobs can be used for a SP list, I found that Flow lacks several features. While it provides a lot of connectors, triggers, actions and conditions, as developers or power users there are a lot of actions/trigger/functionalities we need that are missing. At the end of this post I'll explain in brief about a scenario I tried to implement using Flow however it did not work out well.

Sample Timer Job

Meanwhile I decided to develop a simple timer job for asset request and procurement process. The job runs on a schedule and moves the "Approved" asset requests from requests list to a separate "Procurement" list. In this post we will see how to

1. Get items from SharePoint list
2. Filter SharePoint list items using a ODATA filter.
3. Loop on the list items
4. Create and delete list item

I have created two lists that will be used in this flow. Asset Request list with NEW and APPROVED requests (choice column) and a Asset Procurement list which will be empty initially.

Navigate to Flow from Office 365 and create a new flow from blank template.

Give your flow a name. The first step we add here is a Schedule action. This will enable us to define a schedule for our process to run periodically.

The schedule can be set based on Minutes/Hours/Days etc as given below

Now, we will retrieve items from Asset Request list. Add a new action and select SharePoint

Select the Get items action

Configure the Get items action. We retrieve the list items from Asset Request list and filter by only the items which are Approved

Now that we have the approved items, the next step is to iterate through the items and create these items in the Asset Procurement list. To add the Loop operation, click "Add an apply to each" action as shown below

We need to tell the loop action which data source it should iterate. Select the 'value' as indicated below. Notice the data source label is the previous action "Get items"

We need to perform two actions now,

1. Create item in Asset Procurement list
2. Delete Approved items from the Asset request list

Add an action to create a list item in SharePoint.

Select the site and list in which the items must be created and indicate the value for each column in target list. Notice that the data source from where we select the value for the item to be created is coming from our previous Get Items action. So the values coming from the Asset Request list will be added to the Asset procurement list.

Now we create a new action to delete the Approved items from the Asset request list.

We need to delete item from the Asset request list. So select the source carefully. Since we have retrieved items from the Asset request list through Get Items action, select the ID from that data source as shown below.

The complete list flow look as shown below.

After the flow execution, our lists look like :

Limitated functionalities :

The limitation or issues mentioned here are something that I noticed at the time of writing this post. As and when updates are pushed to Microsoft Flow, I will try and update this post as well :)

While creating a SharePoint list item through flow, I noticed that we cannot add values to People column and Choice column as of now. There are a couple of more types of columns which are not supported.

Also, the SharePoint get items action only accepts Odata filter and does not support the Odata Select operation.

There might be several other actions/triggers which you may need to implement your business requirement but are not yet available in Flow.

Hope this helps !!

No comments:

Post a Comment