Introduction
In this article, you will learn how to retrieve SharePoint user profile properties programmatically in different ways using CSOM with PowerShell on SharePoint 2013 / SharePoint online.
Steps Involved
The following prerequisites need to be executed before going for any operations using CSOM PowerShell on SharePoint sites.
- Add the references using the Add-Type command with necessary reference paths. The necessary references are Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll and UserProfile dll.
- Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
- Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
- Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"
- Initialize client context object with the site URL.
- $siteURL = ""
- $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
- If you are trying to access SharePoint Online site, then you need to setup the site credentials with credentials parameter and get it set to the client context.
- #Setting Credentials for o365 - Start
- $userId = ""
- $pwd = Read-Host -Prompt "Enter password" -AsSecureString
- $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
- $ctx.credentials = $creds
- #Setting Credentials for o365 - End
- If you are trying to access the SharePoint on premise site, then the credentials parameter is not required to be set to the context. But you need to run the code on the respective SharePoint server or you need to pass the network credentials and set the context.
- #Credentials for on premise site - Start
- $pwd = Read-Host -Prompt "Enter password" -AsSecureString
- $creds = New-Object System.Net.NetworkCredential("", $pwd)
- $ctx.Credentials = $creds
- #Credentials for on premise site - End
Retrieve Current User Properties
- Initialize the people manager object with the context to get the user profile data.
- With people manager object, use GetMyProperties() method to retrieve current user profile information.
- Load the object and execute query to get the data.
- In the below code snippet,
- $myProperties variable holds few important user profile properties and whole collection as property
- $myprofileProperties holds whole user profile properties collection which is retrieved from above variable.
- These variables are key value pairs. Foreach loop can be used to retrieve the data.
- # Get My Profile Properties
- function GetMyProperties(){
- try{
- $peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)
- $myProperties = $peopleManager.GetMyProperties()
- $ctx.Load($myProperties)
- $ctx.ExecuteQuery()
- # Gets all the user profile properties related to current user
- $myprofileProperties = $myProperties.UserProfileProperties
- foreach($key in $myprofileProperties.Keys){
- Write-Host $Key " : " $myprofileProperties[$key]
- }
- }
- catch{
- Write-Host "$($_.Exception.Message)" -foregroundcolor red
- }
- }
Retrieve Target User Profile Properties
In the above section, we have seen how we can retrieve the profile properties of the user who is running the script. Now we will see how we can retrieve target user profile properties. The steps followed are very similar to the above section. Only the method is changed to get the target user properties.
- To get the target user profile information, we can use GetPropertiesFor() method along with people manager object and user id
- # Get Properties of target User
- function GetUserProperties(){
- try{
- $peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)
- # Gets few user profile
- $userProperties = $peopleManager.GetPropertiesFor("i:0#.f|membership|[email protected]") #o365
- #$userProperties = $peopleManager.GetPropertiesFor("domain\userid") #on prem
- $ctx.Load($userProperties)
- $ctx.ExecuteQuery()
- # Gets all the user profile properties for target user
- $userprofileProperties = $userProperties.UserProfileProperties
- foreach($key in $userprofileProperties.Keys){
- Write-Host $Key " : " $userprofileProperties[$key]
- }
- }
- catch{
- Write-Host "$($_.Exception.Message)" -foregroundcolor red
- }
- }
Retrieve Few User Profile Properties
In the above section, we have seen how we can retrieve particular user profile information. Now, we will see how we can set/select the property names and retrieve the specific user information for any user.
- Initialize the people manager object with the context to get the user profile data.
- In the array, set the necessary property names to be retrieved.
- Then initialize the UserProfilePropertiesForUser method, where we can set the context, target user id and the properties array defined.
- With people manager object, use GetUserProfilePropertiesFor method with above object to retrieve current user profile information.
- Load the object and execute query to get the data.
- # Get Few Properties of target User
- function GetUserProfileProperties(){
- try{
- $peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)
- $properties = "PreferredName", "Department", "Title", "HomePhone", "Department"
- $userProfileProperties = New-Object Microsoft.SharePoint.Client.UserProfiles.UserProfilePropertiesForUser($ctx, "i:0#.f|membership|[email protected]", $properties) #o365
- #$userProfileProperties = New-Object Microsoft.SharePoint.Client.UserProfiles.UserProfilePropertiesForUser($ctx, "domain\userid", $properties) #on-prem
-
- $userProfileValues = $peopleManager.GetUserProfilePropertiesFor($userProfileProperties)
- $ctx.Load($userProfileProperties)
- $ctx.ExecuteQuery()
- $counter = 0
- foreach($property in $properties){
- Write-Host $property " : " $userProfileValues[$counter]
- $counter++
- }
- }
- catch{
- Write-Host "$($_.Exception.Message)" -foregroundcolor Red
- }
- }
Note: The above set of operations needs to be called to get it executed.
- GetMyProperties # Get My Profile Properties
- GetUserProperties # Get Properties of target User
- GetUserProfileProperties # Get Few Properties of target User
Summary
Thus you have learned how to retrieve user profile properties for any user by various approaches using CSOM with PowerShell on SharePoint 2013 / SharePoint online sites.