Have you ever wanted to synchronize shifts in Microsoft Teams to evens in Outlook calendars? This is how you can do that!
Today, one of my HybrIT Services colleagues asked me if it was possible to create calendar entries for people after their work was scheduled in Shifts. Shifts is an app in Microsoft Teams that can be used to schedule work for your team members.
My response was of course, Yes! Power Automate can do all of that!
So where do we get started?
First of all it is important to understand how Shifts is used before we look at synchronizing shifts. My colleague stores the task details in the notes of a shift. Sets the start and end time and then that is it.
Now he would manually create a diary entry in the calendar and share that with the persons expected to complete the shift. The events that I will create in this flow are owned by the service account and then the assignee is invited to the event.
Just imaging if he could synchronize shifts to outlook in an automated way!
All flows in Power Automate start with a trigger and this synchronize shifts flow example is no different. I’m running this flow on a schedule of every 30 minutes.
During the development I used a manual starting flow, but in the long run I want to run this flow on a regular basis.
Getting the shifts
Then I want get all the shifts for everybody starting from today. I will not synchronize shifts that are older.
In the below example of List all shifts. I’ve limited the number of shifts to 50. But this can of course be increased. It will just depend on how much you have to schedule and how many shifts you want to synchronize.
I’m also not interested in anything in the past. So I will restrict the action by setting the From start time using the utcNow function.
Get the calendars
Then i’m getting my calendars in Outlook for the service account that runs my flow.
This is a bit of a clumsy action as I can’t filter it to the one that I want. But later in my flow I will filter out the right one.
Process the shifts
Now that I’ve got hold of the calendars and I can step through the Shifts returned by the List all shifts action
For ease of use I’m now adding my shift by setting a compose action to
This especially helps to debug the flow as the compose action displays my data for the shift that I’m processing.
Start time and end time
I’m now going to collect the start and end time from the shift.
This is where trouble starts. In Shifts, you can have draft shifts and shared shifts. Once a shift has been shared the format of the data changes. this means that sometimes I find my data for a shift in a json property sharedShift and sometimes in draftShift.
To get my start time I’m using the following expressions that will pickup the start time form the shared shift or draft shift.
if(equals(items('Apply_to_each_Shift')?['sharedShift/startDateTime'],null), items('Apply_to_each_Shift')?['draftShift/startDateTime'], items('Apply_to_each_Shift')?['sharedShift/startDateTime'])
If that wasn’t bad enough. The time is also 1 hour out. My current time zone is British Summer time and this will need to be corrected.
Time and correcting time
Time in this flow is going to be a bit of a pain. As we will see later on not all actions use the same time zone method.
The time that I get for my shifts are 1 hours out. to correct this time I used the following code in a compose action.
This is the wrong way of doing things of course. But converting time zones is something that I will address in a separate post.
Then the same needs to be repeated for the end time
And finally I’m going to take the notes form the shift
if(equals(items('Apply_to_each_Shift')?['sharedShift/notes'],null), items('Apply_to_each_Shift')?['draftShift/notes'], items('Apply_to_each_Shift')?['sharedShift/notes'])
Create the calendar events
I will need to know who is assigned to a shift first. All I have is a user id
This user id can be used in the get user profile action to get a user’s email address.
We are now ready to synchronize shifts.
Finding the right calendar
To find the right calendar I’m stepping through the calendars that we found earlier using an apply to each step. I could of course use a filter action to get the right one, but this time I’m going to get the apply to each and a condition to do the hard work.
The calendar that I want to use is called Calendar. this is the default calendar for a user.
Checking if a Calendar event already exists
I’m going to need 3 actions to check if my event already exists in the calendar.
I will try to find all the events by checking their subject. The subject will be set to the notes that I collected earlier in this flow.
The notes however are not unique. Therefore I will use the shift id which is stored in the body of my event to filter down to a single event in the calendar.
Now all we have to do is count the number of items that the filter array action is returning and we will know if the Outlook calendar contains an event already or not.
Do we need to create or update an event? Well that depends on the number of events found. If we find one then we do an update and otherwise we will do a creation of a new event.
If there are none found then the create event is easy. Just set the Subject to the notes. and the start and end time with a bit of date formatting magic will get the times sorted out.
Note that the corrected date is used here.
Update an event
When there is already an Outlook calendar event available and update is done. But the update should only be done if there is a change to be made.
So we start with a condition checking the values of the existing event with the values of the updates that we want to make. If this results in the flow finding updates to be required an update event action will be the final step.
Some more thoughts
In this post I ignored the try catch pattern, just so that I can keep the post simple. But of course you should implement this with the Try Catch pattern in place so that errors are handled.