About one week ago I’ve written a post on how to integrate Dynamics 365 Business Central with Microsoft Teams and after that post I’ve received a lot of questions (so surprised that the topic was so hot).
My goal was to write a message to a Teams channel from Dynamics 365 Business Central because I think that (at least for my customer’s base) this is the most interesting and useful scenario: when something important happens on Dynamics 365 Business Central, I want a notification on a particular Teams channel for react to that event.
Many of you have requested me also the opposite way: how can I read a message posted on a Teams channel (or maybe a reply to a message that I’ve previously posted on the Teams channel from Dynamics 365 Business Central) and then react to that message from Dynamics 365 Business Central?
Here the things are a bit more complicated I think that if you want to react to what happens (or what someone writes) on a Teams channel from Dynamics 365 Business Central you have essentially two ways:
In this post I want to talk about the second option (first option maybe in the future ) because it’s the direct way that you can use also from AL code.
Microsoft Graph is a RESTful web API that allows you to access Microsoft Cloud and Office 365 resources. Using Microsoft Graph APIs essentially requires the following steps:
HttpClient.DefaultRequestHeaders.Add('Authorization', 'Bearer ' + token);
Said that, you can test Microsoft Graph APIs by using the Graph Explorer tool. This tool is in my opinion a must to use before using Graph APIs in your application because it permits you to discover the permissions you need to set for your objects (Microsoft Graph is quite tricky about that).
Microsoft Graph APIs are in version 1.0 (the default to use) but some features are actually available only on the BETA version (so you need to set the version to use in your API endpoint).
What can we do now with Graph APIs and Teams?
You can see what Teams you’ve joined by sending a GET http call to the following URL:
As you can see, the response is a JSON document with the ID of an entity (that you need to use for retrieving details for that entity in next calls) and with details for that entity. In the JSON above, you can see that I’m subscribed to two Teams.
To retrieve a list of channels where the user is subscribed for a specific Team, you need to send a GET http call to the following endpoint (where teamid is the ID of the team retrieved on the previous call):
To retrieve the details of a particular channel, you’ve to send a GET http request to the following endpoint:
where channelid id the ID of the channel that you want to retrieve (as returned by the previous call):
Now we want to work with messages on a channel. The 1.0 Graph API doesn not actually permit to work with messages, so for that scope we need to use the BETA api version.
To retrieve a list of messages posted on a channel, you need to perform a GET call to the following endpoint:
To retrieve a particular message, you need to perform a GET call to the following endpoint:
where messageid is the ID of the message to retrieve:
This is the JSON content of the message I’ve sent to Teams from my Dynamics 365 Business Central extension I’ve sent in the previous post.
If you want to check the reply messages to this post, you’ve to send a GET http request to the following url:
As you can see, the message previously sent from Dynamics 365 Business Central to my Teams channel has a reply from a user (Stefano Demiliani) with the content in the red box.
To retrieve the details of a specific reply message, you’ve to send a GET http request to the following url:
where replyid is the ID of the reply message to retrieve.
The JSON of the reply content is like the following:
"displayName": "Stefano Demiliani",
"content": "Thanks D365BC! Message arrived."
By parsing this JSON message, you’ve all what you need (author, content, attachments, mentions etc.) for handling a reply to your message sent to Teams from Dynamics 365 Business Central.
Possible scenarios? Many
You can search for messages and then react to replies (a reply can start a Dynamics 365 Business Cenral process, for example order authorizations and so on).
Many doors are open here, how to use these features is just your fantasy
P.S. At the time of writing this post, unfortunately OData Query parameters are not currently supported, so you can’t directly retrieve a particular reply message by applying a filter via query string. You need to send the query as show above and parse the JSON response.