Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.
  var context = new SP.ClientContext.get_current();

  this.website = context.get_web();

  this.currentUser = website.get_currentUser(); 

  alert(currentUser);

i tried this code, but i got an alert and it shows "[object object]". How would i get the loginName. Any help appreciated.

share|improve this question
add comment

4 Answers 4

If you are working on SharePoint Online (2013), then they have introduced a new variable to hold the current user's login name in the _spPageContextInfo global object.

_spPageContextInfo.userLoginName;

This will get you the login name without making any AJAX calls with the JSOM or REST.

share|improve this answer
    
I just tried that in 2013 and it's undefined. SharePoint Online here. _spPageContextInfo.userId works, though, and is a good starting point. –  Renan Jan 15 at 14:47
    
Yup it won't be available everywhere as they would be updating the API gradually. –  Vardhaman Deshpande Jan 15 at 17:10
add comment
function GetCurrentUsername()
{
var ctxt = new SP.ClientContext.get_current();
this.website = ctxt.get_web();
this.currentUser = website.get_currentUser();
ctxt.load(currentUser);
ctxt.executeQueryAsync(Function.createDelegate(this, this.onSucceess), Function.createDelegate(this, this.onFail));
}



function onSucceess(sender, args)
 {
 alert(currentUser.get_loginName());
 }



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

You never load any objects and never executes the query so there is no wonder you get an error.

Do something like:

var clientContext = SP.ClientContext.get_current();
var website = clientContext.get_web();
currentUser = website.get_currentUser();
clientContext.load(website);
clientContext.load(currentUser);
   clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);

function onRequestSucceeded() {
    alert(currentUser.LoginName);
}

function onRequestFailed(sender, args) {
    alert('Error: ' + args.get_message());
}

Where website and currentUser are declared in the outer function (encapsuling the onRequestSucceeded method)

With reservation for errors in the code (are writing this from my phone)

share|improve this answer
add comment

Try this:

alert(currentUser.get_loginName());

Reference: Working with users using javascript

share|improve this answer
    
i tried your code it gives "Undefined" instead of "[object object]" –  Nera Jul 18 '13 at 18:29
    
Updated answer... –  BlueBird Jul 18 '13 at 18:44
    
I tried this as well. It gave me bellow error "The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested." Does this imply about a referencing issue ?? –  Nera Jul 19 '13 at 4:34
1  
No it implies exactly what both myself and pushpendra have answered you, that you are not loading objects or executing the query :) –  Robert Lindgren Jul 19 '13 at 5:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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