2

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.

2 Answers 2

5

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();
4
  • Can you point me to any reference to do this via server object model?
    – variable
    Commented Mar 22, 2014 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. Commented Mar 22, 2014 at 14:36
  • Can you provide the reference for the server object model code you have posted.
    – variable
    Commented Mar 24, 2014 at 7:43
  • 1
    Also the SOM worked. Reference: fewlines4biju.com/2011/01/…
    – variable
    Commented Mar 24, 2014 at 11:57
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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