尽管我们可以使用客户端对象模型进行SharePoint开发,但SharePoint 2013开始,提供了rest api来更方便的开发。

    1. ajax请求获得list的数据

     比如如果我们想要查询一个list的数据,我们可以这样:

 

  1.  $.ajax({
  2.  type: "GET",
  3.  url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('" + docLibraryName + "')/items",
  4.  headers:{
  5.  "accept": "application/json;odata=verbose"
  6.  },
  7.  success: function (data) {
  8.  console.log(data.d);
  9.  },
  10.  error: function (err) {
  11.  }
  12.  });


    这样是不是比客户端对象模型更方便?

 

    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的这一些基本操作外,还有其他更多的操作。

posted on 2021-02-08 18:54  hanrend  阅读(217)  评论(0)    收藏  举报