Photo by Jonny Gios on Unsplash
Introduction
One of my iThink 365 team called me up about creating events using the Outlook 365 connector and Create Event activity.
The Create Event activity would successfully create an event when the Power Automate flow was run by the developer who created the flow. However, when the flow runs as anyone else then it failed with the following error,
“The specified object was not found in the store.”.
When we looked at the configuration of the Create Event operation we could see that the “Calendar”, calendar had been selected. So, if we have selected the right calendar, why is it failing? Why is it that Power Automate cannot find the Outlook calendar?
Well, the point is that the create event operation has a drop-down. This drop-down allows you to choose the name of the calendar to create the event in (as shown below).
However, when the calendar, “Calendar” is chosen it does not use the name of the calendar but rather the id instead. Each calendar has a unique id for each user’s mailbox. When we chose the calendar, rather than using a generic name, the Power Automate editor uses the calendars id. You can see that Id shown in the screenshot below (this was shown by clicking … for the operation and choosing Peek code).
So when Power Automate flow is running, it is running in the context of a user. In our example it is based on the user who started the flow. Now if the user is different to the developer, then they will have calendars with different IDs to the developer. So, in short, the error is right! The flow cannot find the calendar in the store because the calendar with the ID does not exist in the user’s mailbox!
Solution
So how do we resolve this and build something that works?
Well, we need to get the Id for the calendar and use that instead. How do we do that? Fortunately, there is an operation called Get Calendars which allows us to get all the calendars for the current user.
This operation will retrieve all the calendars in the user’s mailbox and using that it is possible to filter the array of calendars and select the id for the default calendar in the user’s mailbox.
This Filter array operation is then configured to take the output of the Get Calendars action and filter using the name “Calendar”.
Now, that we have the array item for the default calendar for the user, we can now use this with Create event by using the following formulae for the Calendar id.
first(body(‘Get_Users_Default_Calendar’))?[‘Id’]
Now that the Create Event action is using this formula to choose the Calendar Id, the operation will be able to find the calendar to successfully create the event in the user’s calendar. Regardless of which user runs the Power Automate flow!
Caveat
There is a caveat for this approach to work, firstly we need to ensure that the flow runs the connection in the context of the user who started the flow.
To do this, browse to the Power Automate Flow details page. Click edit next to the “Run only users” box. Change the drop-down to for the connector to say “Provided by run-only user”, as shown below.
Finally, set the Outlook 365 Connector to use “Provided by run-only user”.
The other point to make is that the flow will need to run in the context of the user that is having the event created in their calendar.
Therefore, there will be some design choices that you will need to make to ensure that the flow is started by the user who is to have the event created in their calendar.
This means that the flow cannot run off a system event or as an application as it will not have permission to write to the user’s calendar. Currently, the actions do not support being run as a service principal. If this were the case, it would be possible to give permission to write in every user’s calendar.
Anyway, hope that helps and please reach out if you have any questions on this.