Automatically Set Microsoft Teams Work Location Based on Wi-Fi Network

Automatically Set Microsoft Teams Work Location Based on Wi-Fi Network

Back in June Microsoft released the work location feature in Microsoft Outlook and Microsoft Teams. This allowed you to set Teams and Outlook to display if you are working in the office or remotely on a given day, by either manually setting your work location, or by specifying a schedule along with your working hours in Outlook.

As I sometimes break with my normal work location schedule and go into the office when my schedule would say I was working remotely (or vice versa), I started looking for a way to automatically update my Microsoft Teams work location based upon my actual location.

Updating Microsoft Teams Work Location with the Presence API

The first step was to find a way to update my Microsoft Teams work location programmatically. To find a way to do this, I decided to see how the Microsoft Teams for Web client achieves this.

After inspecting the requests Microsoft Teams was making, I found this request was sent when I changed my work location through the UI.

PUT https://presence.teams.microsoft.com/v1/me/workLocation
{"location":1,"expirationTime":"2023-09-09T22:59:59.999Z"}

As we can see, when the work location is changed a PUT request is sent to https://presence.teams.microsoft.com/v1/me/workLocation with a JSON payload. The JSON contains a location and an expiration time.
It seems that the location property has the following possible values:

  • 0 is clear location.
  • 1 is office.
  • 2 is remote.

Now we know the request to make to the Microsoft Teams API to update the work location, we now need a way to make the request.

Using Power Automate to update the Microsoft Teams work location

I didn’t want to have to handle authenticating with the Microsoft Teams API and sending the API request directly, so I started to look for alternatives. I came across this blog post by Loryan Strant which came close to producing the setup I was aiming for.

In that blog post, it’s mentioned that the SharePoint HTTP Request action in Power Automate will handle the authentication process, and as a result avoid storing any authentication details on the device that triggers the work location update.

The Power Automate flow which I setup contains the following actions and trigger and steps.

1 - When a HTTP request is received

The request body JSON schema looked like the following:

{
    "type": "object",
    "properties": {
        "location": {
            "type": "string"
        }
    }
}

2 - Initialize variable

This had the name of locationId, a type of Integer and a value of 0.

3 - Switch

This action acted on the location variable, and had 3 cases.

  • Case 1 - location equals office so set locationId to 1.
  • Case 2 - location equals remote so set locationId to 2.
  • Default - Do nothing

4 - Current time

This action gets the current date and time, and makes it available to the following actions.

5 - Initialize variable

This had the name of expirationDateTime, a type of String and a value from this expression: concat(formatDateTime(parseDateTime(body('Current_time')), 'yyyy-MM-dd', 'en-GB'), 'T22:59:59.999Z').

6 - Send HTTP request to SharePoint

This action had a site address of https://presence.teams.microsoft.com using the PUT method, and a Uri of /v1/me/workLocation. A header of Content-Type with a value of application/json was also set.

The body of the request used the following JSON payload:
"location":locationId,"expirationTime":"expirationDateTime"}.

Now that the flow is setup, we just need a way to send a HTTP request when required to trigger the flow to run, with the proper location being provided.

Triggering the Power Automate flow with the iOS Shortcuts App

The final step was the choose how to trigger the work location update. My options were mainly basing it on location or basing it on active network connection. I wanted my phone to be the device which triggered the update, so in the end I decided to use the iOS Shortcuts app with an automation triggered by my phone joining to my workplace’s Wi-Fi network.

When the automation is triggered the “Get Contents of URL” action is then run which will make a request to the HTTP endpoint created for the Power Automate flow. The action is configured to contain a JSON payload which will set my location is "remote".

After this was setup, when my phone connects to the workplace Wi-Fi), my Microsoft Teams work location is updated to indicate I am in the office.

Summary

I was pleased to find that it was possible to use the SharePoint HTTP Request action to make an authenticated request to the Microsoft Teams presence API and deliver the necessary JSON payload for the request to be successful.

The creation of the public HTTP endpoint also makes it easy for a wide range of devices to be able to trigger the Microsoft Teams work location change, just by making a HTTP request. In my case, this was done through the iOS Shortcuts app based on the Wi-Fi network my phone is joined to.