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?

3 Answers 3


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


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:


<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();
        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:' + 

    function onFailureMethod(sender, args) {
        alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());

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'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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