• Stars
    star
    155
  • Rank 240,864 (Top 5 %)
  • Language
    C#
  • License
    Other
  • Created about 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Set of Helpers and Controls for Windows development using the Microsoft Graph.

Windows Community Toolkit - Graph Helpers and Controls

Welcome! This is a sub-repo for the Windows Community Toolkit focused on Microsoft Graph providing a set of Authentication and Graph helpers for Windows applications.

Note: This new library replaces the Microsoft.Toolkit.Uwp.UI.Controls.Graph package; however, it is not backwards compatible nor does it provide all the same features at this time.

If you need similar controls for the Web, please use the Microsoft Graph Toolkit.

Supported SDKs

Package Min Supported
CommunityToolkit.Authentication NetStandard 2.0
CommunityToolkit.Authentication.Msal NetStandard 2.0, UWP, .NET 6, .NET 6 Windows 10.0.17763.0, .NET Core 3.1
CommunityToolkit.Authentication.Uwp UWP Windows 10.0.17134.0
CommunityTookit.Graph NetStandard 2.0
CommunityToolkit.Graph.Uwp UWP Windows 10.0.17763.0

Samples

Check out our samples for getting started with authentication providers and making calls to Microsoft Graph:

Contoso Notes Sample

Contoso Notes is a premier sample note-taking app infused with Graph powered features and controls from the Windows Community Toolkit, demonstrated in practical application scenarios.

Getting Started

To get started using Graph data in your application, you'll first need to enable authentication.

1A. Setup authentication with MSAL

Leverage the official Microsoft Authentication Library (MSAL) to enable authentication in any NetStandard application.

  1. Register your app in Azure AAD

    Before requesting data from Microsoft Graph, you will need to register your application to get a ClientID.

    After finishing the initial registration page, you will also need to add an additional redirect URI. Click on "Add a Redirect URI", then "Add a platform", and then on "Mobile and desktop applications". Check the https://login.microsoftonline.com/common/oauth2/nativeclient checkbox on that page. Then click "Configure".

  2. Install the CommunityToolkit.Authentication.Msal package.

  3. Set the GlobalProvder to a new instance of MsalProvider with clientId and pre-configured scopes:

    using CommunityToolkit.Authentication;
    
    string clientId = "YOUR-CLIENT-ID-HERE";
    string[] scopes = new string[] { "User.Read" };
    
    ProviderManager.Instance.GlobalProvider = new MsalProvider(clientId, scopes);

Note: You can use the Scopes property to preemptively request permissions from the user of your app for data your app needs to access from Microsoft Graph.

1B. Setup authentication with WindowsProvider

Try out the WindowsProvider to enable authentication based on the native Windows Account Manager (WAM) APIs in your UWP apps, without requiring a dependency on MSAL.

  1. Associate your app with the Microsoft Store. The app association will act as our minimal app registration for authenticating consumer MSAs. See the WindowsProvider docs for more details.

  2. Install the CommunityToolkit.Authentication.Uwp package.

  3. Set the GlobalProvider to a new instance of WindowsProvider with pre-configured scopes:

    using CommunityToolkit.Authentication;
    
    string[] scopes = new string[] { "User.Read" };
    
    ProviderManager.Instance.GlobalProvider = new WindowsProvider(scopes);

2. Make a Graph request with the Graph SDK

Once you are authenticated, you can then make requests to the Graph using the GraphServiceClient instance.

Install the CommunityToolkit.Graph package.

using CommunityToolkit.Authentication;
using CommunityToolkit.Graph.Extensions;

ProviderManager.Instance.ProviderStateChanged += OnProviderStateChanged;

void OnProviderStateChanged(object sender, ProviderStateChangedEventArgs args)
{
    var provider = ProviderManager.Instance.GlobalProvider;
    if (provider?.State == ProviderState.SignedIn)
    {
        var graphClient = provider.GetClient();
        var me = await graphClient.Me.Request().GetAsync();
    }
}

Make a Graph request manually

Alternatively if you do not wish to use the Graph SDK you can make requests to Microsoft Graph manually instead:

using CommunityToolkit.Authentication;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

private async Task<IList<TodoTask>> GetDefaultTaskListAsync()
{
    var httpClient = new HttpClient();
    var requestUri = "https://graph.microsoft.com/v1.0/me/todo/lists/tasks/tasks";

    var getRequest = new HttpRequestMessage(HttpMethod.Get, requestUri);
    await ProviderManager.Instance.GlobalProvider.AuthenticateRequestAsync(getRequest);

    using (httpClient)
    {
        var response = await httpClient.SendAsync(getRequest);

        if (response.IsSuccessStatusCode)
        {
            var jsonResponse = await response.Content.ReadAsStringAsync();
            var jObject = JObject.Parse(jsonResponse);
            if (jObject.ContainsKey("value"))
            {
                var tasks = JsonConvert.DeserializeObject<List<TodoTask>>(jObject["value"].ToString());
                return tasks;
            }
        }
    }

    return null;
}

That's all you need to get started!

You can use the ProviderManager.Instance to listen to changes in authentication status with the ProviderStateChanged event or get direct access to the .NET Graph Beta API through ProviderManager.Instance.GlobalProvider.GetBetaClient(), just be sure to check if the GlobalProvider has been set first and its State is SignedIn:

using CommunityToolkit.Authentication;
using CommunityToolkit.Graph.Extensions;

public ImageSource GetMyPhoto()
{
    IProvider provider = ProviderManager.Instance.GlobalProvider;
    
    if (provider?.State == ProviderState.SignedIn)
    {
        // Get the beta client
        GraphServiceClient betaGraphClient = provider.GetBetaClient();

        try
        {
            // Make a request to the beta endpoint for the current user's photo.
            var photoStream = await betaGraphClient.Me.Photo.Content.Request().GetAsync();

            using var ras = photoStream.AsRandomAccessStream();
            var bitmap = new BitmapImage();
            await bitmap.SetSourceAsync(ras);

            return bitmap;
        }
        catch
        {
        }
    }

    return null;
}

Feedback and Requests

Please use GitHub Issues for bug reports and feature requests.

Principles

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community. For more information see the .NET Foundation Code of Conduct.

.NET Foundation

This project is supported by the .NET Foundation.

More Repositories

1

WindowsCommunityToolkit

The Windows Community Toolkit is a collection of helpers, extensions, and custom controls. It simplifies and demonstrates common developer tasks building .NET apps with UWP and the Windows App SDK / WinUI 3 for Windows 10 and Windows 11. The toolkit is part of the .NET Foundation.
C#
5,887
star
2

dotnet

.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.
C#
3,007
star
3

Maui

The .NET MAUI Community Toolkit is a community-created library that contains .NET MAUI Extensions, Advanced UI/UX Controls, and Behaviors to help make your life as a .NET MAUI developer easier
C#
2,203
star
4

MVVM-Samples

Sample repo for MVVM package
1,129
star
5

Lottie-Windows

Lottie-Windows is a library (and related tools) for rendering Lottie animations on Windows 10 and Windows 11.
C#
627
star
6

Windows

Collection of controls for WinUI 2, WinUI 3, and Uno Platform developers. Simplifies and demonstrates common developer tasks building experiences for Windows with .NET.
C#
538
star
7

Maui.Markup

The .NET MAUI Markup Community Toolkit is a community-created library that contains Fluent C# Extension Methods to easily create your User Interface in C#
C#
474
star
8

Microsoft.Toolkit.Win32

ARCHIVE - This repository contained XAML Islands wrapper controls and tooling for XAML Islands with WinUI 2, see readme for more info about XAML Islands with WinUI 3 and the WindowsAppSDK.
C#
381
star
9

Labs-Windows

A safe space to collaborate and engineer solutions from the prototyping stage all the way through polished finalized component for the Windows Community Toolkit.
C#
324
star
10

ColorCode-Universal

This is a port of ColorCode to .NET Standard. The original Html only formatter has been separated from the Logic, so now it can produce Syntax Highlighted code for any output. This Project can currently produce HTML, and Render to UWP RichTextBlocks.
C#
216
star
11

Maui.NativeLibraryInterop

Maui.NativeLibraryInterop is a community-created library of binding samples to help .NET MAUI developers interop with native libraries more easily
C#
170
star
12

Datasync

A collection of libraries that implement a client-server system used for synchronizing data from a cloud-based database.
C#
45
star
13

SceneLoader

A library for creating 3D Windows.UI.Composition.Scenes from glTF.
C#
36
star
14

Tooling-Windows-Submodule

Community Toolkit infrastructure for use as a submodule 'tooling' directory in other repositories.
C#
30
star
15

Sample-TabView-TearOff

Sample demonstrating using the TabView UWP control in the Windows Community Toolkit and Windows Template Studio's Multiple View feature to 'tear-off' tabs to create windows and drag them between windows.
C#
27
star
16

Labs-IntelligentAPIs

Intelligent APIs aim to make machine learning (ML) tasks easier for UWP developers to leverage in their applications without needing ML expertise or creating a new model.
C#
21
star
17

announcements

Subscribe to this repo to be notified of Announcements and changes in the Windows Community Toolkit
13
star
18

Sample-Graph-ContosoNotes

A simple note taking app infused with the power of Microsoft Graph and built with the Windows Community Toolkit!
C#
13
star
19

design-assets

Windows Community Toolkit design guide and templates
10
star
20

PWAWindowsHelpers

Helpers for Progressive Windows Apps running on Windows
JavaScript
9
star
21

WindowsCommunityToolkit-wiki

Wiki fork for main WindowsCommunityToolkit repo with project Documentation and Guidance
8
star
22

github-bot-uwp-toolkit

GitHub bot for the UWP Community Toolkit project to manage issues and Pull Requests
TypeScript
7
star
23

ToolkitLabs.dev

Repository for https://toolkitlabs.dev/ site
JavaScript
6
star
24

Labs-GazeControls

Set of user controls that can be reused in different applications with eye gaze input.
C#
6
star
25

Sample-Windows-CppWinRT

Sample which demonstrates using the Windows Community Toolkit within a C++/WinRT UWP application
C++
5
star
26

ReleaseNotesGenerator

dotnet core console app that generates release notes from pull requests for the UWP Community Toolkit
C#
4
star