Friday 14 November 2014

Set another user's profile properties with CSOM

So a while ago, writing User Profile properties via the CSOM was made possible. Vesa Juvonen has a great post about it here:

http://blogs.msdn.com/b/vesku/archive/2014/11/07/sharepoint-user-profile-properties-now-writable-with-csom.aspx

I have put together some code which will allow a Tenant Administrator to modify the User Profile Properties of another user in the tenant. This could be useful if a batch job has to be performed and profile properties have to be set (or updated) for all the users in a Tenant. The code is only to set the properties of one user but you can easily modify it to do the same for multiple users.

This code can also be found as a part of the Office 365 Patterns and Practices code samples on GitHub:
https://github.com/OfficeDev/PnP/tree/master/Samples/UserProfile.Manipulation.CSOM.Console

You will need the AccountName of the user whose profile properties you want to modify.  To get the AccountName of all the user's in a Tenant, you can use People search.

Some points to note with this approach:

1) You need to know the credentials of the Tenant Admin

2) I have tried using this code in a Provider Hosted App with App Only Policy but it does not seem to work. It only works in a Console Application for now.

3) This is only available in SharePoint Online for now and not SharePoint 2013 On-Premises.

4) You will need the 16.0.0.0 version of the following libraries:
 Microsoft.SharePoint.Client.dll
 Microsoft.SharePoint.Client.Runtime.dll
 Microsoft.SharePoint.Client.UserProfiles.dll

The code:

1) Set Single Value Profile Property of another user:




2) Set Multiple Value Profile Property of another user:




8 comments:

Mike said...

Hi Vardhaman,
Is there a JSOM way of doing this?

Vardhaman Deshpande said...

Hi Mike,

I do not think there is a JSOM way to do this.

That is because you will need the tenant admin context and the tenant admin site resides on a different domain. (https://yourtenant-admin.sharepoint.com)

So effectively you will need to do a cross-domain call which is not permitted on the tenant admin site.

gokul a g said...

I have written a console app and referenced the O365 SharePoint Client Dlls. I couldnt able to get the properties which has privacy settings "only to me" in the PersonProperties.UserProfileProperties dictionary . I have written code against the admin center and passed the Global admin credentials to authenticate. Please help.

Unknown said...

I have created a Console Application but not able to set the custom properties. I am getting security exception even I am using global admin credentials. Even I tried to run GetMyProperties, the result is same getting XMLException.

Please let me know if you have any clue.

Thanks
Humbir

Unknown said...

Hi Vardhaman,

Thanks for your above post. I used the above code to set a property for a user and got access denied error for Admin user. But as Admin user, I am able to login to Office 365 and go to User Profiles, open the properties in browser and set values in UI. For the same user, using code I got access denied error. Please guide me.


Thanks & Regards
Avinash

Vardhaman Deshpande said...

Hi Avinash,

Can you check if the property's Default privacy setting is set to "Everyone" and not "Only Me"?

Unknown said...

Hi Vardhaman.

First, thanks for posting this.

While I can easily use this to update my profile, I get the same permission error when updating others. I've checked and privacy is set to everyone. I checked and uncheck user can override. Still the permission error. Using the same admin account, I can make change to other users via the web portal, so it doesn't appear account related.

Has anyone overcome this problem?

Unknown said...

Art Alexion, make sure you are connecting to SharePoint admin site:
string tenantAdministrationUrl = "https://yourtenant-admin.sharepoint.com/";

It will throw access denied if you are using context of some ordinary site.