Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ListDeletedUsers method to UsersUtility #956

Merged
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
65 changes: 65 additions & 0 deletions docs/api/PnP.Framework.Graph.UsersUtility.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ items:
children:
- PnP.Framework.Graph.UsersUtility.GetUser(System.String,System.Guid,System.String[],System.Int32,System.Nullable{System.Int32},System.Int32,System.Int32,System.Boolean,System.Boolean)
- PnP.Framework.Graph.UsersUtility.GetUser(System.String,System.String,System.String[],System.Int32,System.Nullable{System.Int32},System.Int32,System.Int32,System.Boolean,System.Boolean)
- PnP.Framework.Graph.UsersUtility.ListDeletedUsers(System.String,System.String[],System.Int32,System.Int32,System.Boolean,PnP.Framework.AzureEnvironment)
- PnP.Framework.Graph.UsersUtility.ListUserDelta(System.String,System.String,System.String,System.String,System.String[],System.Int32,System.Nullable{System.Int32},System.Int32,System.Int32,System.Boolean,System.Boolean)
- PnP.Framework.Graph.UsersUtility.ListUsers(System.String,System.String,System.String,System.String[],System.Int32,System.Nullable{System.Int32},System.Int32,System.Int32,System.Boolean,System.Boolean)
- PnP.Framework.Graph.UsersUtility.ListUsers(System.String,System.String[],System.Int32,System.Nullable{System.Int32},System.Int32,System.Int32,System.Boolean,System.Boolean)
Expand Down Expand Up @@ -187,6 +188,65 @@ items:
- Shared
fullName.vb: PnP.Framework.Graph.UsersUtility.GetUser(System.String, System.String, System.String(), System.Int32, System.Nullable(Of System.Int32), System.Int32, System.Int32, System.Boolean, System.Boolean)
name.vb: GetUser(String, String, String(), Int32, Nullable(Of Int32), Int32, Int32, Boolean, Boolean)
- uid: PnP.Framework.Graph.UsersUtility.ListDeletedUsers(System.String,System.String[],System.Int32,System.Int32,System.Boolean,PnP.Framework.AzureEnvironment)
commentId: M:PnP.Framework.Graph.UsersUtility.ListDeletedUsers(System.String,System.String[],System.Int32,System.Int32,System.Boolean,PnP.Framework.AzureEnvironment)
id: ListDeletedUsers(System.String,System.String[],System.Int32,System.Int32,System.Boolean,PnP.Framework.AzureEnvironment)
parent: PnP.Framework.Graph.UsersUtility
langs:
- csharp
- vb
name: ListDeletedUsers(System.String,System.String[],System.Int32,System.Int32,System.Boolean,PnP.Framework.AzureEnvironment)
nameWithType: UsersUtility.ListDeletedUsers(System.String, System.String[], System.Int32, System.Int32, System.Boolean, PnP.Framework.AzureEnvironment)
fullName: PnP.Framework.Graph.UsersUtility.ListDeletedUsers(System.String, System.String[], System.Int32, System.Int32, System.Boolean, PnP.Framework.AzureEnvironment)
type: Method
source:
remote:
path: src/lib/PnP.Framework/Graph/UsersUtility.cs
branch: dev
repo: https://github.com/pkbullock/pnpframework.git
id: ListUsers
path: ../src/lib/PnP.Framework/Graph/UsersUtility.cs
startLine: 84
assemblies:
- PnP.Framework
namespace: PnP.Framework.Graph
summary: "\nReturns deleted Users in the current domain\n"
example: []
syntax:
content: public static List<User> ListDeletedUsers(string accessToken, string[] selectProperties = null, int retryCount = 10, int delay = 500, bool ignoreDefaultProperties = false, AzureEnvironment azureEnvironment = AzureEnvironment.Production)
parameters:
- id: accessToken
type: System.String
description: The OAuth 2.0 Access Token to use for invoking the Microsoft Graph
- id: selectProperties
type: System.String[]
description: Allows providing the names of properties to return regarding the users. If not provided, the standard properties will be returned.
- id: retryCount
type: System.Int32
description: Number of times to retry the request in case of throttling
- id: delay
type: System.Int32
description: Milliseconds to wait before retrying the request. The delay will be increased (doubled) every retry.
- id: ignoreDefaultProperties
type: System.Boolean
description: If set to true, only the properties provided through selectProperties will be loaded. The default properties will not be. Optional. Default is that the default properties will always be retrieved.
- id: azureEnvironment
type: PnP.Framework.AzureEnvironment
description: The type of environment to connect to
return:
type: System.Collections.Generic.List{PnP.Framework.Graph.Model.User}
description: List with User objects
content.vb: Public Shared Function ListDeletedUsers(accessToken As String, selectProperties As String() = Nothing, retryCount As Integer = 10, delay As Integer = 500, ignoreDefaultProperties As Boolean = False, azureEnvironment As AzureEnvironment = AzureEnvironment.Production) As List(Of User)
overload: PnP.Framework.Graph.UsersUtility.ListDeletedUsers*
nameWithType.vb: UsersUtility.ListDeletedUsers(String, String(), Int32, Int32, Int32, Boolean, AzureEnvironment)
modifiers.csharp:
- public
- static
modifiers.vb:
- Public
- Shared
fullName.vb: PnP.Framework.Graph.UsersUtility.ListDeletedUsers(System.String, System.String(), System.Int32, System.Int32, System.Boolean, PnP.Framework.AzureEnvironment)
name.vb: ListUsers(String, String(), Int32, Int32, Boolean, AzureEnvironment)
- uid: PnP.Framework.Graph.UsersUtility.ListUsers(System.String,System.String[],System.Int32,System.Nullable{System.Int32},System.Int32,System.Int32,System.Boolean,System.Boolean)
commentId: M:PnP.Framework.Graph.UsersUtility.ListUsers(System.String,System.String[],System.Int32,System.Nullable{System.Int32},System.Int32,System.Int32,System.Boolean,System.Boolean)
id: ListUsers(System.String,System.String[],System.Int32,System.Nullable{System.Int32},System.Int32,System.Int32,System.Boolean,System.Boolean)
Expand Down Expand Up @@ -912,6 +972,11 @@ references:
name: PnP.Framework.Graph.Model
nameWithType: PnP.Framework.Graph.Model
fullName: PnP.Framework.Graph.Model
- uid: PnP.Framework.Graph.UsersUtility.ListDeletedUsers*
commentId: Overload:PnP.Framework.Graph.UsersUtility.ListDeletedUsers
name: ListDeletedUsers
nameWithType: UsersUtility.ListDeletedUsers
fullName: PnP.Framework.Graph.UsersUtility.ListDeletedUsers
- uid: PnP.Framework.Graph.UsersUtility.ListUsers*
commentId: Overload:PnP.Framework.Graph.UsersUtility.ListUsers
name: ListUsers
Expand Down
70 changes: 70 additions & 0 deletions src/lib/PnP.Framework/Graph/UsersUtility.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.Graph;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PnP.Framework.Diagnostics;
using PnP.Framework.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -293,6 +295,74 @@ public static Model.UserDelta ListUserDelta(string accessToken, string deltaToke
return result;
}

/// <summary>
/// Returns deleted Users in the current domain
/// </summary>
/// <param name="accessToken">The OAuth 2.0 Access Token to use for invoking the Microsoft Graph</param>
/// <param name="selectProperties">Allows providing the names of properties to return regarding the users. If not provided, the standard properties will be returned.</param>
/// <param name="retryCount">Number of times to retry the request in case of throttling</param>
/// <param name="delay">Milliseconds to wait before retrying the request. The delay will be increased (doubled) every retry.</param>
/// <param name="ignoreDefaultProperties">If set to true, only the properties provided through selectProperties will be loaded. The default properties will not be. Optional. Default is that the default properties will always be retrieved.</param>
/// /// <param name="azureEnvironment">The type of environment to connect to</param>
/// <returns>List with User objects</returns>
public static List<Model.User> ListDeletedUsers(string accessToken, string[] selectProperties = null, int retryCount = 10, int delay = 500, bool ignoreDefaultProperties = false, AzureEnvironment azureEnvironment = AzureEnvironment.Production)
{
if (String.IsNullOrEmpty(accessToken))
{
throw new ArgumentNullException(nameof(accessToken));
}
// Rewrite AdditionalProperties to Additional Data
var propertiesToSelect = ignoreDefaultProperties ? new List<string>() : new List<string> { "BusinessPhones", "DisplayName", "GivenName", "JobTitle", "Mail", "MobilePhone", "OfficeLocation", "PreferredLanguage", "Surname", "UserPrincipalName", "Id", "AccountEnabled", "DeletedDateTime" };

selectProperties = selectProperties?.Select(p => p == "AdditionalProperties" ? "AdditionalData" : p).ToArray();

if(selectProperties != null)
{
foreach(var property in selectProperties)
{
if(!propertiesToSelect.Contains(property))
{
propertiesToSelect.Add(property);
}
}
}

List<Model.User> result = null;
try
{
// Use a synchronous model to invoke the asynchronous process
result = Task.Run(async () =>
{
List<Model.User> users = new List<Model.User>();
var requestUrl = $"{GraphHttpClient.GetGraphEndPointUrl(azureEnvironment)}directory/deleteditems/microsoft.graph.user";
if (propertiesToSelect.Count > 0)
{
requestUrl += $"?$select={string.Join(",", propertiesToSelect)}";
}

var responseAsString = HttpHelper.MakeGetRequestForString(requestUrl, accessToken, retryCount: retryCount, delay: delay);

var response = JToken.Parse(responseAsString);
var deletedUsers = response["value"];

foreach (var deletedUser in deletedUsers)
{
var user = deletedUser.ToObject<User>();
var modelUser = MapUserEntity(user, selectProperties);
users.Add(modelUser);
}

return users;
}).GetAwaiter().GetResult();
}
catch (ServiceException ex)
{
Log.Error(Constants.LOGGING_SOURCE, CoreResources.GraphExtensions_ErrorOccured, ex.Error.Message);
throw;
}
return result;
}

/// <summary>
/// Maps a Graph User result to a local User model
/// </summary>
Expand Down