1

We want to update the customValue user profile property using powershell sharepoint online. The code updates only for one account '[email protected]' who is the globalk admin of the site. But none of other users, any suggestions

Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
               $username = "[email protected]" 
               $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 Answer 1

1

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

$userEmailToCheck = "[email protected],[email protected]"
$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 "[email protected]" -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()
    }   
}
5
  • Thanks but how to update the user profile property?
    – SandeshR
    Sep 7, 2016 at 17:53
  • like you were doing, updated Sep 7, 2016 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.":
    – SandeshR
    Sep 7, 2016 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 Sep 7, 2016 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.
    – SandeshR
    Sep 7, 2016 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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