In SharePoint on-premise, we know that administrators are able to configure the synchronization of values from different sources to SharePoint User Profile application; however, it is quite different in synchronization of values from Azure Active Directory (AAD) to the SharePoint User Profile Service Application (UPA).

While working in SharePoint Online project, I implemented a very interesting task to sync a property from Azure Active Directory to SharePoint Online.

In SharePoint Online, you can see User Profile properties of a user ("SharePoint Admin Centre > User Profiles > Manage User Profiles > Edit User Profile") as below.

Sync A Property From Azure Active Directory To SharePoint Online Using PowerShell
Sync A Property From Azure Active Directory To SharePoint Online Using PowerShell

First, let’s understand the Azure Active Directory (AAD) mailbox's structure and the custom attributes (Go to Exchange Admin -> mailboxes).

Sync A Property From Azure Active Directory To SharePoint Online Using PowerShell

Double-click the username (in my case, it was Vipul Jain). Then, a window will open where we can set the Custom Attribute or property.

Sync A Property From Azure Active Directory To SharePoint Online Using PowerShell
Sync A Property From Azure Active Directory To SharePoint Online Using PowerShell

In my requirement, I needed to update a custom property, i.e., Circle, available in SharePoint Online with the value of the above custom attribute value (I took the 5th attribute, so in PowerShell code, we need to specify CustomAttribute5).

Sync A Property From Azure Active Directory To SharePoint Online Using PowerShell
Fig: Initially Circle Property is Blank

Prerequisites

To run the PowerShell, install the following.

NOTE
If the PowerShell version is less than or equal to 3, then update the PowerShell version.

In my case, below is the screenshot of the PowerShell version.

Sync A Property From Azure Active Directory To SharePoint Online Using PowerShell

Steps for running the PowerShell

Once the above steps are executed in SharePoint Online Management Shell, run the below PowerShell code to update the “Circle” property.

  1. Import-Module MSOnline
  2. Import-Module Microsoft.Online.SharePoint.PowerShell
  3. # add SharePoint CSOM libraries
  4. Import-Module 'C:\Program Files\Common Files\Microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
  5. Import-Module 'C:\Program Files\Common Files\Microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
  6. Import-Module 'C:\Program Files\Common Files\Microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll'
  7. # Defaults
  8. $spoAdminUrl = https://tenant-admin.sharepoint.com
  9. $overwriteExistingSPOUPAValue = "True"
  10. # Get credentials of account that is AzureAD Admin and SharePoint Online Admin
  11. $credential = Get-Credential
  12. Try {
  13. # Connect to AzureAD
  14. Connect-MsolService -Credential $credential
  15. # Get credentials for SharePointOnline
  16. $spoCredentials=New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.GetNetworkCredential().Username, (ConvertTo-SecureString $credential.GetNetworkCredential().Password -AsPlainText -Force))
  17. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($spoAdminUrl)
  18. $ctx.Credentials = $spoCredentials
  19. $spoPeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)
  20. # Get all AzureAD Users
  21. $AzureADUsers = Get-MSolUser -All
  22. ForEach ($AzureADUser in $AzureADUsers) {
  23. $targetUPN = $AzureADUser.UserPrincipalName.ToString()
  24. if ($targetUPN -eq "user_name") {
  25. $displayName =(get-mailbox $targetUPN).CustomAttribute5
  26. $targetUPN = $AzureADUser.UserPrincipalName.ToString()
  27. $targetSPOUserAccount = ("i:0#.f|membership|" + $targetUPN)
  28. # Check to see if the AzureAD User has a displayName specified
  29. if (!([string]::IsNullOrEmpty($displayName))) {
  30. # Get the existing value of the SPO User Profile Property Circle
  31. $targetUserTestCircle = $spoPeopleManager.GetUserProfilePropertyFor($targetSPOUserAccount, "Circle")
  32. $ctx.ExecuteQuery()
  33. $userTestCircle = $targetUserTestCircle.Value
  34. # If target property is empty let's populate it
  35. if ([string]::IsNullOrEmpty($userTestCircle)) {
  36. $targetspoUserAccount= ("i:0#.f|membership|" + $AzureADUser.UserPrincipalName.ToString())
  37. $spoPeopleManager.SetSingleValueProfileProperty($targetspoUserAccount, "Circle", $displayName)
  38. $ctx.ExecuteQuery()
  39. }
  40. else {
  41. # Target property is not empty
  42. # Check to see if we're to overwrite existing property value
  43. if ($overwriteExistingSPOUPAValue -eq "True") {
  44. $targetspoUserAccount = ("i:0#.f|membership|" + $AzureADUser.UserPrincipalName.ToString())
  45. $spoPeopleManager.SetSingleValueProfileProperty($targetspoUserAccount, "Circle", $displayName)
  46. $ctx.ExecuteQuery()
  47. }
  48. else {
  49. # Not going to overwrite existing property value
  50. Write-Output "Target SPO UPA Circle is not empty for $targetUPN and we're to preserve existing properties"
  51. }
  52. }
  53. }
  54. else {
  55. # AzureAD User displayName is empty, nothing to do here
  56. Write-Output "AzureAD displayName Property is Null or Empty for $targetUPN"
  57. }
  58. }
  59. }
  60. }
  61. Catch {
  62. [Exception]
  63. echo $_.Exception.GetType().FullName, $_.Exception.Message
  64. }

NOTE
Update the SharePoint tenant details, custom attribute number, and custom SharePoint user profile property name in the above code based on your requirement.

Here is the important command used in the above code.

Get-Mailbox
This cmdlet is used to view the mailbox objects and attributes, populate property pages, or supply mailbox information to other tasks.

Output

Once the above PowerShell code is executed in SharePoint Online Management Shell, the property is updated as shown below.

Sync A Property From Azure Active Directory To SharePoint Online Using PowerShell

Summary

In this article, we studied how we can update a custom user profile property in SharePoint Online (Office 365) from Azure Active Directory using PowerShell. In continuation to this, in the next article, I will write about how we can schedule the PowerShell script using Windows Task Scheduler.

Happy Coding!!