1. SharePoint 2013對REST編程的支持
自從SharePoint2013開始, SharePoint開始了對REST 編程的支持,這樣除了.NET , Silverlight, Powershell之外, 又多了一種可以和SharePoint Server進行CSOM編程的方式。那么,問題來了:什么是REST呢?什么是ODATA?為什么這么多產品都開始支持這個了?
2. 什么是REST & ODATA?
如果這個世界上只有一家IT公司就好了,這樣就不需要REST了 :) 但是很顯然,這是不可能的。不同IT公司有自己提出的技術標准及解決方案。以Microsoft為例,其實現分布式處理的技術和解決方案發展歷程如下:COM -> DCOM -> COM+ ,跨入.NET時代,又出現了 Enterprise Component -> Web Service -> Web Service Enhancemant -> WCF ...
可以看到,僅僅一家公司,其提出的技術和解決方案就如此之多。在加上IBM, Google等等大鱷,就形成了各自為政,群雄混戰的局面。這樣的解決就是:在各自的系統和技術內,大家各自搞自己的。很顯然這是不符合時代發展的 :) 比如沒有任何一家公司,只用某一家IT公司的解決方案。不同公司的系統之間,也需要互相通訊。
那怎么解決這個問題呢?大家握手言和吧:制定一個基於HTTP協議的標准。HTTP協議是目前為止,最通用和成功的協議。如果某個公司說我就是不遵守HTTP協議,那他就沒法轉了。 因此,只要大家遵循這個標准,系統之間都可以互相通信。如果利用Microsoft的技術開發了一個服務,只要這個服務遵循這個標准,采用IBM技術的客戶也可以consume這個服務,那不是很好嗎? 因此這就是REST服務出現的現實需要。
言歸正傳,什么是REST 服務呢?
REST的全稱是: Representational state transfer (有點繞口 :)), 她的基本思想是:可以通過基於HTTP協議發出的不同 操作:Create, Read, Update, and Delete (CRUD), 來實現對數據的操作。其有三大基本特征:
1) 客戶端- 服務端:
所以數據都存儲在服務端,客戶端不知道也無需知道服務端如何處理的,只需要發送一個HTTP請求。
2) 無狀態:
這個比較抽象。打個比方:
你申明一個proxy去調用服務器端的求和函數 proxy.sum(1,2) ,服務器端返回3給客戶端。 當你再次調用proxy.sum(3,4)的時候,如果是無狀態的話,其是不會記住你上次操作的結果的,也就是其會返回7;但是如果是有狀態的操作,其會記錄上一次調用的結果,並把這次結果累加再返回給客戶端,也就是3+7=10.
3)可緩存的
4)分層的
5) 統一的接口
以下是關於REST的Wiki解釋:
http://en.wikipedia.org/wiki/Representational_state_transfer
3. 如何實現一個RESTful服務
實現一個RESTful服務的方案有很多。以Microsoft的.NET解決方案為例, WCF3.5就開始了對REST服務的支持。你可以用WCF輕松創建一個 WCF REST API服務供其它客戶端consume.
有關如何通過WCF 實現一個REST服務,可以參考下面的文檔:
https://msdn.microsoft.com/en-us/magazine/dd315413.aspx
4 如何過濾和選擇數據
如何讓服務器返回自己真正想要的數據呢? 其實ODATA提供了豐富的數據過濾和選擇操作:
1)$filter : 過濾數據
2) $select : 選擇需要的字段
3) $expand: 擴展返回的屬性
4) $orderby:排序
5) $top:返回前N條數據
5. SharePoint 2013對REST服務的支持
SharePoint 2013 實現了REST服務的支持,這樣我們又多了一種途徑可以訪問SharePoint了。實際上SharePoint的REST服務是一個通過叫做client.svc的WCF REST服務來實現的。其URL為:https://sharepointserver/_vti_bin/client.svc
1) SharePoint 2013的REST服務部署的路徑如下:
https://servername/sitename/_api/....
實際上上面這個地址只是其原始服務的一個別名(alias), 畢竟打出完成的client.svc有點丑陋哦。
比如:
https://winstononline.sharepoint.com/_api/...
https://winstononline.sharepoint.com/sites/spdev/...
2) SharePoint 2013支持的命名空間:
要對SharePoint 2013發出一個REST請求,必須指明你所要訪問的namespace。支持的namespace包括:
a) _api/web
b) _api/site
c) _api/search
d) _api/publishing
例如: 我想返回一個叫做Doctest的list里面ID為3的item,並且只返回ID字段:
http://winstononline.sharepoint.com/_api/web/lists/Doctest/items?$filter=ID eq 3 & $select=ID
又如:
https://winstononline.sharepoint.com/_api/web/lists/Doctest/items?$select=ID & $orderby=ID
<?xml version="1.0" encoding="utf-8" ?> - <feed xml:base="https://winstononline.sharepoint.com/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"> <id>b3c0e660-6937-4da9-b1cf-c1bd6613be1a</id> <title /> <updated>2015-02-04T07:22:31Z</updated> - <entry m:etag=""3""> <id>bccd25b6-c6cf-4748-a75b-aca5b6826fe8</id> <category term="SP.Data.DoctestItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> <link rel="edit" href="Web/Lists(guid'906b7bcc-bd4f-4d8c-9e46-6e01b5579d5f')/Items(1)" /> <title /> <updated>2015-02-04T07:22:31Z</updated> - <author> <name /> </author> - <content type="application/xml"> - <m:properties> <d:Id m:type="Edm.Int32">1</d:Id> <d:ID m:type="Edm.Int32">1</d:ID> </m:properties> </content> </entry> - <entry m:etag=""4""> <id>7604db11-97fa-4433-b652-51eb70b013d9</id> <category term="SP.Data.DoctestItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> <link rel="edit" href="Web/Lists(guid'906b7bcc-bd4f-4d8c-9e46-6e01b5579d5f')/Items(2)" /> <title /> <updated>2015-02-04T07:22:31Z</updated> - <author> <name /> </author> - <content type="application/xml"> - <m:properties> <d:Id m:type="Edm.Int32">2</d:Id> <d:ID m:type="Edm.Int32">2</d:ID> </m:properties> </content> </entry> - <entry m:etag=""2""> <id>ec2a5b46-ae8f-4334-8a85-9a777660021c</id> <category term="SP.Data.DoctestItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> <link rel="edit" href="Web/Lists(guid'906b7bcc-bd4f-4d8c-9e46-6e01b5579d5f')/Items(3)" /> <title /> <updated>2015-02-04T07:22:31Z</updated> - <author> <name /> </author> - <content type="application/xml"> - <m:properties> <d:Id m:type="Edm.Int32">3</d:Id> <d:ID m:type="Edm.Int32">3</d:ID> </m:properties> </content> </entry> </feed>
6. SharePoint客戶端技術對REST服務的支持
如果采用上面發送REST請求的方法來和SharePoint進行交互,可以很容易的執行各種操作。但是唯一的確定是沒法進行批量化的操作。為了讓客戶端采用javascript來調用SharePoint REST的時候更加友好和強大,SharePoint提供了2個javascript類庫,分別為sp.js和sp.runtime.js, 其位於_layouts/15/###.js下。與開發者相關的,更多的是sp.js里面的定義。這樣所有的REST操作都被封裝在sp.js里定義的API 里面,顯得更加友好。
下面是通過Napa創建的一個default sharePoint App, 里面就用到了SP.js。
<%-- The following 4 lines are ASP.NET directives needed when using SharePoint components --%> <%@ Page Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" MasterPageFile="~masterurl/default.master" Language="C#" %> <%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%-- The markup and script in the following Content element will be placed in the <head> of the page --%> <asp:Content ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server"> <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js"></script> <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script> <script type="text/javascript" src="/_layouts/15/sp.js"></script> <!-- Add your CSS styles to the following file --> <link rel="Stylesheet" type="text/css" href="../Content/App.css" /> <!-- Add your JavaScript to the following file --> <script type="text/javascript" src="../Scripts/App.js"></script> </asp:Content> <%-- The markup in the following Content element will be placed in the TitleArea of the page --%> <asp:Content ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server"> Page Title </asp:Content> <%-- The markup and script in the following Content element will be placed in the <body> of the page --%> <asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server"> <div> <p id="message"> <!-- The following content will be replaced with the user name when you run the app - see App.js --> initializing... </p> </div> </asp:Content> <!--app.js--> var context = SP.ClientContext.get_current(); var user = context.get_web().get_currentUser(); (function () { // 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 () { getUserName(); }); // This function prepares, loads, and then executes a SharePoint query to get // the current users information function getUserName() { context.load(user); context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail); } // This function is executed if the above call is successful // It replaces the contents of the 'message' element with the user name function onGetUserNameSuccess() { $('#message').text('Hello ' + user.get_title() +"This is my first Napa app"); } // This function is executed if the above call fails function onGetUserNameFail(sender, args) { alert('Failed to get user name. Error:' + args.get_message()); } })(); function getParameterByName(name) { }
6. 調用SharePoint REST服務示例
未完待續...