1

We want to update the customValue user profile property using powershell sharepoint online. The code updates only for one account 'GlobalAdminUser@Test.onmicrosoft.com' who is the globalk admin of the site. But none of other users, any suggestions

Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
               $username = "GlobalAdminUser@Test.onmicrosoft.com" 
               $Adminurl = "https://Test-admin.sharepoint.com"
               $siteColUrl = "https://Test.sharepoint.com"


            try
             {
               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.UserProfiles.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"


               #Bind to site collection
               $password = Read-Host -Prompt "Enter password" -AsSecureString 
               $Context = New-Object Microsoft.SharePoint.Client.ClientContext($siteColUrl)
               #$ContextAdmin = New-Object Microsoft.SharePoint.Client.ClientContext($UserProfurl)

               $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username,$Password)
               $Context.Credentials = $Creds
               Write-Host "Successfully connected.." -ForegroundColor Green


               #Retrieve lists
               $web = $Context.Web
               $Users = $Context.Web.SiteUsers
               $items = $list.GetItems($query)
               $Context.Load($Users)
               $Context.Load($web)

               $Context.ExecuteQuery()
               $CustomValue  =10
               Write-Host $list.Title
               $PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Context)
               Write-Host $items.Count

                Foreach ($User in $Users)
                {  
                  $UserNameValue =  $User.LoginName 
                  $UserProfile = $PeopleManager.GetPropertiesFor($UserNameValue)
                  $context.Load($UserProfile)
                  $Context.ExecuteQuery()
                      $PeopleManager.SetSingleValueProfileProperty($UserProfile.AccountName,"CustomValue", $CustomValue)
        $Context.ExecuteQuery() 
    }
    }
1

Last I knew, you had to do something like this:

$userEmailToCheck = "user1@dommain.com,user2@domain.com"
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext("https://tenant-admin.sharepoint.com")
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName, $credential.Password)
$web = $ctx.Web

$ctx.Load($web)
$ctx.ExecuteQuery()

$Users = $userEmailToCheck.Split(",")

$PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)
$loader =[Microsoft.SharePoint.Client.UserProfiles.ProfileLoader]::GetProfileLoader($ctx)

#To Get Profile
$profile = $loader.GetUserProfile()
$ctx.Load($profile)
$ctx.ExecuteQuery()


foreach($User in $Users){
  $UserProfile = $PeopleManager.GetPropertiesFor("i:0#.f|membership|"+$User)
  $ctx.Load($UserProfile)
  $ctx.ExecuteQuery()
  $PeopleManager.SetSingleValueProfileProperty($UserProfile.AccountName,"CustomValue", $CustomValue)
  $ctx.ExecuteQuery()      
}

I have another function I've used more recently that looks like this:

function Set-UPSValue {
        <#
        .SYNOPSIS
        Updates a SharePoint user profile property
        .DESCRIPTION    
        Updates a SharePoint user profile property
        .PARAMETER Email
        The users email address
        .PARAMETER Property
        The SharePoint Porperty to update
        .PARAMETER Value
        The property value
        .EXAMPLE
        Set-UPSValue -Email "wp.test01@domain.com" -Property "Fax" -Value "567-555-1234"
    #>
    Param(
        [Parameter(Mandatory=$true,HelpMessage="The user email address",Position=0)][ValidateNotNull()]
        [string]$Email,

        [Parameter(Mandatory=$true,HelpMessage="The property to update",Position=1)][ValidateNotNull()]
        [string]$Property,

        [Parameter(Mandatory=$true,HelpMessage="The property value",Position=2)][ValidateNotNull()]
        [string]$Value
    )
    Begin {
        #Get the Client Context and Bind the Site Collection
        $context = New-Object Microsoft.SharePoint.Client.ClientContext($CAProd)

        #Authenticate
        $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName, $credential.Password)

        #Fetch the users in Site Collection
        $users = $context.Web.SiteUsers
        $context.Load($users)
        $context.ExecuteQuery()

        #Create an Object [People Manager] to retrieve profile information
        $people = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($context)
    }
    Process {
        try{
            $user = "i:0#.f|membership|"+$Email
            $userprofile = $people.GetPropertiesFor($user)
            $context.Load($userprofile)
            $context.ExecuteQuery()

            $people.SetSingleValueProfileProperty($userprofile.AccountName, $Property, $Value)
            $context.ExecuteQuery()
        }
        catch{
            $ErrorMessage = $_.Exception.Message
            write-host $ErrorMessage
        }
    }
    End {
        $context.Dispose()
    }   
}
  • Thanks but how to update the user profile property? – spStacker Sep 7 '16 at 17:53
  • like you were doing, updated – Eric Alexander Sep 7 '16 at 18:10
  • the script that have in question works fine if the user for whom we are updating is a global admin but for the rest of the users it is throwing this error ...Exception calling "ExecuteQuery" with "0" argument(s): Access denied. You do not have permission to perform this action or access this resource.": – spStacker Sep 7 '16 at 18:13
  • 1
    this is what i used about a year ago to do some bulk operations, it is all about the binding of contexts, when doing it in your case, you bind as the user and can only update their profile, this binds to a different context that allows you to manipulate other users data. I don't reacll the source atm – Eric Alexander Sep 7 '16 at 18:28
  • Thanks Eric for the reply .. the problem was with the context. I had to use 2 context ..one for sharepoint site(getting list) and other context pointing to the Sharepoint admin site. The sharepoint admin site context is used for the updating of user profile while the sharepoint site context used for the getting list data. – spStacker Sep 7 '16 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.