2

I am building some CSOM code in my DispForm.aspx. I try to get the current user from the context. This works and I can get the loginName, id, title and email. But I would like to have the address and phonenumber of the current user. Is this possible client side?

THIS CODE IS WORKING

<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(init,'sp.js');
var currentUser;
function init(){
    this.clientContext = new SP.ClientContext.get_current();
    this.oWeb = clientContext.get_web();
    currentUser = this.oWeb.get_currentUser();
    this.clientContext.load(currentUser);
    this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
}

function onQuerySucceeded() {
    document.getElementById('userLoginName').innerHTML = currentUser.get_loginName(); 
    document.getElementById('userId').innerHTML = currentUser.get_id();
    document.getElementById('userTitle').innerHTML = currentUser.get_title();
    document.getElementById('userEmail').innerHTML = currentUser.get_email();
}

function onQueryFailed(sender, args) {
    alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}
</script>
<div>Current Logged User:
    <span id="userLoginName"></span>
    <span id="userId"></span>
    <span id="userTitle"></span>
    <span id="userEmail"></span>
</div>

enter image description here

2

Use SP.UserProfiles.PeopleManager.getMyProperties to load user properties for the current user in JSOM, for example:

function getMyProperties(success,error)
{
      var context = SP.ClientContext.get_current();
      var peopleManager = new SP.UserProfiles.PeopleManager(context);
      var myProperties = peopleManager.getMyProperties();

      context.load(myProperties);
      context.executeQueryAsync(
      function () {
         success(myProperties);
      }, 
      error
   ); 
}

Usage

SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() {
   SP.SOD.executeFunc('userprofile', 'SP.UserProfiles.PeopleManager', function() {

   getMyProperties(
       function(myProperties){
          var workPhone = myProperties.get_userProfileProperties()['WorkPhone'];
          var office = myProperties.get_userProfileProperties()['Office'];
       },
       function(sender,args){
           console.log(args.get_message());
       }
   );

   });

});

SP.SOD.executeFunc is used to ensure UserProfile JS Library (SP.UserProfiles.js) is loaded in SharePoint 2013

1
  • 1
    nice man, works perfectly Apr 8 '16 at 17:03
1

Use following code and and then look at this: http://msdn.microsoft.com/en-us/library/office/jj679700(v=office.15).aspx to access the collection of properties returned.

Include following js

<SharePoint:ScriptLink ID="ScriptLink1" name="SP.js" runat="server"
    ondemand="false" localizable="false" loadafterui="true" />
<SharePoint:ScriptLink ID="ScriptLink2" name="SP.UserProfiles.js" runat="server"
    ondemand="false" localizable="false" loadafterui="true" />


var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    var personProperties = peopleManager.getMyProperties();
    clientContext.load(currentUser);
    clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
2
  • Thanks Ashish, I get only this error with your example code: Error: Unable to get property 'PeopleManager' of undefined or null reference. How does it come?
    – Ola
    May 9 '14 at 6:31
  • Please see my comments about including the appropriate JS Library. Looks like you are missing SP.UserProfile.js May 9 '14 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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