2

My intention is to retrieve all the entries for a project in a list and mark the field latest=Yes for the newest entry. I am assuming that would be the one with the biggest id.

I am able to retrieve the data and identify the max id but getting an error

items[i].set_item is not a function when trying to update.

I saw few similar posts and tried the suggestions but still getting the same error. your help is greatly appreciated. My working code is below

<script type="text/javascript">

function loadListData() {
 var ctx = new SP.ClientContext.get_current();
       //REPLACE the below with the name of your list as it shows up in your sharepoint
var targetList = ctx.get_web().get_lists().getByTitle( 'APP Projects' );
// var query = SP.CamlQuery.createAllItemsQuery();
var query = new SP.CamlQuery();
query.set_viewXml("<View><Query><Where><Contains><FieldRef Name='Title'/><Value Type='Text'>Integrated</Value></Contains></Where></Query></View>");
   //Declaring rows as global so that it can be accessed in the delegate functions
rows = targetList.getItems(query);
ctx.load(rows);
ctx.executeQueryAsync(Function.createDelegate(this, getDocsAllItemsSuccess), Function.createDelegate(this, getDocsAllItemsFailure));
}

function getDocsAllItemsSuccess(sender, args) {
   var listEnumerator = rows.getEnumerator();
   //DEBUG statement comment this out
   console.log("Succeeded in retrieving items");
var items = [];
   while (listEnumerator.moveNext()) {
     var item = listEnumerator.get_current().get_fieldValues();
     items.push( item );
    }
   console.log(items);
  var idArray= [];
       for (i = 0; i < items.length; i++) { 
        //text += items[i] + "<br>";
        idArray.push(items[i].ID);

        //items[i].set_item('Title', 'My Updated Title');
        }
    console.log(idArray);
    var maxId=Math.max.apply( Math, idArray );
    console.log(maxId);
    //processListItems( items );
    for (i = 0; i < items.length; i++) { 
        //text += items[i] + "<br>";
        if(items[i].ID==maxId) {
            items[i].set_item('Latest', 'Yes');
        }else {
            items[i].set_item('Latest', 'No');
        }
        //items[i].set_item('Title', 'My Updated Title');
        items[i].update();
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
        }
}
function onQuerySucceeded() {

    console.log('Item updated!');
}

function onQueryFailed(sender, args) {

    console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
function getDocsAllItemsFailure(sender, args) {
   //DEBUG statement, comment this out
   console.log('Failed to get list items. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}

ExecuteOrDelayUntilScriptLoaded(loadListData, "sp.js");

</script>
2

You are using get_fieldValues()

If you want to use set_item you'll have to do that on the item:

while (listEnumerator.moveNext()) {
     var item = listEnumerator.get_current();
     items.push( item );
}

But can't you simplify this some and order the result by ID descending, then the first item in the result is the item with highest ID.

Example with your code:

function loadListData() {
    var ctx = SP.ClientContext.get_current();

    var targetList = ctx.get_web().get_lists().getByTitle( 'APP Projects' );

    var query = new SP.CamlQuery();
    query.set_viewXml("<View><Query><Where><Contains><FieldRef Name='Title'/><Value Type='Text'>Integrated</Value></Contains></Where><OrderBy><FieldRef Name='ID' Ascending='FALSE'/></OrderBy></Query></View>");

    var rows = targetList.getItems(query);
    ctx.load(rows, "Include(Title, Id)");
    ctx.executeQueryAsync(function() { 

        rows.get_data().forEach(function(item, i) { 
            if(i === 0) {
                console.log("SET LATEST YES", item.get_item("Title"));
                item.set_item('Latest', 'Yes');
            } else {
                console.log("SET LATEST NO", item.get_item("Title"));
                item.set_item('Latest', 'No');
            }
            item.update();          
        });

        ctx.executeQueryAsync(function() {
            console.log("ALL DONE");
        }, function(s,a) { 
            console.log("error", a.get_message());
        });

    }, function(s,a) { 
        console.log("error", a.get_message());
    });
}

ExecuteOrDelayUntilScriptLoaded(loadListData, "sp.js");
1
  • That works! appreciate your help – SP_Newbie Jan 10 '18 at 18:11

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.