1

I am trying to update the user profile properties (CellPhone for example) using the below code in SharePoint online by power-shell.

Mysite URL

$site = 'site url'

Admin User Principal Name

$admin = 'user login'

Get Password as secure String

$password = "Password" |ConvertTo-SecureString -AsPlainText -force

Get the Client Context and Bind the Site Collection

$context = New-Object Microsoft.SharePoint.Client.ClientContext($site)

Authenticate

$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($admin , $password)
$context.Credentials = $credentials

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)


$userprofile = $people.GetPropertiesFor("useraccount")
$context.Load($userprofile)
$context.ExecuteQuery()


$people.SetSingleValueProfileProperty($userprofile.AccountName, "CellPhone", "1234567890")
$context.ExecuteQuery()

I've created a trial O365 SharePoint site and I'm the admin of the site.

I'm able to update my phone no. using the above code but getting access denied for any other user.

I've tried to update the properties of an external user(Hotmail) that I've added in my site also I tried with a test user account that I created with the same domain of my site. I'm getting access denied for both.

Any suggestions?

  • 1
    Do you provide the tenant admin credentials? – users1100 Aug 5 '16 at 5:05
  • Thank you for the reply.I just resolved this by connecting to the admin console ie. mysite-admin.sharepoint.com earlier i was trying to connect to my root site so getting access denied. – MJaiswal Aug 5 '16 at 5:59
  • i could change the phone no. but i want to change the profile picture, so in the line: $people.SetSingleValueProfileProperty($userprofile.AccountName, "CellPhone", "1234567890") i can use pictureURl instead of cellphone but what value do i provide in the url – MJaiswal Aug 5 '16 at 6:02
  • If you have resolved it please post it as an answer and ask a new question for your new issue – Robert Lindgren Aug 5 '16 at 6:10
1

I could resolve the issue admin console:

$site = ''https://mysite-admin.sharepoint.com'
Admin User Principal Name

$admin = 'user login'
Get Password as secure String

$password = "Password" |ConvertTo-SecureString -AsPlainText -force
Get the Client Context and Bind the Site Collection

$context = New-Object Microsoft.SharePoint.Client.ClientContext($site)
Authenticate

$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($admin , $password)
$context.Credentials = $credentials
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)


$userprofile = $people.GetPropertiesFor("useraccount")
$context.Load($userprofile)
$context.ExecuteQuery()


$people.SetSingleValueProfileProperty($userprofile.AccountName, "CellPhone", "1234567890")
$context.ExecuteQuery()

Use https://mysite-admin.sharepoint.com in the site url instead of root site

https://mysite.sharepoint.com/sites/Testsite and you will not get access denied while updating other user's profile, provided you should be the global admin

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.