1

Environment : SP 2010

I have a custom property in User Profile called CompanyCode.

I need to fetch this property using javascript/jquery in a master page. Can anyone let me know how to fetch custom property from user profile.

I used

var companyCode = $().SPServices.SPGetCurrentUser({
      fieldName: "CompanyCode"
   });
alert(companyCode);

returned empty string.

Note :

I have also tried default properties but no result.

var companyCode = $().SPServices.SPGetCurrentUser({
              fieldName: "CompanyCode"
           });
        alert(companyCode);

Let me know how can I achieve this?

Edit: Now I have taken SP.Profile.js file from 2013 environment and referenced it. Still facing issue.

2
  • is it necessary to use spservices? Commented Jun 10, 2016 at 8:52
  • no.. only client side and output is required.. may be anyway
    – Gaurravs
    Commented Jun 10, 2016 at 8:53

2 Answers 2

1

This code is of retrieving user properties of current logged in user. you can read more in this article of how to modify the below code to obtain what you need.

(function($){

  $(document).ready(function(){         
    // Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
    SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js'); 
  });

  var userProfileProperties;

  function loadUserData(){

    //Get Current Context   
    var clientContext = new SP.ClientContext.get_current();

    //Get Instance of People Manager Class
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

    //Get properties of the current user
    userProfileProperties = peopleManager.getMyProperties();

    clientContext.load(userProfileProperties);

    //Execute the Query.
    clientContext.executeQueryAsync(onSuccess, onFail);

  }

  function onSuccess() {        

    alert(userProfileProperties.get_displayName());

  }

  function onFail(sender, args) {
    alert("Error: " + args.get_message());
  } 

})(jQuery);
1
  • I couldn't find any reference to SP.UserProfiles.js, my environment is 2010 not 2013
    – Gaurravs
    Commented Jun 10, 2016 at 9:14
0

I solved it. This can be achieved in 2 ways:

$(document).ready(function() {
        var soapEnv =
            "<?xml version='1.0' encoding='utf-8'?> \
    <soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> \
  <soap:Body> \
    <GetUserProfileByName xmlns='http://microsoft.com/webservices/SharePointPortalServer/UserProfileService'> \
      <AccountName>"+"DOMAIN\\UserID"+"</AccountName> \
    </GetUserProfileByName> \
  </soap:Body> \
</soap:Envelope>";

        $.ajax({
            url: "http://devsp10:2000/_vti_bin/userprofileservice.asmx",
            type: "POST",
            dataType: "xml",
            data: soapEnv,
            complete: processResult,
            contentType: "text/xml; charset=\"utf-8\""
        });
});

function processResult(xData, status) {
         var cCode ;
         $(xData.responseXML).find("PropertyData > Name:contains('CompanyCode')").each(function() {
            cCode = $(this).parent().find("Values").text();
         alert("cCode : "+ cCode);
      });       
}

OR Can be achieved by adding reference to SPServices from codeplex.

ExecuteOrDelayUntilScriptLoaded(init,'sp.js');

function init(){

   var cCode;
   $().SPServices({
    operation: "GetUserProfileByName",
    async: false,
    AccountName: $().SPServices.SPGetCurrentUser(),
    completefunc: function (xData, Status) {
      $(xData.responseXML).find("PropertyData > Name:contains('CompanyCode')").each(function() {
        cCode = $(this).parent().find("Values").text();
        alert("Company Code : "+cCode);
      });
    }
  });
}

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.