更新
我现在尝试了一种新方法。这在我第一次调用它时有效,但第二次它告诉我由于某种原因尚未启动收集。此代码片段的测试功能中的注释将澄清我的问题。
Javascript:
function test(){
//this first call works
countRetrieve('Very', 'Difficult');
//this second call generates error that collListItem hasnt been initiated
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);
//update the count, raise it 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 updated');
}
我试图检索列表中“计数”列的当前值,然后将该值增加 1。但是我收到一个错误,告诉我集合尚未初始化。
它没有在this.collListItem = oList.getItems(camlQuery);中初始化吗??
这个功能可能完全错误,我非常感谢有关如何执行此任务的提示,因为我是 Sharepoint 和 Javascript 的新手。
这是我的代码(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));
//update
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var count = oListItem.get_item('Count');
oListItem.set_item('Count', '40');
oListItem.update();
}
clientContext.executeQueryAsync(Function.createDelegate(this, this.onUpdateQuerySucceeded),Function.createDelegate(this, this.onQueryFailed));
}