is it possible to get item ids when not using executeQueryAsync. See my code below. I would like to get the item ids as i'm doing below in successGetListItemId() but would like to store in an array and use it later to update some item fields.

Is this possible and if not what are my options?

Thanks in advance

<script language="javascript" type="text/javascript">
///Can't get the itemIDs here
var IDs = [];
var arrayLength = IDs.length;
for (var i = 0; i < arrayLength; i++) {
    alert(IDs[i]);
    //Want to do an update here :(
}


$(document).ready(function () {               
                    ExecuteOrDelayUntilScriptLoaded(GetListItemId, "sp.js");          
});



function GetListItemId() {

        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        var list = web.get_lists().getByTitle('Test');
        var query = SP.CamlQuery.createAllItemsQuery();
        allItems = list.getItems(query);
        context.load(allItems);
        context.executeQueryAsync(Function.createDelegate(this, this.successGetListItemId));
    }

    function successGetListItemId() {

        var TextFiled = "";
        var ListEnumerator = this.allItems.getEnumerator();
        while (ListEnumerator.moveNext()) {
            var currentItem = ListEnumerator.get_current();
            TextFiled += currentItem.get_item('ID');
            IDs.push(currentItem);

        }


        //alert(TextFiled);
        $('#ids').append(TextFiled);
        console.log(IDs);
    }

</script>

<div id="ids"></div>
up vote 1 down vote accepted

You need to run the update function after you get the ids.

<script language="javascript" type="text/javascript">
var IDs = [];     

$(document).ready(function () {               
                    ExecuteOrDelayUntilScriptLoaded(GetListItemId, "sp.js");          
});    

function GetListItemId() {    
        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        var list = web.get_lists().getByTitle('Test');
        var query = SP.CamlQuery.createAllItemsQuery();
        allItems = list.getItems(query);
        context.load(allItems);
        context.executeQueryAsync(Function.createDelegate(this, this.successGetListItemId));
    }

    function successGetListItemId() {    
        var TextFiled = "";
        var ListEnumerator = this.allItems.getEnumerator();
        while (ListEnumerator.moveNext()) {
            var currentItem = ListEnumerator.get_current();
            TextFiled += currentItem.get_item('ID');
            IDs.push(currentItem.get_item('ID'));    
        }    

        //alert(TextFiled);
        $('#ids').append(TextFiled);
        console.log(IDs);
        updateItems(IDs);
    }

    function updateItems(IDs){
    var arrayLength = IDs.length;
        for (var i = 0; i < arrayLength; i++) {
            alert(IDs[i]);
            // do update here
        }
    }

</script>

<div id="ids"></div>
  • Thanks JoannaW qill get it a crack – naijacoder Dec 5 at 3:15
  • If it works, please don't forget to accept it as the best answer. – JoannaW_MSFT Dec 5 at 4:37

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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