1

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>

1 Answer 1

1

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>
1
  • If it works, please don't forget to accept it as the best answer. Commented Dec 5, 2018 at 4:37

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.