When trying to resolve a user with the ensureUser() call, the object is always returned as undefined even though the user exists as a Domain and SharePoint user.

//Calling  code.
var currentCtx = SP.ClientContext.get_current();
var picker = SPClientPeoplePicker.SPClientPeoplePickerDict.clientPeoplePicker_TopSpan.GetAllUserInfo();
var user = _UserRepo.GetUserId(currentCtx, picker[0].Key);

//Business logic.
UserRepository: {
        GetUserId: function (currentCtx, loginName) { //loginName comes in as i:0#.w|domain\userName
            var resolveUser = currentCtx.get_web().ensureUser(loginName);
            //resolveUser is undefined after Async call and console reads
            //Uncaught TypeError: Cannot read property 'get_id' of undefined
            currentCtx.executeQueryAsync(function () { console.log(resolveUser.get_id()); return resolveUser; }, function (sender, args) { console.log(args.get_message()); });

I thought I was doing everything right according to the client OM documentation, but I guess not. How do I get the user properly?

  • 2
    Try hard-coding the login name as ensureUser("i:0#.w|domain\\userName") and see if it works. Or just try removing the claims token and try with "domain\\userName" – Akhoy Oct 26 '15 at 1:34
  • Have you check what you are getting in 'loginName' in GetUSerId function? – Kaushal Khamar Oct 26 '15 at 4:41

Using the below code you may get the properties such as login id, name and email id of the user currently logged in. Is this the one you are expecting?

<script type="text/JavaScript">

function GetCurrentUsername()
var ctxt = new SP.ClientContext.get_current();
this.website = ctxt.get_web();
this.currentUser = website.get_currentUser();
ctxt.executeQueryAsync(Function.createDelegate(this, this.onSucceess), Function.createDelegate(this, this.onFail));

function onSucceess(sender, args)
alert(currentUser.get_loginName()+" ... "+currentUser.get_title()+" ... "+currentUser.get_email());

function onFail(sender, args)
alert('request failed ' + args.get_message() + '\n'+ args.get_stackTrace());
| improve this answer | |

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.