2

I want to understand the difference between the following two methods to retrieve the current user email id:

Method 1:

function getCurrentUserEmail(){
  var clientContext = new SP.ClientContext.get_current();
  this.website = clientContext.get_web();
  this.currentUser = website.get_currentUser();
  clientContext.load(currentUser);
  clientContext.executeQueryAsync(Function.createDelegate(this, this.onRequestSuccess), Function.createDelegate(this, this.onRequestFail));
}
function onRequestSuccess(sender, args){
  var curUserEmail = currentUser.get_email();
}
function onRequestFail(sender, args){
  alert('request failed ' + args.get_message() + '\n'+ args.get_stackTrace());
}

Method 2:

function getCurrentUserEmail(){
  var targetUser = "domainName\\Username"; //logged in user
  var clientContext = new SP.ClientContext.get_current();
  var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
  personProperties = peopleManager.getPropertiesFor(targetUser);
  clientContext.load(personProperties);
  clientContext.executeQueryAsync(Function.createDelegate(this, this.onRequestSuccess), Function.createDelegate(this, this.onRequestFail));
}
function onRequestSuccess(sender, args){
  var strEmailID = personProperties.get_userProfileProperties()['WorkEmail'];
  var strLastName = personProperties.get_userProfileProperties()['FirstName'];
  var strEmailID = personProperties.get_userProfileProperties()['LastName'];
}
function onRequestFail(sender, args){
  alert('request failed ' + args.get_message() + '\n'+ args.get_stackTrace());
}


With the first method I can only get UserID, Email, LoginName and Title, but the second method is required to get first name & last name fields.

The problem is, that both methods are working fine in my local environment, but Method 2 is not working in the production environment.

3

In order to access the PeopleManager you need to load SP.UserProfiles.js so this line on the top of method two.

 SP.SOD.executeOrDelayUntilScriptLoaded(getCurrentUserEmail, 'SP.UserProfiles.js');

You can additionally use fiddler and IE/Chrome developer tools to check if scripts are loaded properly.

  • still it is going to requestFail method with error message like -- "Unknown Error, Undefined" – Yayati Jun 19 '15 at 5:32
  • How are you accessing it? App, web part or what? what JS script files are loading in the chrome/IE at the time of error? Could you paste the screen shot? – Falak Mahmood Jun 19 '15 at 5:38
  • I am using it in a site page with inside a <script> block and calling the methods on click of two buttons. <SharePoint:ScriptLink name="clienttemplates.js" runat="server" LoadAfterUI="true" Localizable="false"/> Like this I am using sp.js, sp.runtime.js, sp.core.js, SP.UserProfiles.js – Yayati Jun 19 '15 at 5:46
0

I just found out the other way of getting current user info

function getCurrentUserEmail(){
  var clientContext = new SP.ClientContext.get_current();
  this.website = clientContext.get_web();
  this.currentUser = website.get_currentUser();
  clientContext.load(currentUser);
  clientContext.executeQueryAsync(Function.createDelegate(this, this.onRequestSuccess), Function.createDelegate(this, this.onRequestFail));
}
function onRequestSuccess(sender, args){
  var curUserEmail = currentUser.get_email();
  var curUserloginName = currentUser.get_loginName();
  getUserInfo(curUserloginName);
}
function onRequestFail(sender, args){
  alert('request failed ' + args.get_message() + '\n'+ args.get_stackTrace());
}
function getUserInfo(loginName){
  var context = new SP.ClientContext.get_current();
  var userInfoList = context.get_web().get_siteUserInfoList();
  var query = new SP.CamlQuery();
  var viewXml = "<View><Query><Where><Eq><FieldRef Name='Name' /><Value Type='Text'>" + loginName+ "</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>";
  query.set_viewXml(viewXml);
  var items = userInfoList.getItems(query);
  context.load(items,'Include(Deleted,Department,EMail,FirstName,ID,IsActive,IsSiteAdmin,JobTitle,LastName,MobilePhone,Name,Notes,Office,Picture,SipAddress,UserName,WebSite,WorkPhone)');
  context.executeQueryAsync(function(){
    if(items.get_count() > 0) {
      var item = items.itemAt(0);
      console.log('User Id: ' + item.ID);
      console.log('First Name: ' + item.FirstName);
      console.log('Last Name: ' + item.LastName);
      console.log('EMail: ' + item.EMail);
      console.log('Name: ' + item.Name);
      console.log('User Name: ' + item.UserName);
    }
  },
    console.log(args.get_message());
  );
}

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.