Synchronize Shifts from Microsoft Teams to Outlook Calendars

Synchronize Shifts from Microsoft Teams to Outlook Calendars in 1 easy step by step tutorial

Have you ever wanted to synchronize shifts in Microsoft Teams to evens in Outlook calendars? This is how you can do that!

Shifts

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.

Microsoft Shifts Schedule in Microsoft Teams

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!

The Trigger

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.

Recurrence trigger

During the development I used a manual starting flow, but in the long run I want to run this flow on a regular basis.

New Line action

Later on in the flow we will need to handle new lines. For this I’m adding a compose action and rename this to New Line.

Synchronize Shifts from Microsoft Teams to Outlook Calendars in 1 easy step by step tutorial 1

The new Line action may look empty but it is not. It has a new line in it. Just hit that enter key once and you will have the job done!

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 if you need to synchronize shifts beyond the first 50. It will just depend on how much you have to schedule and how many shifts you want to synchronize.

List All Shifts action in Power Automate

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.

The odd thing however is that this will now only give me up to 50 shifts somewhere in the future. Most likely the most recently updated shifts. This is not what I want.

By setting the end time as well this problem goes away. So I set the end time to 4 weeks from now using the following formatDateTime expression that then I managed to get a lot more shifts.

formatDateTime(adddays(utcNow(),28), 'yyyy-MM-ddT00:00:00.000Z')

Get the calendars

Then i’m getting my calendars in Outlook for the service account that runs my flow.

Get calendars action to get outlook calendar

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.

The Get Calendars will give us an array of calendars, even if we only have one calendar that we are interested in. It’s possible to process all of these calendars and use a condition but why not filter the unwanted ones out.

Synchronize Shifts from Microsoft Teams to Outlook Calendars in 1 easy step by step tutorial 2

The compose action after the filter is there to turn the array into a single item. That way we avoid one of those unwanted apply to each steps making the overall Synchronize Shifts to Outlook Calendar events process that bit easier to understand.

All I have to do is use the following expression:

first(body('Filter_Calendar'))

Process the shifts

Now that I’ve got hold of the calendars and the Shifts I can step through my data.

Apply to each Shift

For ease of use I’m now adding my shift by setting a compose action to

items('Apply_to_each_Shift')

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'])
Shift Compose action followed by Start time compose

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.

addHours(outputs('Start_Time'),1)
Corrected start time to move to right time zone

This is the wrong way of doing things of course. But converting time zones is something that I will address in a separate post.

The expression I used here is:

addHours(outputs('Start_Time'),1)

Then the same needs to be repeated for the end time

End time

Notes in Shifts

And finally I’m going to take the notes from the shift

notes
if(equals(items('Apply_to_each_Shift')?['sharedShift/notes'],null), items('Apply_to_each_Shift')?['draftShift/notes'], items('Apply_to_each_Shift')?['sharedShift/notes'])

Now to make sure that the notes don’t have nay quotes and other characters causing problems I’m adding an compose action with the following expression:

replace(replace(replace(replace(outputs('notes'),'\/','\\\/'),'&','and'),'''','\'''), outputs('New_Line'), ' ')
Synchronize Shifts from Microsoft Teams to Outlook Calendars in 1 easy step by step tutorial 3

Create the calendar events

I will need to know who is assigned to a shift first. All I have is a user id

items('Apply_to_each_Shift')?['userId']

This user id can be used in the get user profile action to get a user’s email address.

Get user profile

We are now ready to synchronize shifts.

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.

get events

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.

The way to count the number of calendar events found is:

length(body('Filter_array_2'))

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.

We could use a condition for this as shown below, however I’m preferring the Switch action here!

create event

Why use a switch?

Well I actually might have 3 situations.

  1. The event doesn’t exist
  2. The item already exists
  3. Multiple items are found ( this really shouldn’t happen but just in case…)
Synchronize Shifts from Microsoft Teams to Outlook Calendars in 1 easy step by step tutorial 4

Create an 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.

formatDateTime(outputs('Corrected_Start_Time'),'yyyy-MM-ddTHH:mm')

Note that the corrected date is used here.

Synchronize Shifts from Microsoft Teams to Outlook Calendars in 1 easy step by step tutorial 5

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.

update event

The conditions in the no updates needed will compare the values of the existing item with the new values. The can be done with the following 4 expressions:

convertToUtc(items('Apply_to_each_2')?['start'],'Central Europe Standard Time','yyyy-MM-ddTHH:mm:00.0000000')

formatDateTime(outputs('Start_Time'),'yyyy-MM-ddTHH:mm:00.0000000')

convertToUtc(items('Apply_to_each_2')?['end'],'Central Europe Standard Time','yyyy-MM-ddTHH:mm:00.0000000')

formatDateTime(outputs('End_Time'),'yyyy-MM-ddTHH:mm:00.0000000')

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.

update event details

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.

35 thoughts on “Synchronize Shifts from Microsoft Teams to Outlook Calendars in 1 easy step by step tutorial

  1. Could you post the full flow? Im a little confused on how it all works together when its broken up like this.
    When do I start creating actions out side of “apply to each shift?”

  2. Kindly provide more full work so that we can understand better or can check what we did wrong.

    1. Hi Luka,

      for the first image:
      outputs(‘escaped_notes’)
      formatDateTime(outputs(‘Start_Time’),’yyyy-MM-ddTHH:mm’)
      formatDateTime(outputs(‘End_Time’),’yyyy-MM-ddTHH:mm’)

      For the length expressions:
      length(body(‘Filter_array’))

  3. Hi, and thanks for this huge work.
    I tried to implement it, but I encountered an issue, when I tried to save the flow:
    Flow save failed with code ‘InvalidTemplate’ and message ‘The template validation failed: ‘The action(s) ‘escaped_notes’ referenced by ‘inputs’ in action ‘No_update_needed’ are not defined in the template.’.’.

    I check the expression outputs(‘escaped_notes’), but no errors seen…

    Do you have any idea ?

    Thanks.

    1. Hi David,

      It sounds, like the action “escaped notes” doesn’t exist. Can you check the compose action that is called escaped notes and check if things are spelled exactly like that ( case sensitive? ). Power Automate will replace all spaces form the actions with an underscore ( _ )

  4. Hi,
    I have a working flow running now, but one issue remains for me for it to perform well. Do you have any tips on how you would go forward to make the flow delete the shifts that are already synced with the outlook calendar if they are removed from the Shifts calendar?

    1. Do you know which part is slow?

      I wouldblook at using Microsoft Graph and remove any nested apply to each steps. Can you share any details on the amount of tems that you are syncing?

  5. Thank you for the response first of all. I don’t think it’s is running slow, so that’s not a problem. The flow synchronizes the shifts of one team (~10 people) for the next 90 days, and recur every afternoon. It synchronizes from UtcNow() and 90 days forward, and it doesn’t add duplicates if there already is a shift there in outlook. The only problem is that if changes are made in the teams shift plan, the flow will not delete the shift that someone else took over. I haven’t tried microsoft graph before, but will check it out.

    1. So far I haven’t looked at deleting. My client didn’t have a need for that. But if any future client wants that then Ibam sure it would be possible to implement with a flow.

  6. For some steps it’s just not clear if it’s part of a step or a new step in itself. Get user profile for example I had it as a new step, but then it automatically gets placed into a new ‘apply to each’ which doesn’t add up to the amount of apply to each actions you seem to have (by my count 3). But if I place it in the previous ‘apply to each shift’ action then it still doesn’t look exactly like your picture where you can clearly see the + button below it whereas I get the ‘add an action’ below this step.

    In the ‘apply to each’ from Finding the right calendar, it’s not clear what goes under the ‘if yes, if no’ part.

    Except for the part of ‘Update an event’, which is done really well, there’s just a lack of overview of what goes where for those who are just starting out with power automate.

  7. So I created a flow based on your guide above, thanks for the work! It has no errors, and it creates calendar events. The only thing is, the calendar events are only one hour of length, and they don’t have any information in them. So no notes/subjects. And also the start time doesn’t match with the shift start time. So apparently something is going wrong, do you have any idea?

    1. Hi Daan,

      You might have to do some of the following additional steps:

      1. Convert time zones to your local time zone. These things can be a real pain. Ideally if anybody is within the same time zone you could make it easy for your self and just add X hours. Alternatively you could use the time zone conversion functions available

      2. In the flow that I created I used the notes. Simply because that is what my client used for the short description of the shifts. Personally I think that the display name of each task would be better. Also because the displayname of the task is shorter. When using the notes potentially you could end up with very long descriptions in Outlook. And outlook would simply chop this to the first 60 characters.

  8. I follow everything exactly but the calendar invite getting created in my outlook instead of the assigned agents. any reason?

    1. The out of the box calendar action cannot get to shared calendars so the flow owner account will need to send the invite.

      If you can use Microsoft Graph to do the updates that problem will go away.

  9. Pieter,

    I am having the same trouble as david5786David above. I get an error message that says‘The template validation failed: ‘The action(s) ‘escaped_notes’ referenced by ‘inputs’ in action ‘No_update_needed’ are not defined in the template.’.’.

    You provided him directions about making sure the escaped notes expression was correct. In the instructions provided on the page, I do not see an escaped notes expression in the instructions above.

    Where do I put an escaped notes expression in the template? Will you please provide me instructions on that? Thanks!

  10. Thanks Pieter,

    I added the escaped notes and input the function replace(replace(replace(replace(outputs(‘notes’),’\/’,’\\\/’),’&’,’and’),””,’\”’), outputs(‘New_Line’), ‘ ‘) however, I received an error stating, “Correct to include a valid refence to ‘New_Line’ for the input parameters of action ‘escaped_notes’.

    Should I be adding another action for New Line? If not, how should I correct this? Thanks!

  11. Hi Pieter, I would like to use your flow, but my flow skills are not too good. Can you export the flow so I can download it?
    Thanks!

    1. Sorry, I can’t export this flow as it is part of a bigger client related piece of work. All the steps are included in this post, if you need any help rebuilding the flow then please let me know.

  12. Hi Pieter, I can’t seem to get the Create Event function to work as its giving me an error on the formatdatetime expression. The error code is below. Any thoughts? Maybe it makes a difference (don’t think it does) but i skipped the Checking if a Calendar event already exists steps because we won’t have that issue here. I went straight from Apply to Each Shift to Create an Event…

    Unable to process template language expressions in action ‘Create_event_(V4)’ inputs at line ‘1’ and column ‘33603’: ‘The template language function ‘formatDateTime’ expects its first parameter to be of type string. The provided value is of type ‘Array’. Please see https://aka.ms/logicexpressions#formatdatetime for usage details.’.

    1. Hi Bryce,

      How are you triggering your flow?

      In my case it runs every 30 minutes and every 30 minutes all events are picked up from Shifts. This means that on the second run you would be creating a second copy of the same.

      The error that you are getting is that in the format date function that is used you are referencing some data that is holding multiple objects. What is the exact expression that you are using there?

  13. Hello, Pieter,
    has the post been reviewed since the question of Selena Schipper? Because I am having the same issue of not knowing if it is part of a step or a new step itself.
    Thanks in advance.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: