Recent Update
After experimenting with a new approach, I encountered an issue. The code works fine on the first call, but when attempting a second call, it throws an error stating that the collection has not been initialized. More details about this problem can be found in the comments within the test function below.
Javascript:
function test(){
//the initial call works without issues
countRetrieve('Very', 'Difficult');
//the second call triggers an error regarding uninitialized collListItem
countRetrieve('Less', 'Interesting');
}
function countRetrieve(grade, title) {
var siteUrl = '/sites/MySite';
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('Summary');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where>' +
'<And>' +
'<Eq><FieldRef Name=\'Grad\'/><Value Type=\'Text\'>' +
grade +
'</Value></Eq>' +
'<Eq><FieldRef Name=\'Title\'/><Value Type=\'Text\'>' +
title +
'</Value></Eq>' +
'</And>' +
'</Where></Query></View>');
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onRetrieveQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onRetrieveQuerySucceeded(sender, args) {
listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
itemId = oListItem.get_id();
itemCount = oListItem.get_item('Count');
}
updateCount();
}
function updateCount() {
var clientContext = new SP.ClientContext('/sites/MySite');
var oList = clientContext.get_web().get_lists().getByTitle('Summary');
this.oListItem = oList.getItemById(itemId);
//increment the count by one
var c = itemCount + 1;
oListItem.set_item('Count', c);
oListItem.update();
clientContext.executeQueryAsync(Function.createDelegate(this, this.onUpdateSucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onUpdateSucceeded(sender, args){
alert('item count successfully updated');
}
I am attempting to retrieve the current value of the "Count" column in my list and increment it by 1. However, I'm encountering an error indicating that the collection has not been initialized.
Wasn't it initialized in this.collListItem = oList.getItems(camlQuery); ?
If there are better ways to achieve this task in Sharepoint and Javascript, I would greatly appreciate any guidance as I am relatively new to both technologies.
Here is my revised code (javascript):
function countUpdate() {
var siteUrl = '/sites/MySite';
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('Summary');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where>' +
'<And>' +
'<Eq><FieldRef Name=\'Grade\'/><Value Type=\'Text\'>' +
'Really' +
'</Value></Eq>' +
'<Eq><FieldRef Name=\'Property\'/><Value Type=\'Text\'>' +
'Narrow' +
'</Value></Eq>' +
'</And>' +
'</Where></Query></View>');
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onUpdateQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var count = oListItem.get_item('Count');
oListItem.set_item('Count', '40'); //updating count to 40
oListItem.update();
}
clientContext.executeQueryAsync(Function.createDelegate(this, this.onUpdateQuerySucceeded),Function.createDelegate(this, this.onQueryFailed));
}