Let us see how to work with Microsoft teams from SharePoint programmatically using Graph APIs.
Microsoft has provided the Graph APIs to work with Microsoft
Teams and its channels. Though, it is in a beta version. The graph APIs can be
triggered from any of the portals. Here, let us only look
at triggering the graph APIs from the SharePoint portals.
For example, the following graph API is used to get all the
teams which you are part of.
- https://graph.microsoft.com/beta/me/joinedTeams
Application with Microsoft Teams Permissions
First, you need to register the application in Microsoft dev
portal, where you need to provide the necessary permissions for accessing the
Microsoft Teams data from any portal.
- Login to the Microsoft applications portal https://apps.dev.microsoft.com
- Provide the name
- Copy the Application ID generated. (It will be used as
client ID in the SharePoint code in the below section)
- Add the platform as web and then provide the redirect URL.
In our sample, you need to provide the SharePoint page URL you will be using to
test the functionality.
- In the Microsoft Graph Permissions, add User.ReadWrite.All and
Group.ReadWrite.All permissions to the both the permissions (delegated and
applications permissions).
- Then save and close the application.
Navigate to the SharePoint page, where you want to test the
functionality. Add a content editor web part with jquery script reference.
Acquire Access Token and Hit Microsoft Teams Graph API
Access Token to be acquired for getting the Microsoft Teams
data. The request token will be generated by redirect URL. The redirect URL to
be built with the help of authentication server URL, domain details, response type,
client ID (generated above), resource URL (Graph API URL) and redirect URI
(SharePoint Page where the functionality is tested). In the following script
file, required logic to get the access token is written.
Once the page is redirected, the access token will be
available in the redirected URL. We need to extract the token and the ajax call
is made to the graph API using the access token.
Create a custom JavaScript file (teams.js) with the script content available in the below section.
Test
Test the functionality by hitting the SharePoint page
URL. The snapshot shows the Microsoft Teams.