Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

How can you get the current users id using javascript? Can you do this any JS file or does it need to be added at a certain point?

share|improve this question
add comment

3 Answers 3

up vote 3 down vote accepted

My favorite is to use the SPServices jQuery library, specifically the GetCurrentuser operation.

share|improve this answer
add comment

Since you are working on 2010 version you can use Client Object Model to get current user (and some other cool stuff).

Here is link to simple example:

Code:

<script type="text/javascript">
    ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js");
    var context = null;
    var web = null;
    var currentUser = null;

    function getWebUserData() {
        context = new SP.ClientContext.get_current();
        web = context.get_web();
        currentUser = web.get_currentUser();
        currentUser.retrieve();
        context.load(web);
        context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), 
             Function.createDelegate(this, this.onFailureMethod));
    }

    function onSuccessMethod(sender, args) {
        var userObject = web.get_currentUser();
        alert('User name:' + userObject.get_title() + '\n Login Name:' + 
                 userObject.get_loginName());
    }

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

Just wanted to share: If you are looking for the ID of the current user only, there is no need to use any framework or API - SharePoint (at least in 2010, don't know in other versions) saves the ID in a global javascript variable named '_spUserId'.

share|improve this answer
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.