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 ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
ctx.load(web);
var user = web.get_currentUser();
alert(user);

I used above code to get the current user using JavaScript, but I got an error:

'SP.ClientContext.get_current' is null or not an object

What should I do? Any help appreciated.

share|improve this question
    
possible duplicate of SP.ClientContext.get_current() returning undefined objects –  Robert Lindgren Jul 18 '13 at 13:52
    
Which version of SharePoint are you using and what type of solution are you building? –  EHorodyski Jul 18 '13 at 14:59
    
i am using SharePoint 2013. –  Nera Jul 18 '13 at 17:50

2 Answers 2

I have used SharePoint’s JavaScript Object Model to retrieve the current logged in username.Once you have executed the load and executeQueryAsync methods and the call to SharePoint was sucessful, then inside of the onQuerySucceeded method is where I display the username to the browser using the SP.Principal.get_loginName property. Try it out…you can host and run the code below inside of a content editor web part in SharePoint 2010.

<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(init,'sp.js');
var currentUser;
function init(){
    this.clientContext = new SP.ClientContext.get_current();
    this.oWeb = clientContext.get_web();
    currentUser = this.oWeb.get_currentUser();
    this.clientContext.load(currentUser);
    this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
}

function onQuerySucceeded() {
    document.getElementById('userLoginName').innerHTML = currentUser.get_loginName(); 
    document.getElementById('userId').innerHTML = currentUser.get_id();
    document.getElementById('userTitle').innerHTML = currentUser.get_title();
    document.getElementById('userEmail').innerHTML = currentUser.get_email();
}

function onQueryFailed(sender, args) {
    alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}
</script>
<div>Current Logged User:
    <span id="userLoginName"></span>
    <span id="userId"></span>
    <span id="userTitle"></span>
    <span id="userEmail"></span>
</div>

or use the below code

function CallClientOM()
{
var context = new SP.ClientContext.get_current();
this.website = context.get_web();
this.currentUser = website.get_currentUser();
context.load(currentUser);
context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}



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



function onQueryFailed(sender, args)
{
alert('request failed ' + args.get_message() + '\n'+ args.get_stackTrace());
}
share|improve this answer
    
i tried your code, but got an error it showed "Sys.ArgumentUndefinedException: Value cannot be undefined. Parameter name: method" –  Nera Jul 18 '13 at 18:22

Refer this hyperlink it is useful and shall help you

share|improve this answer
    
Hi Mancy! It is custom at the StackExchange Network to add a solution to the problem in text and images, and using links as reference. Only link answers will not be very useful the day the link is broken for some reason. So please update your answer with ths solution, and use the link as reference. Thank You! –  Benny Skogberg Jan 7 at 11:51

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.