Working with User Porfiles and Javascript CSOM

Hi,

Please find the useful code for retrieving user profile properties using javascript CSOM,

<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.0.min.js"></script>
<script src="/_layouts/15/SP.Runtime.js"></script>
<script src="/_layouts/15/SP.js"></script>
<script src="/_layouts/15/SP.UserProfiles.js"></script>
<script type="text/javascript">


(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());
     $("input[Title='RequestorName']").val(userProfileProperties.get_displayName())

 var department = userProfileProperties.get_userProfileProperties()["Manager"];
      alert(department);
    if (department  === undefined) {
     $("input[Title='Department']").val(department)};
   
  }


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

           
})(jQuery);


</script>







Also please refer the below link,

http://www.vrdmn.com/2013/02/sharepoint-2013-working-with-user.html

Comments

Popular posts from this blog

Run Powershell script on Remote Computer/Server

Power BI Refresh Issue: unable to convert the value '' from the source data type 'VT_BSTR' to the expected data type 'VT_I8.

SPDActivities - DP.SharePoint.Workflow - Workflow failed to run