SharePoint 2013的REST编程基础
1. SharePoint 2013對REST編程的支持
自從SharePoint2013開始, SharePoint開始了對REST 編程的支持,這樣除了.NET , Silverlight, Powershell之外, 又多了一種可以和SharePoint Server進(jìn)行CSOM編程的方式。那么,問題來了:什么是REST呢?什么是ODATA?為什么這么多產(chǎn)品都開始支持這個(gè)了?
?
2. 什么是REST & ODATA?
如果這個(gè)世界上只有一家IT公司就好了,這樣就不需要REST了 :) 但是很顯然,這是不可能的。不同IT公司有自己提出的技術(shù)標(biāo)準(zhǔn)及解決方案。以Microsoft為例,其實(shí)現(xiàn)分布式處理的技術(shù)和解決方案發(fā)展歷程如下:COM -> DCOM -> COM+ ,跨入.NET時(shí)代,又出現(xiàn)了 Enterprise Component -> Web Service -> Web Service Enhancemant -> WCF ...
可以看到,僅僅一家公司,其提出的技術(shù)和解決方案就如此之多。在加上IBM, Google等等大鱷,就形成了各自為政,群雄混戰(zhàn)的局面。這樣的解決就是:在各自的系統(tǒng)和技術(shù)內(nèi),大家各自搞自己的。很顯然這是不符合時(shí)代發(fā)展的 :) 比如沒有任何一家公司,只用某一家IT公司的解決方案。不同公司的系統(tǒng)之間,也需要互相通訊。
?
那怎么解決這個(gè)問題呢?大家握手言和吧:制定一個(gè)基于HTTP協(xié)議的標(biāo)準(zhǔn)。HTTP協(xié)議是目前為止,最通用和成功的協(xié)議。如果某個(gè)公司說我就是不遵守HTTP協(xié)議,那他就沒法轉(zhuǎn)了。 因此,只要大家遵循這個(gè)標(biāo)準(zhǔn),系統(tǒng)之間都可以互相通信。如果利用Microsoft的技術(shù)開發(fā)了一個(gè)服務(wù),只要這個(gè)服務(wù)遵循這個(gè)標(biāo)準(zhǔn),采用IBM技術(shù)的客戶也可以consume這個(gè)服務(wù),那不是很好嗎? 因此這就是REST服務(wù)出現(xiàn)的現(xiàn)實(shí)需要。
?
言歸正傳,什么是REST 服務(wù)呢?
REST的全稱是:?Representational state transfer?(有點(diǎn)繞口 :)), 她的基本思想是:可以通過基于HTTP協(xié)議發(fā)出的不同 操作:Create,?Read,?Update, and?Delete (CRUD), 來實(shí)現(xiàn)對數(shù)據(jù)的操作。其有三大基本特征:
1) 客戶端- 服務(wù)端:
?所以數(shù)據(jù)都存儲在服務(wù)端,客戶端不知道也無需知道服務(wù)端如何處理的,只需要發(fā)送一個(gè)HTTP請求。
2) 無狀態(tài):
這個(gè)比較抽象。打個(gè)比方:
你申明一個(gè)proxy去調(diào)用服務(wù)器端的求和函數(shù) proxy.sum(1,2) ,服務(wù)器端返回3給客戶端。 當(dāng)你再次調(diào)用proxy.sum(3,4)的時(shí)候,如果是無狀態(tài)的話,其是不會記住你上次操作的結(jié)果的,也就是其會返回7;但是如果是有狀態(tài)的操作,其會記錄上一次調(diào)用的結(jié)果,并把這次結(jié)果累加再返回給客戶端,也就是3+7=10.
3)可緩存的
4)分層的
5) 統(tǒng)一的接口
以下是關(guān)于REST的Wiki解釋:
http://en.wikipedia.org/wiki/Representational_state_transfer
?
3. 如何實(shí)現(xiàn)一個(gè)RESTful服務(wù)
實(shí)現(xiàn)一個(gè)RESTful服務(wù)的方案有很多。以Microsoft的.NET解決方案為例, WCF3.5就開始了對REST服務(wù)的支持。你可以用WCF輕松創(chuàng)建一個(gè) WCF REST API服務(wù)供其它客戶端consume.
有關(guān)如何通過WCF 實(shí)現(xiàn)一個(gè)REST服務(wù),可以參考下面的文檔:
https://msdn.microsoft.com/en-us/magazine/dd315413.aspx
?
4 如何過濾和選擇數(shù)據(jù)
如何讓服務(wù)器返回自己真正想要的數(shù)據(jù)呢? 其實(shí)ODATA提供了豐富的數(shù)據(jù)過濾和選擇操作:
1)$filter : 過濾數(shù)據(jù)
2) $select : 選擇需要的字段
3) $expand: 擴(kuò)展返回的屬性
4) $orderby:排序
5) $top:返回前N條數(shù)據(jù)
?
5. SharePoint 2013對REST服務(wù)的支持
SharePoint 2013 實(shí)現(xiàn)了REST服務(wù)的支持,這樣我們又多了一種途徑可以訪問SharePoint了。實(shí)際上SharePoint的REST服務(wù)是一個(gè)通過叫做client.svc的WCF REST服務(wù)來實(shí)現(xiàn)的。其URL為:https://sharepointserver/_vti_bin/client.svc
1) SharePoint 2013的REST服務(wù)部署的路徑如下:
https://servername/sitename/_api/....
實(shí)際上上面這個(gè)地址只是其原始服務(wù)的一個(gè)別名(alias), 畢竟打出完成的client.svc有點(diǎn)丑陋哦。
比如:
https://winstononline.sharepoint.com/_api/...
https://winstononline.sharepoint.com/sites/spdev/...
?
2) SharePoint 2013支持的命名空間:
要對SharePoint 2013發(fā)出一個(gè)REST請求,必須指明你所要訪問的namespace。支持的namespace包括:
?? a) _api/web
?? b) _api/site
?? c) _api/search
?? d) _api/publishing
?
例如: 我想返回一個(gè)叫做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客戶端技術(shù)對REST服務(wù)的支持
如果采用上面發(fā)送REST請求的方法來和SharePoint進(jìn)行交互,可以很容易的執(zhí)行各種操作。但是唯一的確定是沒法進(jìn)行批量化的操作。為了讓客戶端采用javascript來調(diào)用SharePoint REST的時(shí)候更加友好和強(qiáng)大,SharePoint提供了2個(gè)javascript類庫,分別為sp.js和sp.runtime.js, 其位于_layouts/15/###.js下。與開發(fā)者相關(guān)的,更多的是sp.js里面的定義。這樣所有的REST操作都被封裝在sp.js里定義的API 里面,顯得更加友好。
下面是通過Napa創(chuàng)建的一個(gè)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 informationfunction 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 namefunction onGetUserNameSuccess() {$('#message').text('Hello ' + user.get_title() +"This is my first Napa app");}// This function is executed if the above call failsfunction onGetUserNameFail(sender, args) {alert('Failed to get user name. Error:' + args.get_message());}})();function getParameterByName(name) {}6. 調(diào)用SharePoint REST服務(wù)示例
未完待續(xù)...
總結(jié)
以上是生活随笔為你收集整理的SharePoint 2013的REST编程基础的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2TB不到900元 美光原厂闪存SSD硬
- 下一篇: 【转】.net框架读书笔记---CLR内