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(); | |
} | |
} | |
} | |
} |