Showing posts with label UserProfile. Show all posts
Showing posts with label UserProfile. Show all posts

Friday 27 May 2016

Update User Profile Properties On SharePoint Online Using CSOM PowerShell


In this article, you will learn how to update SharePoint user profile properties programmatically in different ways using CSOM with PowerShell on SharePoint online - Office 365 sites. The update property option is available only on Office 365 sites as of now and it is not available on on-premise versions.


Steps Involved:


The following prerequisites need to be executed before going for any operations using CSOM PowerShell on SharePoint sites.
  1. 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. The references should be from 16 folder, since these are latest updates from Microsoft.
    1. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
    2. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
    3. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"  
  2. Initialize client context object with the site URL.
    1. $siteURL = ""  
    2. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  
  3. 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. 
    1. #Setting Credentials for o365 - Start  
    2. $userId = ""  
    3. $pwd = Read-Host -Prompt "Enter password" -AsSecureString  
    4. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  
    5. $ctx.credentials = $creds  
    6. #Setting Credentials for o365 - End  
  4. There are multiple ways of updating the properties of SharePoint user profile. There will be properties with just a value or line of text and properties with collection of values. Two different methods are used to update those properties. Following tells us with examples. 
    • Single Value properties - WorkPhone, Mobile, FirstName, LastName, etc. 
    • Multi Valued properties - Ask Me About, Skills, Interests, etc. 


Update Single Value Properties:

  • Initialize the people manager object with the context to get the user profile data.
  • Using SetSingleValueProfileProperty method set the property for any target user.
  • Load the object and execute query to get the data.
    1. # Update User Profile property (office 365)  
    2. function SetSingleValueProperty(){  
    3.     try{  
    4.         $peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)                  
    5.         $peopleManager.SetSingleValueProfileProperty("i:0#.f|membership|abc@abc.onmicrosoft.com""AboutMe""Your description")  
    6.         $ctx.Load($peopleManager)  
    7.         $ctx.ExecuteQuery()  
    8.         Write-Host "Property updated"  
    9.     }  
    10.     catch{  
    11.         Write-Host "$($_.Exception.Message)" -foregroundcolor Red  
    12.     }  
    13. }  
You can update as many properties as possible. 
  1. $peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)                  
  2. $peopleManager.SetSingleValueProfileProperty("i:0#.f|membership|abc@abc.onmicrosoft.com""AboutMe""Your description")  
  3. $peopleManager.SetSingleValueProfileProperty("i:0#.f|membership|abc@abc.onmicrosoft.com""SPS-Location""Bangalore")  
  4. $ctx.Load($peopleManager)  
  5. $ctx.ExecuteQuery()  
  6. Write-Host "Properties updated"   


Update Multi Valued Properties:

In the above section, we have seen how we can set the single value profile properties for target user. Now we will see how we can set user profile multi valued properties for target user.
  • Initialize the people manager object with the context to get the user profile data.
  • Then initialize the list collection object and set the values in the collection variable.
  • Using SetMultiValuedProfileProperty method, set the multi valued property for any target user. (Here updated ask me about - Responsibility field).
  • Load the object and execute query to get the data.
    1. # Update User Profile Multi Valued properties (office 365)  
    2. function SetMultiValuedProperty(){  
    3.     try{  
    4.         $peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)                  
    5.         $propertyValues =New-Object "System.Collections.Generic.List``1[System.string]"  
    6.         # My responsibilities  
    7.         $propertyValues.Add("SharePoint")  
    8.         $propertyValues.Add("Project1")          
    9.         $propertyValues.Add("Project2")          
    10.         #update value  
    11.         $peopleManager.SetMultiValuedProfileProperty("i:0#.f|membership|abc@abc.onmicrosoft.com""SPS-Responsibility", $propertyValues)  
    12.         $ctx.Load($peopleManager)  
    13.         $ctx.ExecuteQuery()  
    14.     }  
    15.     catch{  
    16.         Write-Host "$($_.Exception.Message)" -foregroundcolor Red  
    17.     }  
    18. }  
The above operation will override (set) the existing property values. If you want to update the existing property values, you need to retrieve the existing value using get operation and then set the properties along with new values.

Note: The above set of operations needs to be called to get it executed.
  1. SetSingleValueProperty # Update User Profile properties (office 365)  
  2. SetMultiValuedProperty # Update User Profile Multi Valued properties (office 365)   

Monday 23 May 2016

Retrieve User Profile properties from SharePoint using CSOM PowerShell


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 2016 / SharePoint online sites.


Steps Involved:


The following prerequisites need to be executed before going for any operations using CSOM PowerShell on SharePoint sites.
  1. 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.
    1. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
    2. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
    3. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"  
  2. Initialize client context object with the site URL.
    1. $siteURL = ""  
    2. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  
  3. 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. 
    1. #Setting Credentials for o365 - Start  
    2. $userId = ""  
    3. $pwd = Read-Host -Prompt "Enter password" -AsSecureString  
    4. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  
    5. $ctx.credentials = $creds  
    6. #Setting Credentials for o365 - End  
  4. 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.
    1. #Credentials for on premise site - Start  
    2. $pwd = Read-Host -Prompt "Enter password" -AsSecureString  
    3. $creds = New-Object System.Net.NetworkCredential("", $pwd)  
    4. $ctx.Credentials = $creds  
    5. #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.
      1. # Get My Profile Properties  
      2. function GetMyProperties(){  
      3.     try{  
      4.         $peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)  
      5.         $myProperties = $peopleManager.GetMyProperties()  
      6.         $ctx.Load($myProperties)  
      7.         $ctx.ExecuteQuery()     
      8.         # Gets all the user profile properties related to current user  
      9.         $myprofileProperties = $myProperties.UserProfileProperties  
      10.         foreach($key in $myprofileProperties.Keys){  
      11.             Write-Host $Key " : " $myprofileProperties[$key]  
      12.         }  
      13.     }  
      14.     catch{  
      15.         Write-Host "$($_.Exception.Message)" -foregroundcolor red   
      16.     }      
      17. }  



Retrieve Target User Profile Properties:


In the above section, we have seen how we can retrieve the profile properties of 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
    1. # Get Properties of target User  
    2. function GetUserProperties(){  
    3.     try{  
    4.         $peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)  
    5.         # Gets few user profile   
    6.         $userProperties = $peopleManager.GetPropertiesFor("i:0#.f|membership|abc@abc.onmicrosoft.com") #o365  
    7.         #$userProperties = $peopleManager.GetPropertiesFor("domain\userid") #on prem  
    8.         $ctx.Load($userProperties)  
    9.         $ctx.ExecuteQuery()     
    10.         # Gets all the user profile properties for target user  
    11.         $userprofileProperties = $userProperties.UserProfileProperties  
    12.         foreach($key in $userprofileProperties.Keys){  
    13.             Write-Host $Key " : " $userprofileProperties[$key]  
    14.         }  
    15.     }  
    16.     catch{  
    17.         Write-Host "$($_.Exception.Message)" -foregroundcolor red   
    18.     }  
    19. }  



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.

    1. # Get Few Properties of target User  
    2. function GetUserProfileProperties(){  
    3.     try{  
    4.         $peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)  
    5.         $properties = "PreferredName""Department""Title""HomePhone""Department"  
    6.         $userProfileProperties = New-Object Microsoft.SharePoint.Client.UserProfiles.UserProfilePropertiesForUser($ctx, "i:0#.f|membership|abc@abc.onmicrosoft.com", $properties) #o365  
    7.         #$userProfileProperties = New-Object Microsoft.SharePoint.Client.UserProfiles.UserProfilePropertiesForUser($ctx, "domain\userid", $properties) #on-prem  
    8.           
    9.         $userProfileValues = $peopleManager.GetUserProfilePropertiesFor($userProfileProperties)  
    10.         $ctx.Load($userProfileProperties)  
    11.         $ctx.ExecuteQuery()  
    12.         $counter = 0  
    13.         foreach($property in $properties){  
    14.             Write-Host $property " : " $userProfileValues[$counter]  
    15.             $counter++  
    16.         }          
    17.     }  
    18.     catch{  
    19.         Write-Host "$($_.Exception.Message)" -foregroundcolor Red  
    20.     }  
    21. }  
Note: The above set of operations needs to be called to get it executed.
  1. GetMyProperties # Get My Profile Properties  
  2. GetUserProperties # Get Properties of target User  
  3. GetUserProfileProperties # Get Few Properties of target User