0

I try to modify the value of some fields in a list with CSOM on a button click.

Here is the script I call in the html:

$("#palettebutton").click(function(){

        getItemsToBeUpdated();

    });



        var collListItemToBeUpdated = "";
        var enumerator;

        var paletteitem;
        var palettenumberdébut;
        var palettenumberfin;


        function getItemsToBeUpdated() //this function called on button click to get ID's
        {
             if($('#numberpalettedébut').val() && ($('#numberpalettedébut').val())!=undefined && ($('#numberpalettedébut').val())!="")
             {
                palettenumberdébut = $('#numberpalettedébut').val()-1;
             }
             if($('#numberpalettefin').val() && ($('#numberpalettefin').val())!=undefined && ($('#numberpalettefin').val())!="")
             {
                palettenumberfin= $('#numberpalettefin').val();
             }
             if($('#counterpalette').val() && ($('#counterpalette').val())!=undefined && ($('#counterpalette').val())!="")
             {
                paletteitem= $('#counterpalette').val();
             }
             collListItemToBeUpdated = "";
             enumerator;
             var clientContext = new SP.ClientContext.get_current();
             var oList = clientContext.get_web().get_lists().getByTitle('Matériel Recyclage');
             var camlQuery = new SP.CamlQuery();
             camlQuery.set_viewXml("<View> <Query> <OrderBy> <FieldRef Name='Type_x0020_de_x0020_mat_x00e9_ri' Ascending='True' /> </OrderBy> </Query> <RowLimit>" + palettenumberfin + "</RowLimit></View> ");
             collListItemToBeUpdated = oList.getItems(camlQuery);
             clientContext.load(collListItemToBeUpdated);
             clientContext.executeQueryAsync(getItemsToBeUpdatedSuccess, getItemsToBeUpdatedFailed);
        }
        function getItemsToBeUpdatedSuccess()
        {
             var itemArray = [];
             var clientContext = SP.ClientContext.get_current();
             var oList = clientContext.get_web().get_lists().getByTitle('Matériel Recyclage');
             var i = 0;
             enumerator = collListItemToBeUpdated.getEnumerator();
             while (enumerator.moveNext()) {
                 if(i>=palettenumberdébut)
                 {

                        var oListItem = enumerator.get_current();
                        oListItem.set_item('Palette', paletteitem);  
                        oListItem.update();
                        itemArray.push(oListItem);
                        clientContext.load(itemArray[itemArray.length-1]);                  
                 }
                 i++;

             }
             clientContext.executeQueryAsync(updateMultipleListItemsSuccess, updateMultipleListItemsFailed);
        }
        function getItemsToBeUpdatedFailed(sender, args)
        {
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }

        function updateMultipleListItemsSuccess() 
        {    
            location.reload();
            alert('Mise à jour effectuée avec succès');

        }

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

I try to modify the value of the items I choose : between #numberpalettedébut and #numberpalettefin.

But I don't understand why sometimes it works and goes to updateMultipleListItemsSuccess(sender, args) and sometimes it goes to updateMultipleListItemsFailed(sender, args) and fails.

The error : status code 0

I just sort the items by the field with ID Type_x0020_de_x0020_mat_x00e9_ri but in some cases it works and others similar cases it doesn't work.

Thanks in advance

0

Ok..did you try in the developer tool? Try running the code in developer tool and see if you are hitting the elements you are calling. Its working sometime and sometime its not working can be of several reason. The first reason I can think of DOM not getting loaded. Once the focus is on element, the scripts works.

Try loading your whole function inside window.load functionality. Here is an example:

$(window).load(function() {       alert("window load occurred!"); });

Implement this and if it works mark it as answer.

Thanks,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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