Rest API 操作List Items
获取所有的List Items
function getItems(url) {
$.ajax({
url: url,
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
},
success: function(data) {
//console.log(data);
for(var i=0;i<data.d.results.length;i++){
console.log(data.d.results[i].Number);
}
if (data.d.__next) {
getItems(data.d.__next);
}
},
error: function(jqxhr) {
alert(jqxhr.responseText);
}
});
}
getItems(_spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Association')/items");
删除所有的List Items: (获取所有的list Items可以使用上面的方法)
function deleteItem(url,prm) {
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + url,
type: "DELETE",
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"If-Match": "*"
},
success: function (data) {
console.log(prm);
},
error: function (error) {
alert(JSON.stringify(error));
}
});
}
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Association')/items?$filter=Number%20ne%20%27120%27&$top=2000",
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
},
success: function (data) {
var items = data.d.results;
console.log(items.length);
for(var i=0;i<items.length;i++){
var url = "/_api/Web/Lists/getByTitle('Association')/getItemById("+items[i].ID+")";
deleteItem(url,items[i].ID);
}
},
error: function (error) {
alert(JSON.stringify(error));
}
});
删除单个:
function deleteListItem() {
var id = $("#txtId").val();
var siteUrl = _spPageContextInfo.webAbsoluteUrl;
var fullUrl = siteUrl + "/_api/web/lists/GetByTitle('MyCompanyInfo')/items(" + id + ")";
$.ajax({
url: fullUrl,
type: "POST",
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "DELETE",
"IF-MATCH": "*"
},
success: onQuerySucceeded,
error: onQueryFailed
});
}
function onQuerySucceeded(sender, args) {
$("#divResult").html("Item successfully deleted!");
}
function onQueryFailed() {
alert('Error!');
}
添加List Items:
function addListItem() {
var title = $("#txtTitle").val();
var industry = $("#txtIndustry").val();
var siteUrl = _spPageContextInfo.webAbsoluteUrl;
var fullUrl = siteUrl + "/_api/web/lists/GetByTitle('MyAssociation')/items";
var itemType = GetItemTypeForListName(listName);
$.ajax({
url: fullUrl,
type: "POST",
data: JSON.stringify({
'__metadata': { 'type': itemType },//'SP.Data.MyAssociationListItem'
'Title': title,
'Industry': industry
}),
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: onQuerySucceeded,
error: onQueryFailed
});
}
function onQuerySucceeded(sender, args) {
$("#divResult").html("Item successfully added!");
}
function onQueryFailed() {
alert('Error!');
}
// Get List Item Type metadata
function GetItemTypeForListName(name) {
return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
}
Update List Items:
ExecuteOrDelayUntilScriptLoaded(initializePage, "sp.js");
function initializePage() {
var siteURL;
var itemsArray = [];
// This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
$(document).ready(function() {
var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
UpdateListItem();
});
}
//Retrieve list items from sharepoint using API
function UpdateListItem() {
siteURL = _spPageContextInfo.webAbsoluteUrl;
console.log("from top nav - " + siteURL);
var apiPath = siteURL + "/_api/lists/getbytitle(''samplelist'')/items/getbyid(1)";
$.ajax({
url: apiPath,
type: "POST",
headers: {
Accept: "application/json;odata=verbose"
},
data: "{__metadata:{'type':'SP.Data.YourlistnameListItem'},Title:”Ur input "
}
",
async: false, success: function(data) {
alert("Item updated successfully");
}, eror: function(data) {
console.log("An error occurred. Please try again.");
}
});
}
How to update List Item via SharePoint REST interface:
function updateJson(endpointUri,payload, success, error)
{
$.ajax({
url: endpointUri,
type: "POST",
data: JSON.stringify(payload),
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest" : $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "MERGE",
"If-Match": "*"
},
success: success,
error: error
});
} function getItemTypeForListName(name) {
return"SP.Data." + name.charAt(0).toUpperCase() + name.slice(1) + "ListItem";
} function updateListItem(webUrl,listTitle,listItemId,itemProperties,success,failure)
{
var listItemUri = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items(" + listItemId + ")";
var itemPayload = {
'__metadata': {'type': getItemTypeForListName(listTitle)}
};
for(var prop in itemProperties){
itemPayload[prop] = itemProperties[prop];
}
updateJson(listItemUri,itemPayload,success,failure);
} Usage: var itemProperties = {'Title':'John Doe'};
updateListItem(_spPageContextInfo.webAbsoluteUrl,'Contacts',1,itemProperties,printInfo,logError);
function printInfo()
{
console.log('Item has been created');
}
function logError(error){
console.log(JSON.stringify(error));
}
获取所有的List Items Count:
_spPageContextInfo.webAbsoluteUrl+" _api/web/lists/getByTitle('DepartProd')/itemcount" //?$filter=AssignedTo/ID eq 2
Rest API 操作List Items的更多相关文章
- Python API 操作Hadoop hdfs详解
1:安装 由于是windows环境(linux其实也一样),只要有pip或者setup_install安装起来都是很方便的 >pip install hdfs 2:Client——创建集群连接 ...
- 转 用C API 操作MySQL数据库
用C API 操作MySQL数据库 参考MYSQL的帮助文档整理 这里归纳了C API可使用的函数,并在下一节详细介绍了它们.请参见25.2.3节,“C API函数描述”. 函数 描述 mysql_a ...
- hive-通过Java API操作
通过Java API操作hive,算是测试hive第三种对外接口 测试hive 服务启动 package org.admln.hive; import java.sql.SQLException; i ...
- Hadoop学习记录(3)|HDFS API 操作|RPC调用
HDFS的API操作 URL方式访问 package hdfs; import java.io.IOException; import java.io.InputStream; import java ...
- HBase 6、用Phoenix Java api操作HBase
开发环境准备:eclipse3.5.jdk1.7.window8.hadoop2.2.0.hbase0.98.0.2.phoenix4.3.0 1.从集群拷贝以下文件:core-site.xml.hb ...
- hadoop2-HBase的Java API操作
Hbase提供了丰富的Java API,以及线程池操作,下面我用线程池来展示一下使用Java API操作Hbase. 项目结构如下: 我使用的Hbase的版本是 hbase-0.98.9-hadoop ...
- 使用Java API操作HDFS文件系统
使用Junit封装HFDS import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org ...
- HBase API操作
|的ascII最大ctrl+shift+t查找类 ctrl+p显示提示 HBase API操作 依赖的jar包 <dependencies> <dependency> < ...
- MSComm控件与Win32 API操作串口有何区别?
MSComm控件与Win32 API操作串口有何区别? [问题点数:50分,结帖人shell_shell] 收藏帖子 回复 我是一个小兵,在战场上拼命! 结帖率 83.33% 我以前用MSCo ...
随机推荐
- HTML基础之JS中的字符转义--转义中文或特殊字符
1.在标准的url的规范中是不允许出现中文字符或某些特殊字符的,所以要进行转义 2.& 代表参数的链接,如果就是想传& 给后端那么必须转义 decodeURI(url) URl中未转义 ...
- ubuntu 下开机启动项修复(进不去windows系统)
1.终端输入: sudo gedit /etc/default/grub 2.更改: GRUB_DEFAULT=0 改为 GRUB_DEFAULT=4 GRUB_TIMEOUT=10 改为 ...
- TP5架构下链接SQL数据库的一种方法
1.database设置 2.连接到所需要的表格 *.数据库目录
- input 上报流程图
input 上报流程图 http://blog.chinaunix.net/uid-28320320-id-3389196.html
- numpy数组取每一列的数据
也可以运用到列表中,a原本是一个列表的嵌套,将a转为了数组进行此操作,可以取固定的值,这就是numpy的好处.
- Linux性能调优之gprof和oprofile
为了更好的优化程序性能,我们必须找到性能瓶颈点,“好钢用在刀刃上”才能取 得好的效果,否则可能白做工作. 为了找到关键路径,我们可以使用profilng技术,在linux平台上,我们可以使用gprof ...
- MQTT控制---pingreq
心跳请求 客户端向服务端发送PINGREQ报文用于: 在没有任何其他控制报文从client发给server时,告诉server,client还活着 请求server发送 响应确认它还活着 使用网络以确 ...
- LR使用web_add_cookie函数进行cookie模拟
1 为什么要使用cookie模拟 从日常项目测试过程中的问题说起.比如要进行论坛中的文件下载功能的测试.我们都知道只有登录用户才能进行下载操作,这样我们的测试过程可能就变成了先登录系统,然后再进 ...
- OpenStack--ntp组件时间同步服务
作用:ntp主要是用于对计算机的时间同步管理操作 环境: 服务端: 192.168.245.172 客户端: 192.168.245.171 时间是对服务器来说是很重要的,一般很多网站都需要读取服务器 ...
- SpringBoot 整合 Redis缓存
在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. Spr ...