SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a requirement that I need to check the list item title in SharePoint 2010 using ECMA script.

For that I am using:

this.oList = web.get_lists().getByTitle('UserThemes');


    for (var itemId = 0; itemId < oList.get_itemCount(); itemId++) {
        this.oListItem = oList.getItemById(itemId + 1);
        if (oListItem.get_item('Title') == currentUser.get_loginName()) {
            oListItem.set_item('CurrentTheme', scriptLink);           
            context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));            

function onQuerySucceeded(sender, args) 
    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo += '\nID: ' + oListItem.get_id() +
            '\nTitle: ' + oListItem.get_item('Title');

function onQueryFailed(sender, args) 
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());

but I am having issue finding the list item count "oList.get_itemCount();" and "oListItem.get_item('Title')".

Can any one please help me on this?

share|improve this question

You cannot get the Title or anything else really before context.executeQueryAsync purely because the ajax call to the web server has not been made yet.

Here's a example of how to iterate using Client OM :

<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(initializeScript, "sp.js");

// ----   SP VARIABLES   -----------------------------------------
var clientContext = null;
var spWebobj = null;
var spListobj = null;
var spListobjItems = null;
var liCount;

// -----------------------------------------------------------
function initializeScript() {
clientContext = new SP.ClientContext.get_current();
spWebobj = this.clientContext.get_web();

spListobj = spWebobj.get_lists().getByTitle("yourlistname");

var spCamlQuery = new SP.CamlQuery();
var spViewXML = '<View><Query /></View>';

spListobjItems = spListobj.getItems(spCamlQuery);
clientContext.executeQueryAsync(onSuccess, onFail);

function onSuccess() {
    liCount = spListobjItems.get_count();
    for (x = 0; x < liCount; x++) {
        var listItem = spListobjItems.itemAt(x);
        var itemTitle  = listItem.get_item('Title');

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

Doing alerts is not much use. Unfortunately executeQueryAsync cannot return or populate global variables. The executeQueryAsync has little use except to display alerts since no variables can be passed to other methods.

share|improve this answer

Your Answer


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.