Synchronize Shifts from Microsoft Teams to Outlook Calendars

Synchronize Shifts from Microsoft Teams to Outlook Calendars

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.

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

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.

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.

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

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.

Then the same needs to be repeated for the end time

End time

And finally I’m going to take the notes form 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 1

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.

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.

find calendar

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.

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.

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.

create 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.

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

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.

29 thoughts on “Synchronize Shifts from Microsoft Teams to Outlook Calendars

  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?”

    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’))

  2. 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 ( _ )

  3. 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?

  4. 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.

  5. 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.

  6. 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 apparantly 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.

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

  8. 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!

  9. 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!

Leave a Reply

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

%d bloggers like this: