Skip to content

CommunityToolkit/Graph-Controls

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Repository files navigation

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

PackageMin Supported
CommunityToolkit.AuthenticationNetStandard 2.0
CommunityToolkit.Authentication.MsalNetStandard 2.0, UWP, .NET 6, .NET 6 Windows 10.0.17763.0, .NET Core 3.1
CommunityToolkit.Authentication.UwpUWP Windows 10.0.17134.0
CommunityTookit.GraphNetStandard 2.0
CommunityToolkit.Graph.UwpUWP 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:

    usingCommunityToolkit.Authentication;stringclientId="YOUR-CLIENT-ID-HERE";string[]scopes=newstring[]{"User.Read"};ProviderManager.Instance.GlobalProvider=newMsalProvider(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:

    usingCommunityToolkit.Authentication;string[]scopes=newstring[]{"User.Read"};ProviderManager.Instance.GlobalProvider=newWindowsProvider(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.

usingCommunityToolkit.Authentication;usingCommunityToolkit.Graph.Extensions;ProviderManager.Instance.ProviderStateChanged+=OnProviderStateChanged;voidOnProviderStateChanged(objectsender,ProviderStateChangedEventArgsargs){varprovider=ProviderManager.Instance.GlobalProvider;if(provider?.State==ProviderState.SignedIn){vargraphClient=provider.GetClient();varme=awaitgraphClient.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:

usingCommunityToolkit.Authentication;usingNewtonsoft.Json;usingNewtonsoft.Json.Linq;privateasyncTask<IList<TodoTask>>GetDefaultTaskListAsync(){varhttpClient=newHttpClient();varrequestUri="https://graph.microsoft.com/v1.0/me/todo/lists/tasks/tasks";vargetRequest=newHttpRequestMessage(HttpMethod.Get,requestUri);awaitProviderManager.Instance.GlobalProvider.AuthenticateRequestAsync(getRequest);using(httpClient){varresponse=awaithttpClient.SendAsync(getRequest);if(response.IsSuccessStatusCode){varjsonResponse=awaitresponse.Content.ReadAsStringAsync();varjObject=JObject.Parse(jsonResponse);if(jObject.ContainsKey("value")){vartasks=JsonConvert.DeserializeObject<List<TodoTask>>(jObject["value"].ToString());returntasks;}}}returnnull;}

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:

usingCommunityToolkit.Authentication;usingCommunityToolkit.Graph.Extensions;publicImageSourceGetMyPhoto(){IProviderprovider=ProviderManager.Instance.GlobalProvider;if(provider?.State==ProviderState.SignedIn){// Get the beta clientGraphServiceClientbetaGraphClient=provider.GetBetaClient();try{// Make a request to the beta endpoint for the current user's photo.varphotoStream=awaitbetaGraphClient.Me.Photo.Content.Request().GetAsync();usingvarras=photoStream.AsRandomAccessStream();varbitmap=newBitmapImage();awaitbitmap.SetSourceAsync(ras);returnbitmap;}catch{}}returnnull;}

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.