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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.SharePoint.Client; | |
using Microsoft.SharePoint.Client.UserProfiles; | |
using System.Security; | |
namespace UserProfile.Manipulation.CSOM.Console | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Tenant Admin Details | |
string tenantAdministrationUrl = "https://yourtenant-admin.sharepoint.com/"; | |
string tenantAdminLoginName = "admin@yourtenant.onmicrosoft.com"; | |
string tenantAdminPassword = "Password"; | |
//AccountName of the user whose property you want to update. | |
//If you want to update properties of multiple users, you can fetch the accountnames through search. | |
string UserAccountName = "i:0#.f|membership|anotheruser@yourtenant.onmicrosoft.com"; | |
using (ClientContext clientContext = new ClientContext(tenantAdministrationUrl)) | |
{ | |
SecureString passWord = new SecureString(); | |
foreach (char c in tenantAdminPassword.ToCharArray()) passWord.AppendChar(c); | |
clientContext.Credentials = new SharePointOnlineCredentials(tenantAdminLoginName, passWord); | |
// Get the people manager instance for tenant context | |
PeopleManager peopleManager = new PeopleManager(clientContext); | |
// Update the AboutMe property for the user using account name. | |
peopleManager.SetSingleValueProfileProperty(UserAccountName, "AboutMe", "Value updated from CSOM"); | |
clientContext.ExecuteQuery(); | |
} | |
} | |
} | |
} |
2) Set Multiple Value Profile Property of another user:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.SharePoint.Client; | |
using Microsoft.SharePoint.Client.UserProfiles; | |
using System.Collections.Generic; | |
using System.Security; | |
namespace UserProfile.Manipulation.CSOM.Console | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Tenant Admin Details | |
string tenantAdministrationUrl = "https://yourtenant-admin.sharepoint.com/"; | |
string tenantAdminLoginName = "admin@yourtenant.onmicrosoft.com"; | |
string tenantAdminPassword = "Password"; | |
//AccountName of the user whose property you want to update. | |
//If you want to update properties of multiple users, you can fetch the accountnames through search. | |
string UserAccountName = "i:0#.f|membership|anotheruser@yourtenant.onmicrosoft.com"; | |
using (ClientContext clientContext = new ClientContext(tenantAdministrationUrl)) | |
{ | |
SecureString passWord = new SecureString(); | |
foreach (char c in tenantAdminPassword.ToCharArray()) passWord.AppendChar(c); | |
clientContext.Credentials = new SharePointOnlineCredentials(tenantAdminLoginName, passWord); | |
// Multiple values | |
List<string> skills = new List<string>() { "SharePoint", "Office 365", "C#", "JavaScript" }; | |
// Get the people manager instance for tenant context | |
PeopleManager peopleManager = new PeopleManager(clientContext); | |
// Update the SPS-Skills property for the user using account name from profile. | |
peopleManager.SetMultiValuedProfileProperty(UserAccountName, "SPS-Skills", skills); | |
clientContext.ExecuteQuery(); | |
} | |
} | |
} | |
} |
8 comments:
Hi Vardhaman,
Is there a JSOM way of doing this?
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.
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.
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
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
Hi Avinash,
Can you check if the property's Default privacy setting is set to "Everyone" and not "Only Me"?
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?
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.
Post a Comment