Monday, March 17, 2014

Update UserProfile properties in using SPServices in SharePoint

I got a requirement to update the User Profile properties for one of my client. We have only option to do that using SPServices because they are not allowing any server side code. In SPServices we can directly update the user profile services using “ModifyUserPropertyByAccountName” method. By passing current logged-in user account to the method we can update the user profile properties using SPServices. I have found the following code from here (http://azzu-sheikh.blogspot.in/2014/01/update-user-profile-property-through.html)to update the user profile service.

function updateUserProfileProperty(currentLoggedInUser, propertyName, propertyValue) {
   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: currentLoggedInUser,
       newData: propertyData,
       completefunc: function (xData, Status) {
           var result = $(xData.responseXML);
           if (Status == "success") {
              //do update action
           }
       }
   });
}


We can change the user property details by getting property data in to the method and passing the current logged-in user account details as shown the code above.

Share this