SharePoint Online: Sync User Profile Property from Azure AD using PowerShell

Requirement: Sync User Profile Property from Azure Active Directory to SharePoint Online using PowerShell

How to Sync a User Profile Property in SharePoint Online?
In a typical SharePoint Online environment, The user profile synchronization process imports user profiles from On-Premises AD to Azure (through AD Sync Tool) and then from the Azure Active Directory (AAD) certain properties are mapped and synchronized with the SharePoint Online User Profiles. Not all the properties from Azure AD are synced to SharePoint Online. I've got a requirement to sync "Mobile Phone" Property to SharePoint Online user profile. Although the Azure AD Sync tool imports "Mobile" property from On-Premises AD, SharePoint doesn't has a mapping for this property!

And the bad news is: currently there are no ways to map and synchronize additional properties from the SharePoint Online Administration page (As we do in SharePoint On-Premises). However, we can loop through the users from Azure AD and update the User Profile Property in SharePoint Online using CSOM PowerShell script.
sharepoint online sync user profile property powershell

PowerShell to Import a User Profile Property in SharePoint Online:
Using the Azure AD PowerShell and the SharePoint Client Side Object Model (CSOM), we can get the user profile property value from Azure AD and update the corresponding properties in the SharePoint Online User Profiles and then schedule this script to run on a regular basis. Let's sync mobile phone from azure active directory to SharePoint Online using PowerShell.
#Load SharePoint CSOM Assemblies
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"

Function Sync-UserProfileProperty()
{
    Param
    (
        [Parameter(Mandatory=$true)] [string] $AdminSiteUrl,
        [Parameter(Mandatory=$true)] [string] $UserPrincipalName,
        [Parameter(Mandatory=$true)] [string] $ADPropertyName,
        [Parameter(Mandatory=$true)] [string] $SPOPropertyName
    )
    #Get Credentials to connect
    $Cred = Get-Credential
 
    Try {
        #Connect to AzureAD
        Connect-AzureAD -Credential $Cred | Out-Null
 
        #Get the User Property from AzureAD
        $AzureADUser = Get-AzureADUser -ObjectId $UserPrincipalName
        $ADUserPropertyValue = $AzureADUser | Select -ExpandProperty $ADPropertyName

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminSiteUrl)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
        #Get User Profile Manager
        $PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Ctx)
  
        #Sync User Profile Property from AD
        $UserAccount = "i:0#.f|membership|$UserPrincipalName"
        $PeopleManager.SetSingleValueProfileProperty($UserAccount, $SPOPropertyName, $ADUserPropertyValue)
        $Ctx.ExecuteQuery()
        Write-host "User Profile Property has been Synched for: $UserPrincipalName" -f Green 
    }
    Catch {
        write-host -f Red "Error Synching User Profile Property!" $_.Exception.Message
    }
}
#Config parameters
$AdminSiteUrl = "https://crecent-admin.sharepoint.com"
$UserPrincipalName = "Salaudeen@crescent.com"
$ADPropertyName = "MobilePhone"
$SPOPropertyName = "CellPhone"

#Call the function to sync a Property from Azure AD to SharePoint Online
Sync-UserProfileProperty -AdminSiteUrl $AdminSiteURL -UserPrincipalName $UserPrincipalName -ADPropertyName $ADPropertyName -SPOPropertyName $SPOPropertyName

This PowerShell script imports a given property from Azure AD to SharePoint Online for a particular user. Make sure you have Azure AD Module installed in your machine prior running this script:
Install-Module AzureAD

PowerShell to Sync User Profile Property Value for All Users
This time, let's sync a user profile property for all users from Azure AD to SharePoint Online using PnP PowerShell. In addition, let's check:
  1. If the AD user profile property has some value (not null!)
  2. If the user profile property in SharePoint Online doesn't has any existing value in it!
#Config Variables
$AdminSiteURL = "https://crescent-admin.sharepoint.com"
$ADPropertyName = "Department"
$SPOPropertyName = "Department"

#Get Credentials to connect to Azure AD and SharePoint Online Admin Center
$Cred = Get-Credential

Try {
    #Connect to AzureAD
    Connect-AzureAD -Credential $Cred | Out-Null

    #Get All Users from AzureAD
    $AllUsers = Get-AzureADUser -All:$True
    Write-host "Total Number of User Profiles Found:"$AllUsers.Count  

    #Connect to PnP Online
    Connect-PnPOnline -Url $AdminSiteURL -Credentials $Cred

    #Iterate through All Users
    $Counter = 1
    ForEach($User in $AllUsers)
    {
        Write-host "`nUpdating User Profile Property for: $($User.UserPrincipalName)" -f Yellow

        #Get the User Property value from Azure AD        
        $ADUserPropertyValue = $User | Select -ExpandProperty $ADPropertyName

        #Check if the AD Property is not Null
        If (!([string]::IsNullOrEmpty($ADUserPropertyValue))) 
        {
            #Get existing User Profile Property from SharePoint
            $UserAccount = "i:0#.f|membership|$($User.UserPrincipalName)"
            $UserProfile = Get-PnPUserProfileProperty -Account $UserAccount
            $UserProfileProperty = $UserProfile.UserProfileProperties[$SPOPropertyName]

            #Check if the Existing SharePoint User Profile Property is Null
            If (([string]::IsNullOrEmpty($UserProfileProperty))) 
            {
                Set-PnPUserProfileProperty -Account $UserAccount -PropertyName $SPOPropertyName -Value $ADUserPropertyValue
                Write-host "`tUpdated User Profile Property for: $($User.UserPrincipalName)" -f Green
            }
            Else
            {
                Write-host "`t Existing Value of the Property in SharePoint is Not Null! Skipping..." -f Yellow
            }
        }
        else
        {
            Write-host "`t AD Value of the Property is Null! Skipping..." -f Yellow
        }
        $Counter++
        Write-Progress -Activity "Updating User Profile Data..." -Status "Updating User Profile $Counter of $($AllUsers.Count)" -PercentComplete (($Counter / $AllUsers.Count)  * 100)
    }
}
Catch {
    write-host -f Red "Error Updating User Profile Property!" $_.Exception.Message
}
Please note, This property update is only for SharePoint Online, it can’t be synced to Office 365 or Azure AD. User profile property created in SharePoint Online admin center will not create or sync that property in the Office 365!
SharePoint Online: Sync User Profile Property from Azure AD using PowerShell SharePoint Online: Sync User Profile Property from Azure AD using PowerShell Reviewed by Salaudeen Rajack on March 02, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.