Question

How can I get full name from SPUser? I got him from get_currentUser() from web context. And how can I convert from SPUser to SPFieldUser in JS?

EDIT: I need it to compare, I get from list field SPFieldUser and want to compare it with current user. Or by full name or by domain login (which is better i think). But I cant get both. get_loginName() from web current user doesnt work but . But I get email by _spPageContextInfo.userLoginName or user by userDisplayName. From list I got full user name by get_lookupValue() but cant get login name. But I wish to compare it by domain login or email.

Was it helpful?

Solution

You can retrieve current user's display name, id & login name from _spPageContextInfo object.

window.console && console.log('Display Name: ' + _spPageContextInfo.userDisplayName);
window.console && console.log('Login Name: ' + _spPageContextInfo.userLoginName);
window.console && console.log('User Id: ' + _spPageContextInfo.userId);

To set the list item's user field value

var userFld = new SP.FieldUserValue();
userFld.set_lookupId(_spPageContextInfo.userId);

//...
//...
// Set the list item 
item.set_item(person_field_column_Name, userFld);
//...

OTHER TIPS

Please use below method to get all the required information of user...

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());
  );

use get_title() to get the full name:

var usertxt = web.get_currentUser();
usertxt.get_title();

why do you want to convert an SPUser to an SPFieldUser?

this will explain why I ask the question above, if its to update a list item just use the spuser object

How do I convert SPUser to SPFieldUser?

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top