一、新增
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
var siteUrl = ' /sites/MySiteCollection ' ;
function createListItem() {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle( ' Announcements ' );
var itemCreateInfo = new SP.ListItemCreationInformation();
this .oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item( ' Title ' , ' My New Item! ' );
oListItem.set_item( ' Body ' , ' Hello World! ' );
oListItem.update();
clientContext.load(oListItem);
clientContext.executeQueryAsync(Function.createDelegate( this , this .onQuerySucceeded), Function.createDelegate( this , this .onQueryFailed));
}
function onQuerySucceeded() {
alert( ' Item created: ' + oListItem.get_id());
}
function onQueryFailed(sender, args) {
alert( ' Request failed. ' + args.get_message() + ' \n ' + args.get_stackTrace());
}
< / script>
二、查询
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
function getProductById(productId) {
try {
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle( ' product ' );
this .product = list.getItemById(productId);
context.load(product, ' ProductName ' , ' ProductDescription ' , ' ProductType ' , ' LaunchDate ' , ' AvailableQuantity ' );
context.executeQueryAsync(Function.createDelegate( this , this .productReceived), Function.createDelegate( this , this .failed));
}
catch (e) {
alert(e);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
function getProducts(title) {
try {
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle( ' product ' );
var query = ' <View Scope=\ ' RecursiveAll\ ' > ' +
' <Query> ' +
' <Where> ' +
' <Contains> ' +
' <FieldRef Name=\ ' ProductName\ ' /> ' +
' <Value Type=\ ' Text\ ' > ' + title + ' </Value> ' +
' </Contains> ' +
' </Where> ' +
' </Query> ' +
' </View> ' ;
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(query);
this .productcollection = list.getItems(camlQuery);
context.load( this .productcollection, ' Include(ProductName, ProductDescription, ProductType, LaunchDate, AvailableQuantity) ' );
context.executeQueryAsync(Function.createDelegate( this , this .productsReceived), Function.createDelegate( this , this .failed));
}
catch (e) {
alert(e);
}
}
function productsReceived() {
alert( ' got products ' );
prcessProducts( this .productcollection);
}
function failed(sender, args) {
alert( ' failed. Message: ' + args.get_message());
}
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
function onQuerySucceeded(sender, args) { var listItemInfo = ''; var listItemEnumerator = collListItem.getEnumerator(); while (listItemEnumerator.moveNext()) { var oListItem = listItemEnumerator.get_current(); listItemInfo += '\nID: ' + oListItem.get_id() + '\nDisplay name: ' + oListItem.get_displayName(); } alert(listItemInfo.toString()); } function onQueryFailed(sender, args) { alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
function retrieveListItems() { var clientContext = new SP.ClientContext('/sites/MySiteCollection'); var oList = clientContext.get_web().get_lists().getByTitle('My List'); var camlQuery = new SP.CamlQuery(); camlQuery.set_viewXml('<View><Query><Where><Leq>' + '<FieldRef Name=\'ID\'/><Value Type=\'Number\'>100</Value>' + '</Leq></Where></Query><RowLimit>50</RowLimit></View>'); this.collListItem = oList.getItems(camlQuery); clientContext.load(collListItem, 'Include(Id, DisplayName)'); 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 += '\nID: ' + oListItem.get_id() + '\nDisplay name: ' + oListItem.get_displayName(); } alert(listItemInfo.toString()); } function onQueryFailed(sender, args) { alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); }
三、删除
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
function deleteListItem() {
this .itemId = 2 ;
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle( ' Announcements ' );
this .oListItem = oList.getItemById(itemId);
oListItem.deleteObject(); // Update
clientContext.executeQueryAsync(Function.createDelegate( this , this .onQuerySucceeded), Function.createDelegate( this , this .onQueryFailed));
}
function onQuerySucceeded() {
alert( ' Item deleted: ' + itemId);
}
function onQueryFailed(sender, args) {
alert( ' Request failed. ' + args.get_message() + ' \n ' + args.get_stackTrace());
}
四、CAML查询
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
function retrieveListItemsInclude(siteUrl) { var clientContext = new SP.ClientContext(siteUrl); var oList = clientContext.get_web().get_lists().getByTitle('Announcements'); var camlQuery = new SP.CamlQuery(); camlQuery.set_viewXml('<View><RowLimit>100</RowLimit></View>'); this.collListItem = oList.getItems(camlQuery); clientContext.load( collListItem, 'Include(Id, DisplayName, HasUniqueRoleAssignments)' ); 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 += '\nID: ' + oListItem.get_id() + '\nDisplay name: ' + oListItem.get_displayName() + '\nUnique role assignments: ' + oListItem.get_hasUniqueRoleAssignments(); } alert(listItemInfo.toString()); } function onQueryFailed(sender, args) { alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); }
五、获取附件
The attachment files are stored in the list as the structure as follows:
List rootfoler à ‘attachment’ folder à ‘Item Id’ folder à attachments.
For example, the path of the attachment in my server likes this: http://<servername>/lists/listtest/Attachments/1/doctext.docx .
So the main logic of my solution is:
1. Get the folder of the url likes : var attachmentFolder=web.getFolderByServerRelativeUrl('Lists/City/Attachments/'+itemId);
2. Loop all the files in the folder, and get the path of them using get_serverRelativeUrl ;
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
ExecuteOrDelayUntilScriptLoaded(getWebProperties, "SP.js"); var attachmentFiles; function getWebProperties() { var itemId=2; var ctx = new SP.ClientContext.get_current(); var web = ctx.get_web(); var attachmentFolder=web.getFolderByServerRelativeUrl('Lists/City/Attachments/'+itemId); attachmentFiles= attachmentFolder.get_files(); ctx.load(attachmentFiles); ctx.executeQueryAsync(Function.createDelegate(this,this.onSuccess),Function.createDelegate(this,this.onFailed)); } function onSuccess(sender, args) { var i=0; for(var file in attachmentFiles) { alert(attachmentFiles.itemAt(i).get_serverRelativeUrl()); i++; } } function onFailed(sender, args) { alert("sorry!"); }
六、更新
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
item = itemEnumerator.get_current(); var itemID = item.get_item("ID"); var visitCounter = parseInt(item.get_item("VisitTimes")); visitCounter++; ShowCounterOnHome(visitCounter); var updateItem = countList.getItemById(itemID); updateItem.set_item('VisitTimes', visitCounter); updateItem.update(); curContext.executeQueryAsync();
七、文件夹创建
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
function createFolder() { targetList = list.getByTitle("MyDocumentLibrary"); itemCreation = new SP.ListItemCreationInformation(); itemCreation.set_underlyingObjectType(SP.FileSystemObjectType.folder); itemCreation.set_leafName("MyCustomFolder"); var folderItem = targetList.addItem(itemCreation); folderItem.update(); context.load(folderItem); context.executeQueryAsync(onFolderCreationSuccess, onFolderCreationFail); }