A Simple Document Expiry Solution with Microsoft Flow — Part One

Peter Baddeley
Baddaz
Published in
4 min readFeb 6, 2018

--

For one of my current projects I am starting to do a little bit of work with Microsoft Flow and SharePoint. Hopefully I will be extending this learning to look at how it could work with Dynamics 365.

The Scenario

Anyway my specific scenarios was looking at simple way to automate the deletion of documents when they expire. Specifically in my scenario I had a document Library with a meta data field called Expiry. Any documents which had an Expiry Date of before Today should be deleted.

Document Library with Expiry Meta Data

To achieve this I had a few options

  • I could use Labels which work across the whole of Office 365 and can have policies applied to them.
  • I could build a SharePoint Designer workflow.
  • I could enable the Record Management capability in my Site.

However I wanted something pretty simple and to align with the new modern approach, so I decided to take a look at Flow.

Building it in Flow

So I am going to assume that you know how to build a basic Flow, but if not drop over to the Microsoft Site to get some learning.

My first challenge was working out the trigger for this Flow. It was no good running it when users wanted to or based on an item being updated. In theory a document could expire every day.

With a little research I came across the Schedule — Recurrence trigger. This basically can be configured to trigger your flow on a schedule you define (eg every x Days or Weeks). There is also some advanced options to define specifically when this occurs.

Triggering my Flow on a regular basis

So in my scenario I could have the Flow run on a regular basis to see if any documents had gone passed their expiry date.

Next I had to get the documents from my Library, which would potentially be different each time my Flow ran. This is fairly simple using Get Items option, providing the Site and List details. (I found using the Library Guid more reliable to get the extra fields such as Expiry).

Getting the items from my Library

Then I needed to loop through all the of the items returned and delete any documents where the expiry has passed. Again this was fairly simple.

Firstly I need to use the Apply to Each option, which will create a loop area where you can check conditions and run actions on a list of items. It my case I could set the Output of items to run through as the value of the Get Items from my previous steps.

In English, loop through all the items which I retrieved and apply whatever steps I say.

The Check and Deletion Steps

For each item I do a Condition on the Expiry field in the List to see if it is less than the current Date. To get this to work you need use an Expression for what to compare against the Expiry Date, specifically utcNow(). (I noticed that the option to use Less Than and other Date driven comparison options were not available until I set this Expression).

Again in English, I am seeing if the Expiry date value against a document is less than Now/Today. If it is then do something to this document, and either way then loop to the next document.

If my condition turns out to be Yes, then I used the Delete Item action. I had to provide my Site and List again, and the ID of the Document will be available to set as parameter within the Loop you are doing.

That is it and below is the Flow in all its glory.

The finished Flow with all steps

A few thoughts

The first thing to say was that I was surprised how easy this was to build. It’s been a while since I worked with Flow and it is certainly getting easier to work with and more powerful.

There are definitely potentials to extend this solution to have notifications or actions going to users before documents are deleted. Alternatively documents could be moved rather than deleted.

However I would say however this works neatly as a one off solution, but would quite a pain to manage across multiple Sites and Libraries.

--

--

Product Manager and Consultant working with Office 365 and Dynamics 365. Founder of @provisionpoint and @qaixen. Organiser of #SPSLondon.