2021-05-13 01:16:07 +03:00
# Windows Community Toolkit - Graph Helpers and Controls
2019-09-13 05:12:28 +03:00
2021-05-13 01:16:07 +03:00
Welcome! This is a sub-repo for the [Windows Community Toolkit ](https://aka.ms/wct ) focused on [Microsoft Graph ](https://developer.microsoft.com/en-us/graph/ ) providing a set of Helpers and Controls for netstandard and UWP apps.
2019-09-13 05:12:28 +03:00
2021-05-13 01:16:07 +03:00
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.
2019-09-13 05:29:46 +03:00
2019-09-13 05:12:28 +03:00
If you need similar controls for the Web, please use the [Microsoft Graph Toolkit ](https://aka.ms/mgt ).
## <a name="supported"></a> Supported SDKs
2019-09-19 10:21:28 +03:00
2021-05-15 00:45:42 +03:00
| Package | Min Supported |
|--|--|
2021-05-27 21:41:58 +03:00
| `CommunityToolkit.Authentication` | NetStandard 2.0 |
2021-10-26 01:16:57 +03:00
| `CommunityToolkit.Authentication.Msal` | NetStandard 2.0, UWP, .NET 5, .NET 5 Windows 10.0.17763.0, .NET Core 3.1 |
| `CommunityToolkit.Authentication.Uwp` | UWP Windows 10.0.17134.0 |
2021-05-27 21:41:58 +03:00
| `CommunityTookit.Graph` | NetStandard 2.0 |
2021-10-26 01:16:57 +03:00
| `CommunityToolkit.Graph.Uwp` | UWP Windows 10.0.17763.0 |
2019-09-13 05:12:28 +03:00
2021-05-24 23:04:54 +03:00
## Samples
2021-05-26 20:13:25 +03:00
Check out our samples for getting started with authentication providers and making calls to Microsoft Graph:
2021-05-24 23:04:54 +03:00
- [UwpWindowsProviderSample ](./Samples/UwpWindowsProviderSample )
- [UwpMsalProviderSample ](./Samples/UwpMsalProviderSample )
2021-10-26 01:16:57 +03:00
- [WpfNetCoreMsalProviderSample ](./Samples/WpfNetCoreMsalProviderSample )
- [WpfNetMsalProviderSample ](./Samples/WpfNet5WindowsMsalProviderSample )
2021-05-27 21:41:58 +03:00
- [ManualGraphRequestSample ](./Samples/ManualGraphRequestSample )
2021-05-24 23:04:54 +03:00
2021-09-15 20:41:41 +03:00
### Contoso Notes Sample
[Contoso Notes ](https://github.com/CommunityToolkit/Sample-Graph-ContosoNotes ) is a premier sample note-taking app infused with Graph powered features and controls from the Windows Community Toolkit, demonstrated in practical application scenarios.
2019-09-13 05:12:28 +03:00
## <a name="documentation"></a> Getting Started
2019-09-19 10:21:28 +03:00
2021-05-13 01:16:07 +03:00
To get started using Graph data in your application, you'll first need to enable authentication.
2019-09-19 10:21:28 +03:00
2021-05-13 01:16:07 +03:00
### 1A. Setup authentication with MSAL
2019-09-19 10:21:28 +03:00
2021-05-13 01:16:07 +03:00
Leverage the official Microsoft Authentication Library (MSAL) to enable authentication in any NetStandard application.
2019-09-19 10:21:28 +03:00
2021-05-13 01:16:07 +03:00
1. Register your app in Azure AAD
Before requesting data from [Microsoft Graph ](https://graph.microsoft.com ), you will need to [register your application ](https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-app ) to get a **ClientID** .
2019-09-19 10:21:28 +03:00
2021-05-13 01:16:07 +03:00
> 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".
2019-09-19 10:21:28 +03:00
2021-05-27 21:41:58 +03:00
3. Install the `CommunityToolkit.Authentication.Msal` package.
2021-05-13 01:16:07 +03:00
4. Set the GlobalProvder to a new instance of MsalProvider with clientId and pre-configured scopes:
```csharp
2021-05-27 21:41:58 +03:00
using CommunityToolkit.Authentication;
2019-11-01 23:11:12 +03:00
2021-05-13 01:16:07 +03:00
string clientId = "YOUR-CLIENT-ID-HERE";
string[] scopes = new string[] { "User.Read" };
2019-09-19 10:21:28 +03:00
2021-05-13 01:16:07 +03:00
ProviderManager.Instance.GlobalProvider = new MsalProvider(clientId, scopes);
```
2019-10-23 05:01:53 +03:00
2021-05-13 01:16:07 +03:00
> 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.
2019-09-19 10:21:28 +03:00
2021-05-13 01:16:07 +03:00
### 1B. Setup authentication with WindowsProvider
2019-09-19 10:21:28 +03:00
2021-05-13 01:16:07 +03:00
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.
2021-09-02 03:24:51 +03:00
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 ](https://docs.microsoft.com/windows/communitytoolkit/graph/authentication/windows ) for more details.
1. Install the `CommunityToolkit.Authentication.Uwp` package.
2021-05-13 01:16:07 +03:00
1. Set the GlobalProvider to a new instance of WindowsProvider with pre-configured scopes:
```csharp
2021-05-27 21:41:58 +03:00
using CommunityToolkit.Authentication;
2021-05-13 01:16:07 +03:00
string[] scopes = new string[] { "User.Read" };
ProviderManager.Instance.GlobalProvider = new WindowsProvider(scopes);
```
2021-05-27 21:41:58 +03:00
### 2. Make a Graph request with the Graph SDK
2021-05-13 01:16:07 +03:00
Once you are authenticated, you can then make requests to the Graph using the GraphServiceClient instance.
2021-05-27 21:41:58 +03:00
> Install the `CommunityToolkit.Graph` package.
2021-05-13 01:16:07 +03:00
2021-06-04 21:32:19 +03:00
```csharp
2021-05-27 21:41:58 +03:00
using CommunityToolkit.Authentication;
using CommunityToolkit.Graph.Extensions;
2019-09-19 10:21:28 +03:00
2021-06-11 23:18:45 +03:00
ProviderManager.Instance.ProviderStateChanged += OnProviderStateChanged;
2019-09-19 10:21:28 +03:00
2021-06-11 23:18:45 +03:00
void OnProviderStateChanged(object sender, ProviderStateChangedEventArgs args)
2019-09-19 10:21:28 +03:00
{
2021-09-02 03:24:51 +03:00
var provider = ProviderManager.Instance.GlobalProvider;
2021-06-11 23:18:45 +03:00
if (provider?.State == ProviderState.SignedIn)
2021-06-04 21:32:19 +03:00
{
var graphClient = provider.GetClient();
var me = await graphClient.Me.Request().GetAsync();
}
2019-09-19 10:21:28 +03:00
}
```
2021-05-27 21:41:58 +03:00
#### 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:
2021-06-04 21:32:19 +03:00
```csharp
2021-05-27 21:41:58 +03:00
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;
}
```
2021-05-13 01:16:07 +03:00
**That's all you need to get started!**
2019-09-19 10:21:28 +03:00
2021-06-11 20:28:54 +03:00
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 ](https://github.com/microsoftgraph/msgraph-beta-sdk-dotnet ) through `ProviderManager.Instance.GlobalProvider.GetBetaClient()` , just be sure to check if the `GlobalProvider` has been set first and its `State` is `SignedIn` :
2021-05-13 01:16:07 +03:00
```csharp
2021-05-27 21:41:58 +03:00
using CommunityToolkit.Authentication;
using CommunityToolkit.Graph.Extensions;
2021-05-13 01:16:07 +03:00
2021-05-15 00:45:42 +03:00
public ImageSource GetMyPhoto()
{
IProvider provider = ProviderManager.Instance.GlobalProvider;
2021-05-13 01:16:07 +03:00
2021-05-15 00:45:42 +03:00
if (provider?.State == ProviderState.SignedIn)
{
// Get the beta client
GraphServiceClient betaGraphClient = provider.GetBetaClient();
2021-05-13 01:16:07 +03:00
try
{
2021-05-15 00:45:42 +03:00
// Make a request to the beta endpoint for the current user's photo.
2021-05-13 01:16:07 +03:00
var photoStream = await betaGraphClient.Me.Photo.Content.Request().GetAsync();
2021-05-15 00:45:42 +03:00
2021-05-13 01:16:07 +03:00
using var ras = photoStream.AsRandomAccessStream();
var bitmap = new BitmapImage();
await bitmap.SetSourceAsync(ras);
2021-05-15 00:45:42 +03:00
2021-05-13 01:16:07 +03:00
return bitmap;
}
catch
{
}
}
2021-06-04 21:32:19 +03:00
return null;
2021-05-13 01:16:07 +03:00
}
```
2019-09-13 05:12:28 +03:00
## Build Status
2019-09-19 10:21:28 +03:00
| Target | Branch | Status | Recommended package version |
2019-09-13 05:12:28 +03:00
| ------ | ------ | ------ | ------ |
2021-07-19 22:12:16 +03:00
| Pre-release beta testing | main | [![Build Status ](https://dev.azure.com/dotnet/CommunityToolkit/_apis/build/status/CommunityToolkit.Graph-Controls?branchName=main )](https://dev.azure.com/dotnet/WindowsCommunityToolkit/_build/latest?definitionId=102& branchName=main) | [![MyGet ](https://img.shields.io/dotnet.myget/uwpcommunitytoolkit/vpre/Microsoft.Toolkit.Graph.svg )](https://dotnet.myget.org/gallery/uwpcommunitytoolkit) |
2019-09-13 05:12:28 +03:00
## Feedback and Requests
2021-07-19 22:12:16 +03:00
Please use [GitHub Issues ](https://github.com/CommunityToolkit/Graph-Controls/issues ) for bug reports and feature requests.
2019-09-13 05:12:28 +03:00
## Principles
This project has adopted the code of conduct defined by the [Contributor Covenant ](http://contributor-covenant.org/ )
to clarify expected behavior in our community.
For more information see the [.NET Foundation Code of Conduct ](http://dotnetfoundation.org/code-of-conduct ).
## .NET Foundation
This project is supported by the [.NET Foundation ](http://dotnetfoundation.org ).