Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Graph.Beta" Version="4.0.0-preview" />
<PackageReference Include="Microsoft.Graph" Version="4.0.0-preview.1" />
<PackageReference Include="Microsoft.Graph.Core" Version="2.0.0-preview.8" />
</ItemGroup>

Expand Down
40 changes: 40 additions & 0 deletions CommunityToolkit.Net.Graph/Extensions/GraphExtensions.People.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Threading.Tasks;
using Microsoft.Graph;

namespace CommunityToolkit.Net.Graph.Extensions
{
/// <summary>
/// People focused extension methods to the Graph SDK used by the controls and helpers.
/// </summary>
public static partial class GraphExtensions
{
/// <summary>
/// Shortcut to perform a person query.
/// </summary>
/// <param name="graph">Instance of the <see cref="GraphServiceClient"/>.</param>
/// <param name="query">User to search for.</param>
/// <returns><see cref="IUserPeopleCollectionPage"/> collection of <see cref="Person"/>.</returns>
public static async Task<IUserPeopleCollectionPage> FindPersonAsync(this GraphServiceClient graph, string query)
{
try
{
return await graph
.Me
.People
.Request()
.Search(query)
.WithScopes(new string[] { "people.read" })
.GetAsync();
}
catch
{
}

return new UserPeopleCollectionPage();
}
}
}
126 changes: 126 additions & 0 deletions CommunityToolkit.Net.Graph/Extensions/GraphExtensions.Users.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.IO;
using System.Threading.Tasks;
using Microsoft.Graph;

namespace CommunityToolkit.Net.Graph.Extensions
{
/// <summary>
/// User focused extension methods to the Graph SDK used by the controls and helpers.
/// </summary>
public static partial class GraphExtensions
{
/// <summary>
/// Retrieve the curernt user.
/// </summary>
/// <param name="graph">Instance of the <see cref="GraphServiceClient"/>.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public static async Task<User> GetMeAsync(this GraphServiceClient graph)
{
try
{
return await graph.Me.Request().GetAsync();
}
catch
{
}

return null;
}

/// <summary>
/// Retrieve a user by id.
/// </summary>
/// <param name="graph">Instance of the <see cref="GraphServiceClient"/>.</param>
/// <param name="userId">The is of the user to retrieve.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public static async Task<User> GetUserAsync(this GraphServiceClient graph, string userId)
{
try
{
return await graph.Users[userId].Request().GetAsync();
}
catch
{
}

return null;
}

/// <summary>
/// Shortcut to perform a user query.
/// </summary>
/// <param name="graph">Instance of the <see cref="GraphServiceClient"/>.</param>
/// <param name="query">User to search for.</param>
/// <returns><see cref="IGraphServiceUsersCollectionPage"/> collection of <see cref="User"/>.</returns>
public static async Task<IGraphServiceUsersCollectionPage> FindUserAsync(this GraphServiceClient graph, string query)
{
try
{
return await graph
.Users
.Request()
.Filter($"startswith(displayName, '{query}') or startswith(givenName, '{query}') or startswith(surname, '{query}') or startswith(mail, '{query}') or startswith(userPrincipalName, '{query}')")
.WithScopes(new string[] { "user.readbasic.all" })
.GetAsync();
}
catch
{
}

return new GraphServiceUsersCollectionPage();
}

/// <summary>
/// Helper to get the photo of a particular user.
/// </summary>
/// <param name="graph">Instance of the <see cref="GraphServiceClient"/>.</param>
/// <param name="userId">UserID.</param>
/// <returns>Stream with user photo or null.</returns>
public static async Task<Stream> GetUserPhoto(this GraphServiceClient graph, string userId)
{
try
{
return await graph
.Users[userId]
.Photo
.Content
.Request()
.WithScopes(new string[] { "user.readbasic.all" })
.GetAsync();
}
catch
{
}

return null;
}

/// <summary>
/// Get the photo of the current user.
/// </summary>
/// <param name="graph">Instance of the <see cref="GraphServiceClient"/>.</param>
/// <returns>Stream with user photo or null.</returns>
public static async Task<Stream> GetMyPhotoAsync(this GraphServiceClient graph)
{
try
{
return await graph
.Me
.Photo
.Content
.Request()
.WithScopes(new string[] { "user.read" })
.GetAsync();
}
catch
{
}

return null;
}
}
}
86 changes: 5 additions & 81 deletions CommunityToolkit.Net.Graph/Extensions/GraphExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.IO;
using System.Threading.Tasks;
using Microsoft.Graph;

namespace CommunityToolkit.Net.Graph.Extensions
{
/// <summary>
/// Extension methods to the Graph SDK used by the Microsoft.Toolkit.Graph.Controls.
/// Extension methods to the Graph SDK used by the controls and helpers.
/// </summary>
public static class GraphExtensions
public static partial class GraphExtensions
{
/// <summary>
/// Simple method to convert a <see cref="User"/> to a <see cref="Person"/> with basic common properties like <see cref="Entity.Id"/>, <see cref="User.DisplayName"/>, <see cref="Person.EmailAddresses"/>, <see cref="User.GivenName"/>, and <see cref="User.Surname"/> intact.
Expand All @@ -28,9 +26,9 @@ public static Person ToPerson(this User user)

// Standard User Info
DisplayName = user.DisplayName,
EmailAddresses = new RankedEmailAddress[]
ScoredEmailAddresses = new ScoredEmailAddress[]
{
new RankedEmailAddress()
new ScoredEmailAddress()
{
Address = user.Mail ?? user.UserPrincipalName,
},
Expand All @@ -41,85 +39,11 @@ public static Person ToPerson(this User user)
// Company Information
CompanyName = user.CompanyName,
Department = user.Department,
Title = user.JobTitle,
JobTitle = user.JobTitle,
OfficeLocation = user.OfficeLocation,
};
}

/// <summary>
/// Shortcut to perform a person query.
/// </summary>
/// <param name="graph">Instance of the <see cref="GraphServiceClient"/>.</param>
/// <param name="query">User to search for.</param>
/// <returns><see cref="IUserPeopleCollectionPage"/> collection of <see cref="Person"/>.</returns>
public static async Task<IUserPeopleCollectionPage> FindPersonAsync(this GraphServiceClient graph, string query)
{
try
{
return await graph
.Me
.People
.Request()
.Search(query)
.WithScopes(new string[] { "people.read" })
.GetAsync();
}
catch
{
}

return new UserPeopleCollectionPage();
}

/// <summary>
/// Shortcut to perform a user query.
/// </summary>
/// <param name="graph">Instance of the <see cref="GraphServiceClient"/>.</param>
/// <param name="query">User to search for.</param>
/// <returns><see cref="IGraphServiceUsersCollectionPage"/> collection of <see cref="User"/>.</returns>
public static async Task<IGraphServiceUsersCollectionPage> FindUserAsync(this GraphServiceClient graph, string query)
{
try
{
return await graph
.Users
.Request()
.Filter($"startswith(displayName, '{query}') or startswith(givenName, '{query}') or startswith(surname, '{query}') or startswith(mail, '{query}') or startswith(userPrincipalName, '{query}')")
.WithScopes(new string[] { "user.readbasic.all" })
.GetAsync();
}
catch
{
}

return new GraphServiceUsersCollectionPage();
}

/// <summary>
/// Helper to get the photo of a particular user.
/// </summary>
/// <param name="graph">Instance of the <see cref="GraphServiceClient"/>.</param>
/// <param name="userid">UserID.</param>
/// <returns>Stream with user photo or null.</returns>
public static async Task<Stream> GetUserPhoto(this GraphServiceClient graph, string userid)
{
try
{
return await graph
.Users[userid]
.Photo
.Content
.Request()
.WithScopes(new string[] { "user.readbasic.all" })
.GetAsync();
}
catch
{
}

return null;
}

/// <summary>
/// Extension to provider Searching on OData Requests.
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions CommunityToolkit.Net.Graph/Extensions/ProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace CommunityToolkit.Net.Graph.Extensions
public static class ProviderExtensions
{
private static GraphServiceClient _client;
private static GraphServiceClient _betaClient;

static ProviderExtensions()
{
Expand Down Expand Up @@ -46,5 +47,24 @@ public static GraphServiceClient Graph(this IProvider provider)

return _client;
}

/// <summary>
/// Lazily gets a GraphServiceClient instance based on the current GlobalProvider, but configured for the beta endpoint.
/// The beta client instance is cleared whenever the GlobalProvider changes.
/// </summary>
/// <param name="provider">The provider for authenticating Graph calls.</param>
/// <returns>A GraphServiceClient instance configured for the beta endpoint.</returns>
public static GraphServiceClient BetaGraph(this IProvider provider)
{
if (_betaClient == null && provider?.State == ProviderState.SignedIn)
{
_betaClient = new GraphServiceClient("https://graph.microsoft.com/beta", new DelegateAuthenticationProvider(async (requestMessage) =>
{
await provider.AuthenticateRequestAsync(requestMessage);
}));
}

return _betaClient;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Graph" Version="4.0.0-preview.1" />
<PackageReference Include="Microsoft.Toolkit.Uwp.UI" Version="7.0.0" />
<PackageReference Include="Microsoft.Toolkit.Uwp.UI.Controls" Version="7.0.0" />
</ItemGroup>
Expand All @@ -35,7 +36,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Graph.Beta" Version="4.0.0-preview" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,4 @@ private async void GraphPresenter_Loaded(object sender, RoutedEventArgs e)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private async void LoadData()
{
// https://github.com/microsoftgraph/microsoft-graph-toolkit/blob/master/src/components/mgt-login/mgt-login.ts#L139
// TODO: Batch with photo request later? https://github.com/microsoftgraph/msgraph-sdk-dotnet-core/issues/29
UserDetails = await provider.Graph().Me.Request().GetAsync();
UserDetails = await provider.Graph().GetMeAsync();
}
catch (Exception e)
{
Expand Down
Loading