Microsoft Graph Toolkit (MGT)

repository·main·Indexed 21 days ago

https://github.com/microsoftgraph/microsoft-graph-toolkit

A collection of web components powered by Microsoft Graph for integrating Microsoft 365 data into web applications. It includes core elements, a React wrapper (@microsoft/mgt-react), and various authentication providers such as Msal2Provider and SharePointProvider. The toolkit supports explicit component registration for tree shaking, tag name disambiguation to avoid collisions in environments like SharePoint Framework (SPFx), and custom provider implementation via SimpleProvider or the IProvider abstract class.

Tokens
56.5K
Snippets
168
Records
215
Agent score
76%

What's inside Microsoft Graph Toolkit

  1. What is the Microsoft Graph Toolkit?

    main

    The Microsoft Graph Toolkit is a collection of reusable, framework-agnostic web components and authentication providers designed to simplify accessing and working with Microsoft Graph.

    Components are fully functional out of the box and can be used in standard HTML/JavaScript or via React wrappers. Providers handle the authentication and token acquisition required to fetch data from Microsoft Graph APIs.

  2. Overview of Microsoft Graph Toolkit

    main

    The Microsoft Graph Toolkit (MGT) is a collection of reusable, framework-agnostic web components and authentication providers designed to work with Microsoft Graph APIs.

    Key features include:

    • Components: Pre-built web components for common Microsoft Graph experiences (e.g., agenda, files, people, tasks).
    • Providers: Implementations for authentication and token acquisition (e.g., MSAL 2.0, SharePoint, TeamsFx).
    • Framework Support: While components are web components, they are also available as React components via @microsoft/mgt-react.

    The @microsoft/mgt package acts as a convenience bundle that includes all MGT packages except for the React wrapper.

  3. Use the mgt-login component for authentication

    main

    The mgt-login component provides a UI for Microsoft Identity Platform authentication. It automatically switches between two states:

    1. Signed out: Displays a sign-in button.
    2. Signed in: Displays the active user's display name and profile photo/avatar. Clicking it opens a flyout containing profile data for all signed-in accounts and a sign-out option.

    Multi-account support: If used with a provider that supports multiple accounts (like MSAL 2.0), the flyout will include an option to "add another account" and allow users to switch between signed-in accounts. Switching accounts updates the active account context for all other Toolkit components on the page.

    Note: The component uses Person components internally to represent users.

    <!-- Example with MSAL 2.0 Provider -->
    <mgt-msal2-provider client-id="123"></mgt-msal2-provider>
    <mgt-login></mgt-login>
  4. Use the mgt-file-picker component

    main

    The mgt-file-picker component provides a user interface for selecting files from OneDrive or SharePoint. It includes a button that opens a dropdown list of files (rendered using mgt-file components) and a "See all" button to launch a full file browsing experience. When a user selects a file, the component returns the selected file object(s) to the developer via the filesSelected event.

    <!-- Example: Show the user's top 10 most recently used files -->
    <mgt-file-picker insight-type="used" show-max="10"></mgt-file-picker>
  5. Use the mgt-taxonomy-picker component

    main

    The mgt-taxonomy-picker is a web component that queries the Microsoft Graph API for Taxonomy to provide a dropdown control for selecting a single term.

    It can retrieve first-level terms from a specific term set, or first-level terms from a specific term within a term set.

    Note: This component currently only supports single selection.

    <!-- Show first level children of a term set -->
    <mgt-taxonomy-picker term-set-id="138a652e-7f23-46f6-b480-13da2308c235"></mgt-taxonomy-picker>
    
    <!-- Show first level children of a specific term under a term set -->
    <mgt-taxonomy-picker term-set-id="138a652e-7f23-46f6-b480-13da2308c235" term-id="a56caeb7-3b7d-4d22-93a9-0232e12905f6"></mgt-taxonomy-picker>
    
    <!-- Show children in a specific language (e.g., French) -->
    <mgt-taxonomy-picker term-set-id="138a652e-7f23-46f6-b480-13da2308c235" term-id="a56caeb7-3b7d-4d22-93a9-0232e12905f6" locale="fr-FR"></mgt-taxonomy-picker>
  6. Use the mgt-picker component

    main

    The mgt-picker is a component that queries a Microsoft Graph API endpoint and renders a dropdown control for selecting a single resource. It can also support predefined lists of resources that do not require a Microsoft Graph query.

    Common use cases include selecting a To Do list or any other resource from an endpoint that returns an array of items.

    <mgt-picker resource="/groups"></mgt-picker>
  7. Use the mgt-file-list component

    main

    The mgt-file-list component displays a list of folders and files using names, icons, and other properties. It leverages the mgt-file component to render individual items. You can configure it to show files from a specific drive, site, or based on user insights (trending, used, or shared), or provide custom queries.

    <mgt-file-list></mgt-file-list>
  8. What is the ProxyProvider and when to use it?

    main

    The ProxyProvider class allows a developer to proxy all calls to Microsoft Graph to their own backend.

    Use Case: Use this when authentication and Microsoft Graph calls must be handled on the backend rather than directly from the client. This ensures that all Microsoft Graph Toolkit (mgt) components function correctly by routing their requests through your controlled proxy endpoint.

  9. How disambiguation works in SharePoint Framework

    main

    Disambiguation allows you to use a specific version of MGT in your SPFx solution without colliding with other solutions on the same page. By disambiguating tag names, you avoid conflicts with other developers' components.

    Important Requirements:

    1. Unique Value: Use a disambiguation value unique to your organization and solution (e.g., contoso-hr-extensions) to prevent collisions.
    2. Helper Function: You must use customElementHelper.withDisambiguation('your-unique-id') to allow the creation of disambiguated tag names.
    3. Tag Format: The resulting HTML tags will follow the pattern <mgt-[disambiguation-id]-[component-name]>.
  10. Register components for tree shaking

    main

    Since v4.0.0, components must be explicitly registered to support tree shaking. Instead of importing all components, use the specific registration function for each component you need. The pattern is registerMgt{Name}Component().

    Note that some registration functions handle dependencies automatically. For example, registerMgtLoginComponent() also registers mgt-person internally.

  11. Customize mgt-picker item rendering with templates

    main

    You can use templates to override how different states of the picker are rendered. The rendered-item template is specifically used to customize the appearance of items within the dropdown list. When using rendered-item, the data context is the item being rendered (accessible via {{ this }}).

    Available template types:

    • default: Overrides the rendering of the entire component (Context: null).
    • loading: Renders the state while the Graph request is in progress (Context: null).
    • error: Renders if the search returns no results (Context: null).
    • rendered-item: Renders the individual items inside the dropdown (Context: the item being rendered).
    <mgt-picker resource="/groups">
      <template type="rendered-item">
        <span>
          <mgt-person person-details="{{ this }}" person-card="none" fetch-image></mgt-person> - {{title}}
        </span>
      </template>
    </mgt-picker>
  12. Restrict search results to specific users or groups

    main

    You can limit the scope of the mgt-people-picker to a specific set of users or groups using the user-ids or group-ids attributes. This is useful for creating a highly controlled selection experience.

    • user-ids: Provide a comma-separated string of user IDs. These users will appear in the dropdown when the input is focused, and search results will be restricted to these specific users.
    • group-ids: Provide a comma-separated string of group IDs. The search experience will be limited to users belonging to these groups. Note: If this is set, the type defaults to person.
    <!-- Example of restricting to specific users -->
    <mgt-people-picker user-ids="48d31887-5fad-4d73-a9f5-3c356e68a038,24fcbca3-c3e2-48bf-9ffc-c7f81b81483d"></mgt-people-picker>
    
    <!-- Example of restricting to specific groups -->
    <mgt-people-picker group-ids="02bd9fd6-8f93-4758-87c3-1fb73740a315,06f62f70-9827-4e6e-93ef-8e0f2d9b7b23"></mgt-people-picker>