Monday, 1 December 2014

Write and Get User Profile Properties in SharePoint Online with CSOM in PowerShell

A couple of weeks ago Vesa Juvonen wrote a post about the new capability in CSOM that is letting us to write user profile properties. This feature is already enabled in all Office 365 tenants.
The features are available in Microsoft.SharePoint.Client.UserProfiles.dll assembly version 16. Here is the moment to mention that if you download the latest CSOM package you will receive client dlls version 15 and version 16. In my earlier post I wrote about using CSOM in powershell I mentioned that the client dlls are located in "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI", well there are version 15 dlls that can be used against SharePoint 2013 on-premise. But if you want to use the latest and greatest features against SharePoint Online you should load the version 16 dlls located in "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI".
The new features are coming  from two new methods in PeopleManager class, SetSingleValueProfileProperty() and SetMultiValuedProfileProperty().
In his post Vesa gives example code that demonstrates the capability in SharePoint app. I found this interesting and since I am not a developer I will show you how this works in PowerShell. Unfortunately, I was unable to get SetMultiValuedProfileProperty() working from powershell, I think that this is because of the issue with the generic lists in powershell. However I will show you a function that edits the AboutMe user profile property and function that will get all user properties. I will authenticate against the admin portal of SharePoint online and I will be able to play with the properties of other users.
Below you can see both function(for editing AboutMe and gettin profile properties) and everything I do befor that.

[*UPDATE*]: I have published "universal" scripts for editing any User Profile property read more HERE

$cDLLS = Get-ChildItem -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI" | Where {$_.Name -notlike "*.Portable.dll"}
ForEach ($dll in $cDLLS)
{
    Add-Type -Path $dll.FullName
}


$username = "******@yankulovdemo.onmicrosoft.com" 
$password = "*******" 
$url = "https://yankulovdemo-admin.sharepoint.com"
$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force

$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$Context.Credentials = $credentials
$Global:oContext = $Context

Function Update-AboutText{
Param(
    [string]$AboutText,
    [string]$UserLogin
)
Process{
    $logIn = ("i:0#.f|membership|" + $UserLogin)
    $ctx = $Global:oContext
    $aboutTextHtml = $AboutText.Replace(([System.Environment]::NewLine), "<br />")
    $peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)
    $Properties = $peopleManager.GetPropertiesFor($logIn)
    $ctx.Load($Properties)
    $ctx.ExecuteQuery()
    $peopleManager.SetSingleValueProfileProperty($logIn, "AboutMe", $AboutText)
    $ctx.ExecuteQuery()
}
}

Function Get-UserProperties{
Param(
    [string]$UserLogin
)
Process{
    $logIn = ("i:0#.f|membership|" + $UserLogin)
    $ctx = $Global:oContext
    $peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)
    $user = $peopleManager.GetPropertiesFor($logIn)
    $ctx.Load($user)
    $ctx.ExecuteQuery()
    Write-Output $user.UserProfileProperties

}
}


The first thing I do is to load the client dlls. Second, I am getting the client context, as I said against the admin portal and putting it as global variable for further reuse.
After we are ready we can start editing the properties and here is the outcome:

Write and Get User Profile Property with CSOM

It seems that this worked out. The updated About Me property is shown in Gencho's profile as well.

He is the master of SharePoint



10 comments:

  1. This is extremely helpful. Using this as a template, I was able to update any Single Value Property including Custom Properties.
    You are a genius at helping PowerShell leverage CSOM. Thank you.

    ReplyDelete
    Replies
    1. Thank you for the comment. Glad you found it useful!

      Delete
  2. Dear Ivan, this is a great script and thank you. I was wondering if you could suggestion how to change this script to change language settings for mysite. Or, do you know where I can find a script that would do that?

    ReplyDelete
    Replies
    1. Hi,
      The display language preference is User Profile property "SPS-MUILanguages".
      It is a single value property string property. Let's say that the value of the property is "bg-BG,en-US,de-DE" this means that the language order will be 1.Bulgarian(bg-BG),2. English US(en-US) and 3.German(de-DE), If you want English to be top language for the profile you will set value for this property to "en-US,bg-BG,de-DE". This is the same list of languages you see when you go to About Me -> Edit -> Language and Region. I personally have not used this with customers, but I have just tested it and it works(need some time to kick off), however I think that this language preference can be overridden in case of O365 language setting is changed. See "https://support.office.com/en-nz/article/Change-your-language-and-region-settings-caa1fccc-bcdb-42f3-9e5b-45957647ffd7". I am hoping that this is what you need.

      Delete
    2. PS: I may soon publish an article how it is done, I think it will be useful. Please, reply if this was what you were looking for.

      Delete
    3. I published a script for writing any property
      http://spyankulov.blogspot.com/2015/05/write-and-get-user-profile-properties-the-scripts.html

      Delete
    4. Any suggestion on how to modify your get script to find all profiles that have set Dutch as the preferred language in SPS-MUILanguages?

      Delete
    5. Yes, you just need list of all users than do foreach of user using the Get script http://www.sptrenches.com/2015/05/write-and-get-user-profile-properties-the-scripts.html and filter all users that have *nl-be* (not sure if that is the right code) in the SPS-MUILanguages

      Delete
  3. Hi there, I got an access denied Error code, Do you know how to set this is in the Sharepoint's App Manifiest.xml.
    Thanks! :)

    ReplyDelete
  4. Hi Michael,

    Maybe you can see how it is configured in this PnP sample https://github.com/SharePoint/PnP/tree/dev/Samples/UserProfile.Manipulation.CSOM

    Let me know if that helps

    ReplyDelete