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

Allow requesting specific base user profile props #3247

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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
### Fixed

- Fixed `Add-PnPContentTypeToList` cmdlet to better handle piped lists. [#3244](https://github.com/pnp/powershell/pull/3244)
- Fixed `Get-PnPUserProfileProperty` cmdlet not allowing basic user profile properties to be retrieved using `-Properties` [#3247](https://github.com/pnp/powershell/pull/3247)

### Contributors

Expand Down
18 changes: 16 additions & 2 deletions documentation/Get-PnPUserProfileProperty.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ You must connect to the tenant admin website (https://\<tenant\>-admin.sharepoin
## SYNTAX

```powershell
Get-PnPUserProfileProperty -Account <String[]> [-Properties <String[]>] [-Connection <PnPConnection>]
Get-PnPUserProfileProperty -Account <String[]> [-Properties <String[]>] [-Verbose] [-Connection <PnPConnection>]
```

## DESCRIPTION
Expand Down Expand Up @@ -93,6 +93,20 @@ Accept pipeline input: False
Accept wildcard characters: False
```

### -Verbose
When provided, additional debug statements will be shown while executing the cmdlet.

```yaml
Type: SwitchParameter
Parameter Sets: (All)

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

## RELATED LINKS

[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
127 changes: 94 additions & 33 deletions src/Commands/UserProfiles/GetUserProfileProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,73 +20,134 @@ public class GetUserProfileProperty : PnPAdminCmdlet

protected override void ExecuteCmdlet()
{
// All the basic profile profile properties that only exist on the PersonProperties object
var basicProperties = new string[] { "AccountName", "DirectReports", "DisplayName", "Email", "ExtendedManagers", "ExtendedReports", "IsFollowed", "LatestPost", "Peers", "PersonalSiteHostUrl", "PersonalUrl", "PictureUrl", "Title", "UserUrl" };

var peopleManager = new PeopleManager(AdminContext);

foreach (var acc in Account)
// Loop through each of the requested users
foreach (var account in Account)
{
var currentAccount = acc;
var result = Tenant.EncodeClaim(currentAccount);
WriteVerbose($"Getting user profile properties for {account}");
var result = Tenant.EncodeClaim(account);
AdminContext.ExecuteQueryRetry();
currentAccount = result.Value;
var currentAccount = result.Value;

WriteVerbose($"Account {account} encoded to {currentAccount}");

SortedDictionary<string, object> upsDictionary = new();

if (ParameterSpecified(nameof(Properties)) && Properties != null && Properties.Length > 0)
// Check if specific user profile properties have been requested
if (ParameterSpecified(nameof(Properties)) && Properties != null && Properties.Length > 0 && Properties.All(p => !basicProperties.Contains(p)))
{
// Specific user profile properties have been requested and none of them are basic user profile properties, return only those properties that have been requested
WriteVerbose($"Retrieving {Properties.Length} specific non basic user profile {(Properties.Length != 1 ? "properties" : "property")} for account {currentAccount}");

UserProfilePropertiesForUser userProfilePropertiesForUser = new UserProfilePropertiesForUser(AdminContext, currentAccount, Properties);
var userRequestedProperties = peopleManager.GetUserProfilePropertiesFor(userProfilePropertiesForUser);
AdminContext.Load(userProfilePropertiesForUser);
AdminContext.ExecuteQueryRetry();

// Add all the requested extended user profile properties to the output
for (var i = 0; i < userRequestedProperties.Count(); i++)
{
object propertyValue = userRequestedProperties.ElementAt(i);
upsDictionary.Add(Properties[i], propertyValue);
}

WriteObject(upsDictionary, true);

}
else
{
// No specific user profile properties have been requested or there were basic user profile properties amongst them
WriteVerbose($"Retrieving all user profile properties for {currentAccount}");

var userProfileProperties = peopleManager.GetPropertiesFor(currentAccount);
AdminContext.Load(userProfileProperties);
AdminContext.ExecuteQueryRetry();

upsDictionary.Add("AccountName", userProfileProperties.AccountName);
upsDictionary.Add("DirectReports", userProfileProperties.DirectReports);
upsDictionary.Add("DisplayName", userProfileProperties.DisplayName);
upsDictionary.Add("Email", userProfileProperties.Email);
upsDictionary.Add("ExtendedManagers", userProfileProperties.ExtendedManagers);
upsDictionary.Add("ExtendedReports", userProfileProperties.ExtendedReports);
upsDictionary.Add("IsFollowed", userProfileProperties.IsFollowed);
upsDictionary.Add("LatestPost", userProfileProperties.LatestPost);
upsDictionary.Add("Peers", userProfileProperties.Peers);
upsDictionary.Add("PersonalSiteHostUrl", userProfileProperties.PersonalSiteHostUrl);
upsDictionary.Add("PersonalUrl", userProfileProperties.PersonalUrl);
upsDictionary.Add("PictureUrl", userProfileProperties.PictureUrl);
upsDictionary.Add("Title", userProfileProperties.Title);
upsDictionary.Add("UserUrl", userProfileProperties.UserUrl);

if (userProfileProperties.UserProfileProperties != null && userProfileProperties.UserProfileProperties.Count > 0)
// Check if we only need to output specific properties or all of them
if (ParameterSpecified(nameof(Properties)) && Properties != null && Properties.Length > 0)
{
for (var i = 0; i < userProfileProperties.UserProfileProperties.Count; i++)
WriteVerbose($"Adding specific {Properties.Length} user profile {(Properties.Length != 1 ? "properties" : "property")} to the output");

// Check if any of the base user profile properties have been requested and if so, add them to the output as well
if (Properties.Contains("AccountName")) upsDictionary.Add("AccountName", userProfileProperties.AccountName);
if (Properties.Contains("DirectReports")) upsDictionary.Add("DirectReports", userProfileProperties.DirectReports);
if (Properties.Contains("DisplayName")) upsDictionary.Add("DisplayName", userProfileProperties.DisplayName);
if (Properties.Contains("Email")) upsDictionary.Add("Email", userProfileProperties.Email);
if (Properties.Contains("ExtendedManagers")) upsDictionary.Add("ExtendedManagers", userProfileProperties.ExtendedManagers);
if (Properties.Contains("ExtendedReports")) upsDictionary.Add("ExtendedReports", userProfileProperties.ExtendedReports);
if (Properties.Contains("IsFollowed")) upsDictionary.Add("IsFollowed", userProfileProperties.IsFollowed);
if (Properties.Contains("LatestPost")) upsDictionary.Add("LatestPost", userProfileProperties.LatestPost);
if (Properties.Contains("Peers")) upsDictionary.Add("Peers", userProfileProperties.Peers);
if (Properties.Contains("PersonalSiteHostUrl")) upsDictionary.Add("PersonalSiteHostUrl", userProfileProperties.PersonalSiteHostUrl);
if (Properties.Contains("PersonalUrl")) upsDictionary.Add("PersonalUrl", userProfileProperties.PersonalUrl);
if (Properties.Contains("PictureUrl")) upsDictionary.Add("PictureUrl", userProfileProperties.PictureUrl);
if (Properties.Contains("Title")) upsDictionary.Add("Title", userProfileProperties.Title);
if (Properties.Contains("UserUrl")) upsDictionary.Add("UserUrl", userProfileProperties.UserUrl);

// Add the extended user profile properties to the output which have been specified in Properties
if (userProfileProperties.UserProfileProperties != null && userProfileProperties.UserProfileProperties.Count > 0)
{
var element = userProfileProperties.UserProfileProperties.ElementAt(i);
if (!upsDictionary.ContainsKey(element.Key))
for (var i = 0; i < userProfileProperties.UserProfileProperties.Count; i++)
{
upsDictionary.Add(element.Key, element.Value);
var element = userProfileProperties.UserProfileProperties.ElementAt(i);

// Check if this property should be included in the output, if not continue with the next property
if(!Properties.Contains(element.Key)) continue;

if (!upsDictionary.ContainsKey(element.Key))
{
upsDictionary.Add(element.Key, element.Value);
}
else
{
upsDictionary[element.Key] = element.Value;
}
}
else
}
}
else
{
// Add all of the basic user profile properties to the output
WriteVerbose("Adding all user profile properties to the output");

upsDictionary.Add("AccountName", userProfileProperties.AccountName);
upsDictionary.Add("DirectReports", userProfileProperties.DirectReports);
upsDictionary.Add("DisplayName", userProfileProperties.DisplayName);
upsDictionary.Add("Email", userProfileProperties.Email);
upsDictionary.Add("ExtendedManagers", userProfileProperties.ExtendedManagers);
upsDictionary.Add("ExtendedReports", userProfileProperties.ExtendedReports);
upsDictionary.Add("IsFollowed", userProfileProperties.IsFollowed);
upsDictionary.Add("LatestPost", userProfileProperties.LatestPost);
upsDictionary.Add("Peers", userProfileProperties.Peers);
upsDictionary.Add("PersonalSiteHostUrl", userProfileProperties.PersonalSiteHostUrl);
upsDictionary.Add("PersonalUrl", userProfileProperties.PersonalUrl);
upsDictionary.Add("PictureUrl", userProfileProperties.PictureUrl);
upsDictionary.Add("Title", userProfileProperties.Title);
upsDictionary.Add("UserUrl", userProfileProperties.UserUrl);

// Add all the extended user profile properties to the output
if (userProfileProperties.UserProfileProperties != null && userProfileProperties.UserProfileProperties.Count > 0)
{
for (var i = 0; i < userProfileProperties.UserProfileProperties.Count; i++)
{
upsDictionary[element.Key] = element.Value;
var element = userProfileProperties.UserProfileProperties.ElementAt(i);
if (!upsDictionary.ContainsKey(element.Key))
{
upsDictionary.Add(element.Key, element.Value);
}
else
{
upsDictionary[element.Key] = element.Value;
}
}
}
}

WriteObject(upsDictionary, true);
}

// Write the collected properties to the output stream
WriteObject(upsDictionary, true);
}
}
}
}
}
Loading