尽管我们可以使用客户端对象模型进行SharePoint开发,但SharePoint 2013开始,提供了rest api来更方便的开发。
1. ajax请求获得list的数据
比如如果我们想要查询一个list的数据,我们可以这样:
-
$.ajax({
-
type:
"GET",
-
url: _spPageContextInfo.webAbsoluteUrl +
"/_api/web/lists/getByTitle('" + docLibraryName +
"')/items",
-
headers:{
-
"accept":
"application/json;odata=verbose"
-
},
-
success:
function (data) {
-
console.log(data.d);
-
},
-
error:
function (err) {
-
}
-
});
这样是不是比客户端对象模型更方便?
2. $select
上面是查询list的所有字段,要是我们只想查询特定的字段呢?我们可以使用$select
将上面的url的值改为_spPageContextInfo.webAbsoluteUrl +"/_api/web/lists/getByTitle('" + docLibraryName +"')/items?$select=FiledName1,FiledName2
3.$filter
尽管能查询特定的字段,但我们还想查询满足某一些特定条件的数据,我们可以使用$filter。
将url的值改为_spPageContextInfo.webAbsoluteUrl +"/_api/web/lists/getByTitle('" + docLibraryName +"')/items?$select=FiledName1,FiledName2&$filter=FiledName1 eq 'Your Value'
其中eq是等于的英文缩写,我相信大于,小于等的英文缩写以此类似。
4.$expand
假如现在我们有两个list:listA和listB,listA的一个字段FiledName2通过lookup和listB建立连接关系。如果这个时候我们在查询listA时,想要查询listB的其他值,这个时候我们可以是$expand。
将上面的url改为_spPageContextInfo.webAbsoluteUrl +"/_api/web/lists/getByTitle('listA')/items?$select=FiledName1,FiledName2/Title,FiledName2/ID&$filter=FiledName1 eq 'Your Value'&$expand=FiledName2
5.$orderby
如果我们想要对查出的list进行排序,我们可以使用$orderby。
6. $top
如果我们想要查询特定数量的值,我们可以使用$top。
7.$skip
如果我们想从第n条数据开始查询,我们可以使用$skip。
8.其他
当然除了对list的这一些基本操作外,还有其他更多的操作。