Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

We have got User profiles in SharePoint with custom properties. We want to programatically insert values into them.

Preferably via javascript object model. Is this possible? Please point to reference links.

Example: We want to save the no. of times users have visited an application page and increment it. This article explains how to fetch value from the user profile. But, there is no reference to save value into the user profile custom property.

share|improve this question

2 Answers 2

up vote 5 down vote accepted

You cannot change any properties in the User Profiles via client OM code (neither .NET nor via JavaScript) with the exception of the user's profile picture.

MSDN - Work with user profiles in SharePoint 2013

Not all functionality that you find in the Microsoft.Office.Server.UserProfiles assembly is available from client APIs. For example, you have to use the server object model to create or change user profiles because they're read-only from client APIs (except the user profile picture). Also, there's no client-side access to some namespaces, such as Microsoft.Office.Server.Audience, Microsoft.Office.Server.ReputationModel, or Microsoft.Office.Server.SocialData. To see what's supported functionality for the client APIs, see Microsoft.SharePoint.Client.Social and Microsoft.SharePoint.Client.UserProfiles.

That being said, there is a legacy web service that might work for you. You can access this via SPServices jQuery plugin pretty easily. Here is an example from Marc Anderson's blog.

Modify User Profile Properties on SharePoint Online 2013 using SPServices

Here's a snip of the code he uses to illustrate the process:

  var propertyData = "<PropertyData>" +
  "<IsPrivacyChanged>false</IsPrivacyChanged>" +
  "<IsValueChanged>true</IsValueChanged>" +
  "<Name>" + propertyName + "</Name>" +
  "<Privacy>NotSet</Privacy>" +
  "<Values><ValueData><Value xsi:type=\"xsd:string\">" + propertyValue + "</Value></ValueData></Values>" +
  "</PropertyData>";

  $().SPServices({
    operation: "ModifyUserPropertyByAccountName",
    async: false,
    webURL: "/",
    accountName: userId,
    newData: propertyData,
    completefunc: function (xData, Status) {
      var result = $(xData.responseXML);
    }
  });

Here are some server object model examples as requested.

using Microsoft.Office.Server.UserProfiles
//etc...

SPServiceContext serviceContext = SPServiceContext.GetContext(site); 

UserProfileManager = new UserProfileManager(serviceContext); 
UserProfile = userProfileManager.CreateUserProfile("mydomain\RKaucher"); 
//Change display Name property label to "Friendly Name".
userProfile.DisplayName = "Friendly Name";  
userProfile["FirstName"].Value = "Robert" 
userProfile[PropertyConstants.Skills].Add("SharePoint Development"); 
//Or you can use this method
userProfile[PropertyConstants.Skills][0] = "jQuery"; 
userProfile.Commit();
share|improve this answer
    
Can you point me to any reference to do this via server object model? –  variable Mar 22 at 14:17
    
Honestly, with the exception of integrating with BCS, I have not seen a lot of examples out on the web with SOM. I've added some code samples. But the only web references I have seen are on MSDN. –  Robert Kaucher Mar 22 at 14:36
    
Can you provide the reference for the server object model code you have posted. –  variable Mar 24 at 7:43
    
Thanks the link (spservices) was very helpful –  variable Mar 24 at 9:18
1  
Also the SOM worked. Reference: fewlines4biju.com/2011/01/… –  variable Mar 24 at 11:57

It can be done using the UserProfile web service and/or SPServices, see here for an example.

Bulk updates are also possible.

I have tested both options successfully in SharePoint Online.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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