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

I have a list . I want select items of list with a special quary.(get items that created with current user). Then get count of items.

I use this code for get current user :

function Initialize()
{
    context = new SP.ClientContext.get_current();
    web = context.get_web();
   var currentUser = web.get_currentUser();
   alert(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 onQueryFailed(sender, args) {
    alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}

But don't get current user.

and use this code for get items of list.

 function Initialize()
{
    clientContext = new SP.ClientContext.get_current();
    web = clientContext.get_web();

    var list = web.get_lists().getByTitle("Sends");
    var camlQuery = new SP.CamlQuery();
    var q = "<Where><Eq><FieldRef Name='CreatedUser' /><Value Type='User'>CurrentUser</Value></Eq></Where>";
    camlQuery.set_viewXml(q);
    this.listItems = list.getItems(camlQuery);
     clientContext.load(listItems, 'Include(ID)');

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemsLoadSuccess), 
    Function.createDelegate(this, this.onQueryFailed));
}
function onListItemsLoadSuccess(sender, args) {
    var count = 0;

     count = this.listItems.get_count();

           }

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

5 Answers

Have you tried jQuery SPServices api. There is a direct method given in the library. I used it.

http://spservices.codeplex.com/wikipage?title=$().SPServices.SPGetCurrentUser

Hope this helps you!

Also, I think you should load the web first and then you can get the current user.

share|improve this answer
add comment
up vote 6 down vote accepted

This code worked:

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
    
This worked for me, too (SP Enterprise 2010). Thanks! –  BGM Dec 7 '12 at 4:04
add comment

If you only want to count items created by a current user, you don't need the whole current user object. Everything you need is the the id of the current user:

var userId = _spPageContextInfo.userId;

Then you can use this id for creating the caml queries. This id is already on the page, so you avoid an asynchronous server call. Take inspirations from other similar questions and answers on sharepoint.stackexchange: Javascript get guid from Sharepoint and can i get the last ID from document library?

share|improve this answer
    
thanks a lot, How do find title of this userid? –  ar.gorgin Sep 1 '12 at 3:52
add comment

Following code is working for me,

    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());
    }
    ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js");

try this may be helpful for you :)

share|improve this answer
    
I use this code but don't work for me . –  ar.gorgin Aug 30 '12 at 5:52
    
what is the error? can you use ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js")? –  Ravi Suthar Aug 30 '12 at 5:53
    
Yes, i use. It is run but don't return current user. –  ar.gorgin Aug 30 '12 at 5:59
add comment

The code posted by Ravi Suthar worked great except I made some modifications to keep it from running in the global scope and added a parameter for a callback function.

/*
    Get the SharePoint username of current user.
    @params: callback:function
    @returns: void
*/
function getCurrentUser(callback) {

    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(null, success), Function.createDelegate(null, fail));
    }
    function success(sender, args) {
        var userObject = web.get_currentUser();
        //returns user object to caller
        callback({
            title: userObject.get_title(),
            login: userObject.get_loginName()
            //add other user properties here...
        });
    }
    function fail(sender, args) {
        callback(null);
        throw ('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    window.ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js");
};
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.