How to get User Presence information in Microsoft Teams using React

Overview:
 
   Microsoft Teams as we all know is a unified communication & collaboration platform that combines persistent workplace chat, video meetings, file storage , and application integration. It is the hub for teamwork. Microsoft has announced retirement of Skype for business by July 31 2020, considering this Microsoft teams wiil be the default enterprise collaboration tool for communication. Microsoft Teams has rich set of features. There are 60+ devops integration apps available out of the box.
 
   Microsoft Teams integration API framework can communicate with outside services via Connectors. Connectors already exist to push updates from JIRA, Azure DevOps etc. and many more to Teams, and this API framework allow more to be built, also which allows organizations to link their internal apps. 
 
There are multiple use case scenarios where your line of business application needs to show the user presence information displayed in Microsoft Teams. 
 
   In this article we will walkthrough the steps to programmatically fetch the user presence information displayed in Microsoft Team into react webapplication. We will be making use of the Microsoft Graph Toolkit.
 
 
Steps to get the user presence information:
 
  •  Create a React web application using below command
  1. npx create-react-app  presence-using-mgt --template typescript  
  2. cd presence-using-mgt
 
  •  Add Microsoft graph toolkit NPM library using below command
  1. npm install @microsoft/mgt –save  
  • Now open open the webapplication folder in your favorite code editor.
  • In the App.tsx file, right after the import statement, add below code to instantiate the object of MSAL provider. Providers enable authentication and Microsoft Graph access for all components. Each provider provides implementation for acquiring the necessary access token for calling the Microsoft Graph APIs. 
 MSAL provider in the background uses MSAL. js to connect to Microsoft 365 and pull the data.Notice that there is a clientID parameter that you need to pass. Enter the client Id created in Azure Active Directory (AAD) here.
    1. function App() {  
    2. const [isLoggedIn, setIsLoggedIn] = useState(false);  
    3.   Providers.globalProvider = new MsalProvider({ clientId: '<Client Id>'});  
    4.   Providers.globalProvider.onStateChanged((e) => {  
    5.     if (Providers.globalProvider.state !== ProviderState.Loading)  
    6.       setIsLoggedIn(Providers.globalProvider.state === ProviderState.SignedIn);  
    7.   });  

  • In the index.tsx add this declaration. this declaration is required as react cant understand the non-"standard" tags associated with web component.
  1. declare global {   
  2.   namespace JSX {   
  3.     interface IntrinsicElements {   
  4.       'mgt-login': any;   
  5.       'mgt-presence': any;   
  6.   }   
  7. }  
Now as all the initialization is completed, we will add two components.
  •  Login component:  For authentication
    • Create a file with name NavBar.tsx in the root folder (same folder where app.tsx resides)
    • Open the NavBar.tsx and add below code to import the react and Microsoft graph toolkit components
      1. import React from 'react';     
      2. import'@microsoft/mgt';     
      3.     

    • Then add the mgt-login web component as show below
      1. const NavBar = () => {   
      2.     return (   
      3.         <nav>   
      4.             <mgt-login></mgt-login>   
      5.         </nav>   
      6.     );   
      7. }   
      8.   
      9. exportdefault NavBar;  
  • Presence component:  To display the user presence.
      • Create a file with name Presence.tsx in the root folder (same folder where app.tsx resides) 
      • Open the Presence.tsx and add below code to import the react and Microsoft graph toolkit components 
      1. import React, { useState } from 'react';  
      2. import { Providers, MsalProvider, ProviderState } from '@microsoft/mgt';  
      • Now create a compoment named Presence and add below code.
        1. const Presence = () => {   
        2.     return(  
        3.         <mgt-person id="online" person-query="<email address of the person whose status to fetch>" show-presence view="twoLines"></mgt-person>  
        4.   
        5.     );  
        6. }  
        7. exportdefault Presence;  
  • Finally use these two components inside the main component called "App". Your App component should look like this:
    1. import React, { useState } from 'react';  
    2. import { Providers, MsalProvider, ProviderState } from '@microsoft/mgt';  
    3.   
    4. import Presence from './Presence';  
    5. function App() {  
    6. const [isLoggedIn, setIsLoggedIn] = useState(false);  
    7.   Providers.globalProvider = new MsalProvider({ clientId: '<Client Id>});  
    8.   Providers.globalProvider.onStateChanged((e) => {  
    9.     if (Providers.globalProvider.state !== ProviderState.Loading)  
    10.       setIsLoggedIn(Providers.globalProvider.state === ProviderState.SignedIn);  
    11.   });  
    12.   
    13.   return (  
    14.     <div>  
    15.       <NavBar />  
    16.       {(isLoggedIn) ?  
    17.         <div className="grid sm:grid-cols-2 gap-4 m-10">  
    18.           <div>  
    19.             <h1 className="mx-3 my-3 text-2xl leading-tight">  
    20.               User Presence  
    21.           </h1>  
    22.             <Presence/>  
    23.           </div>  
    24.             
    25.         </div>  
    26.         :  
    27.         <div className="font-semibold tracking-tight text-center shadow-md p-4 m-10">  
    28.           Login to show user presence  
    29.         </div>  
    30.       }  
    31.     </div>  
    32.   );  
    33. }  
    34.   
    35. export default App;  
 Now open the terminal and execute the command npm start and you should see the login screen as below:
 
 
 
 Provide the user credentials and you will be logged in to see the user presence.
 
 
 
 
Summary:
 
Microsoft Graph Toolkit components and helpers helps developers tremendously when accessing and working with Microsoft 365 data. In the above steps we have seen how easily we can connect to Microsoft 365 and fetch user presence information in Microsoft Teams of a user using Microsoft Graph Toolkit  in a react webapplication.