1

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.

1

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);
//...
  • Thanks, it work! :) Now have to get from list field a login/mail (think its better than compare by full user name). – Rhonin Oct 26 '16 at 6:18
  • userId and lookupId for list item was enough to compare :) I think it works fine. Without domain login /email/full user name, thanks :) – Rhonin Oct 26 '16 at 7:05
1

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());
  );
  • I try it, get error when using get_loginName(), uncaught error that it wasnt initiated. I need only loginname or full user name. – Rhonin Oct 26 '16 at 5:56
  • Can you paste your code here, it seems you are trying to use object before it is loaded(executequery has ran)... – Siddharth Vaghasia Oct 26 '16 at 7:20
  • I have in method all this executequery... and this method return item to me but I cant use these function. function getUser() { var currentUser = web.get_currentUser(); currentUser.retrieve(); clientContext.load(currentUser); clientContext.executeQueryAsync( function () { // code to execute if success }, function () { onQueryFailed(); }); return currentUser; } So this is why this function dont work on my. Now I know and solved problem in other way :) thx anyway – Rhonin Oct 26 '16 at 9:15
  • np....please mark it as answer so that it is helpful to others also...!! – Siddharth Vaghasia Oct 26 '16 at 9:18
0

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?

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.