1,基于Query语句的列表查询。

function retrieveListItems(itemId) {
var siteUrl=_spPageContextInfo.webServerRelativeUrl;
var clientContext = new SP.ClientContext(siteUrl);
//列表名一定要用中文
var oList = clientContext.get_web().get_lists().getByTitle('项目信息'); var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(
'<View><Query><Where><Eq><FieldRef Name=\'ID\'/>' +
'<Value Type=\'Number\'>'+itemId+'</Value></Eq></Where></Query>' +
'<RowLimit>10</RowLimit></View>'
);
this.collListItem = oList.getItems(camlQuery); clientContext.load(collListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function onQuerySucceeded(sender, args) {
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator(); while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
listItemInfo = oListItem.get_item('Title');
alert(listItemInfo);
} } function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
}

camlQuery.set_viewXml(
          '<View><Query>'+
          '<Where>'+
           '<And>'+
           '<Eq>'+
           '<FieldRef Name=\'Project\' LookupId=\'TRUE\'/><Value Type=\'Lookup\'>'+projectId+'</Value>' +
           '</Eq>'+
           '<Eq>'+
           '<FieldRef Name=\'AssignedTo\'/><Value Type=\'User\'>'+userName+'</Value>' +
           '</Eq>'+
           '</And>'+
          '</Where></Query>' +
          '<RowLimit>10</RowLimit></View>'
      );

2,基于ItemId的列表查询

function retrieveListItems(itemId) {
var siteUrl=_spPageContextInfo.webServerRelativeUrl;
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('项目工作项');
this.oListItem = oList.getItemById(itemId);
clientContext.load(this.oListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function onQuerySucceeded(sender, args) {
alert(this.oListItem.get_item('ID'));
} function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

3.根据Id删除条目

function deleteListItem(itemId) {
var siteUrl=_spPageContextInfo.webServerRelativeUrl;
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('项目工作项');
this.oListItem = oList.getItemById(itemId);
oListItem.deleteObject(); clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
} function onQuerySucceeded() {
alert('删除成功');
//SP.UI.ModalDialog.commonModalDialogClose(0);
} function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
}

4.根据Id更新条目

function updateListItem(itemId,newVersion) {
var siteUrl=_spPageContextInfo.webServerRelativeUrl;
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('项目信息');
this.oListItem = oList.getItemById(itemId);
oListItem.set_item('FullVersion',newVersion);
oListItem.update();
clientContext.executeQueryAsync(Function.createDelegate(this, this.onUpdateSucceeded), Function.createDelegate(this, this.onUpdateFailed));
}
function onUpdateSucceeded() {
alert('更新成功!'); }
function onUpdateFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
05-11 21:51