首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 其他相关 >

SharePoint 2013 Odata 惯用实例和基本操作

2013-10-12 
SharePoint 2013 Odata 常用实例和基本操作本文讲述SharePoint 2013 Odata 常用实例和基本操作。Open Data

SharePoint 2013 Odata 常用实例和基本操作

本文讲述SharePoint 2013 Odata 常用实例和基本操作。

Open Data Protocol (OData)是一个基于Rest风格的数据服务,同过这个服务可以使用同一的URI定位到具体的资源(文件和记录等),从而使得这些资源可以使用HTTP请求进行增删改查,它定义了一系列的规则,使得资源更容易被定位和操作。

首先看一下OData定义的URI结构:

SharePoint 2013 Odata 惯用实例和基本操作


那么相对SharePoint 2013,一个简单的实例为:

http://moss2013site/_api/web/lists/GetByTitle('News')/items?$select=Title,Body,ID&$top=10&$orderby=Created desc

这个URI表示查询News 列表里面最新的10条记录,查询的字段为Title,Body,ID

http://moss2013site/_api/web 是service root URI

/lists/GetByTitle('News')/items 是resource path

?$select=Title,Body,ID&$top=10&$orderby=Created desc 是Query option, $select=Title,Body,ID表示选择Title,Body,ID三个字段,$top=10表示选择前10条记录,$orderby=Created desc表示按创建时间倒序排列

service root URI 和 resource path相对简单和容易理解,这里我们用一张来自微软网站http://msdn.microsoft.com/en-us/library/office/apps/fp142385.aspx的图来说明下Query option(注意降序排列不时dsc,应该是desc,如果你使用dsc将会报错),目前SharePoint 2013 的Odata service支持以下查询选项:

SharePoint 2013 Odata 惯用实例和基本操作

SharePoint 2013 的Odata service的常用URI实例:

URI实例解释_api/web/title返回web title _api/web/lists(guid'<list id>')返回列表_api/web/lists/getByTitle('Announcements')/fields返回列表的所有字段_api/web/lists/getByTitle('Task')/items返回Tas列表的所有item_api/web/siteusers返回该站点所有用户_api/web/sitegroups返回该站点所有用户组_api/web/sitegroups(3)/users返回该站点所有在id为3的用户组的用户_api/web/GetFolderByServerRelativeUrl('/Shared Documents')返回Shared Documents文档库根目录_api/web/GetFolderByServerRelativeUrl('/Plans')/Files('a.txt')/$value返回Plans文档库根目录下的a.txt文件带Query option的实例:

查询选项作用$select指定返回字段(列)$filter指定过滤条件$expand用于关联列表,指定返回关联字段(列)$top指定返回前N条记录$skip指定返回结果跳过前N行记录$orderby指定按某个字段排序URI实例解释

_api/web/lists/getByTitle(  'Books')/items?$select=Title,PublishedBy/Name&$expand=PublishedBy
前提book里面有个
PublishedBy 的字段关联到Publisher 列表
返回Books列表的Title和关联
Publisher 列表项目的Name
_api/web/lists/getByTitle('Books')/items?$top=10&$skip=2
返回Books列表的3~10项纪录
_api/web/lists/getByTitle('Books')/items?$skip=2&$top=10
返回Books列表的3~12项纪录
_api/web/lists/getByTitle('Books')/items?$orderby=ID desc&$top=2
返回Books列表的最后两条记录
_api/web/lists/getByTitle(  'Books')/items?$select=Title&$filter=Author eq 'Mark Twain'&$top=2
返回Author 等于
 'Mark Twain'的头两条记录
_api/web/lists/getByTitle('Books')/items?$orderby=Title asc
返回Books列表的所有记录并按Title升序排列
_api/web/lists/getByTitle('Books')/items?$select=Author,Title,ISBN
返回Books列表下记录(只返回列
Author,Title,ISBN


使用JQuery + Odata实现增删改查

按Json格式查询:

function onUpdateCustomer(customerId, firstName) {  // first we need to fetch the eTag to ensure we have the most recent value for it  var requestUri = _spPageContextInfo.webAbsoluteUrl +                "/_api/Web/Lists/getByTitle('Customers')/items(" + customerId + ")";   // execute AJAX request  $.ajax({    url: requestUri,    type: "GET",    headers: { "ACCEPT": "application/json;odata=verbose" },    success: function(data){      // got the eTag      var etag = data.d.etag;        var requestUri = _spPageContextInfo.webAbsoluteUrl +                "/_api/Web/Lists/getByTitle('Customers')/items(" + customerId + ")";            // set the MERGE verb so we only need to provide the update delta      var requestHeaders = {        "ACCEPT": "application/json;odata=verbose",        "X-RequestDigest": $("#__REQUESTDIGEST").val(),        "X-HTTP-Method": "MERGE",        "If-Match": etag      }        var customerData = {        __metadata: { "type": "SP.Data.CustomersListItem" },        FirstName: firstName      };        requestBody = JSON.stringify(customerData);        $.ajax({        url: requestUri,        type: "POST",        contentType: "application/json;odata=verbose",        headers: requestHeaders,        data: requestBody,        success: function(data){          alert("Updated customer");        },        error: function(data){          alert("Failed to update customer");        }      });    },    error:  function(data){      alert("Failed to get customer eTag");    }  });}


更多参考:

OData: http://www.odata.org/documentation/odata-v2-documentation/uri-conventions/

Use OData query operations in SharePoint:http://msdn.microsoft.com/en-us/library/office/apps/fp142385.aspx

Understanding and Using the SharePoint 2013 REST Interface:http://msdn.microsoft.com/en-us/magazine/dn198245.aspx

热点排行