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 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');

   context.load(oList);

    for (var itemId = 0; itemId < oList.get_itemCount(); itemId++) {
        this.oListItem = oList.getItemById(itemId + 1);
        context.load(oListItem);
        if (oListItem.get_item('Title') == currentUser.get_loginName()) {
            oListItem.set_item('CurrentTheme', scriptLink);           
            oListItem.update();
            context.load(oListItem);
            oList.update();
            context.load(oList);
            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');
    }
    alert(listItemInfo.toString());
}

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
add comment

2 Answers

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
add comment

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>';
spCamlQuery.set_viewXml(spViewXML);

spListobjItems = spListobj.getItems(spCamlQuery);
clientContext.load(spWebobj);
clientContext.load(spListobjItems);
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');
        alert(itemTitle);
    }
}

function onFail() {
    alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());   
}
</script>
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.