天天看點

[sharepoint]Rest api相關知識(轉)

最近又開始弄rest api了,通過sharepoint rest api擷取站點資訊,items,fields非常友善,再結合odata查詢,更是得心應手。這裡記錄學習的時候用到的知識點,以及查詢的資料。

在可以使用 rest 服務通路 sharepoint 資源之前,首先必須知道指向該資源的 uri 端點。隻要可能,這些 rest 端點的 uri 就會準确地模仿 sharepoint 用戶端對象模型中資源的 api 簽名。例如:

用戶端對象模型方法:            

list.getbytitle(listname).getitems()

rest 端點:            

http://server/site/_api/lists/getbytitle('listname')/items            

但是,在某些情況下,為了遵守 rest 或 odata 約定,端點 uri 會不同于相應的用戶端對象模型簽名。

下圖顯示 sharepoint rest uri 的通用文法結構。

sharepoint rest uri 文法結構

[sharepoint]Rest api相關知識(轉)

sharepoint 資源的部分端點偏離了這種文法結構:

需要複雜類型作為參數的方法。

如果對應的用戶端對象模型方法要求複雜類型作為參數傳遞,則 rest 端點可能偏離此文法構造說明 rest 限制。

靜态方法和屬性。

rest 端點偏離代表靜态方法和屬性的 uri 的文法結構。

确定 sharepoint 2013 rest 服務端點

若要為 sharepoint 資源構造 rest 端點,請按照以下步驟執行操作:

從 rest 服務引用開始:

http://server/site/_api                

指定合适的入口點。例如:

http://server/site/_api/web                

從入口點導航到您要通路的特定資源。這包括為與用戶端對象模型中的方法對應的端點指定參數。例如:

http://server/site/_api/web/lists/getbytitle('listname')                

引用端點 uri 中的 sharepoint 2013 rest 服務

使用 _api 來表示端點 uri 中的 sharepoint 2013 rest 服務。rest 服務屬于 client.svc web 服務的一部分。但是,要盡早構造 rest uri 以及縮短基礎 rest uri 路徑,rest 服務使用 _api 将顯式引用 client.svc web 服務的需求抽象出來。rest 服務将承認并接受引用 client.svc web 服務的 uri。例如,您可以使用 http://server/site/_vti_bin/client.svc/web/lists 來代替http://server/site/_api/web/lists。但是,使用 _api 是首選慣例。url 限制為 256 個字元,是以,使用 _api 可以縮短基礎 uri,以留下更多的字元用于構造剩餘 url。

rest 服務的主要入口點表示網站集合以及指定上下文的網站。這樣,這些入口點與用戶端對象模型中的 clientcontext.site 屬性和 clientcontext.web 屬性對應。

要通路特定的網站集合,請使用以下構造:

http://server/site/_api/site              

要通路特定的網站,請使用以下構造:

http://server/site/_api/web              

其中 server 表示伺服器的名稱,site 表示特定網站的名稱或路徑。

除 /site 和 /web 外,rest 服務包括幾個其他通路點,通過這些通路點,開發人員可導航至特定功能。下表列出了部分通路點。

功能區域

通路點

網站

http://server/site/_api/site

web

http://server/site/_api/web

使用者配置檔案

http:// server/site/_api/sp.userprofiles.peoplemanager

搜尋

http:// server/site/_api/search

釋出

http:// server/site/_api/publishing

導航到您要通路的特定資源

從這裡,通過周遊對象模型并使用用斜杠分隔的用戶端對象模型中 api 的名稱構造多個特定 rest 端點。下表顯示用戶端對象模型調用及等效 rest 端點示例。

用戶端對象模型 api

rest 端點

clientcontext.web.lists

http://server/site/_api/web/lists

clientcontext.web.lists[guid]

http://server/site/_api/web/lists(‘guid’)

clientcontext.web.lists.getbytitle("title")

http://server/site/_api/web/lists/getbytitle(‘title’)

終結點 uri 不區分大小寫。例如,在上表中,使用 /getbytitle 指定 getbytitle() 方法的 rest 等效項。

sharepoint 2013 擴充了 odata 規範,允許您使用括号來指定方法參數和索引值。這可防止包含多個名稱相同參數的 uri 中的潛在混淆問題。例如,下面兩個 uri 包含名稱相同的參數:

http://server/site/_api/web/lists/getbytitle('announcements')/fields/getbytitle('description')            

http://server/site/_api/web/lists('<guid>')/fields/getbyid('<guid>')            

要指定多個參數,請将參數作為名稱/值對包含在内,并用逗号将參數分隔。例如:

http://server/site/_api/web/getavailablewebtemplates(lcid=1033, includecrosslanguage=true)            

下圖顯示了 sharepoint rest 參數文法。

sharepoint rest 參數文法

[sharepoint]Rest api相關知識(轉)

些方法要求大的有效載荷作為參數。對于要與其對應用戶端對象模型 api 保持功能平衡的 rest 端點,這些端點必須接受複雜類型作為參數。在這種情況下,rest 服務擴充了現有 odata 協定,允許這些 rest 端點接受單個複雜類型作為參數。這僅适用于 post 操作,并且您必須根據 odata 标準以 atom 格式或 json 格式傳遞複雜類型。

例如,listcollection.add 方法以 microsoft.sharepoint.client.listcreationinformation 對象作為參數。要将清單添加到指定網站,請按如下方式構造相應的 rest 端點:

http://server/site/_api/web/lists/add              

然後,在請求正文中傳遞複雜類型,此處使用 json 進行格式設定。

[sharepoint]Rest api相關知識(轉)
[sharepoint]Rest api相關知識(轉)

在 rest 服務調用中使用參數别名

您可以在 odata 中使用"參數别名"語義将參數傳遞到 sharepoint rest 端點。在參數别名中,用參數調用中的别名辨別參數值,而實際值則在 uri 的查詢字元串中指定。這允許您通過使用查詢字元串支援多種類型的字元和一緻的格式。

例如,以下兩個 rest uri 為等效項:

直接指定參數值:              

http://server/site/_api/web/applywebtemplate("sts#0")              

使用參數别名,并在 uri 的查詢字元串中指定實際參數值:              

http://server/site/_api/web/applywebtemplate(title=@template)?@template="sts#0"              

但是,sharepoint rest 服務不支援通過參數别名傳遞複雜類型。例如,以下 uri(參數别名中包含複雜類型)不受支援:

http://server/site/_api/userprofiles/people(7)/getworkplace(@address)?@address={"__metadata":{"type: "odatademo.address"},"street":"ne 228th", "city":"sammamish","state":"wa","zipcode":"98074","country": "usa"}              

sharepoint rest 服務參數别名文法

[sharepoint]Rest api相關知識(轉)

指定字典作為參數值

對于與以 dictionary<string, string> 字典作為參數的方法相對應的 rest 端點,在查詢字元串中将字典作為一組以逗号分隔的名稱/值對傳遞。

dictionary 參數 rest 服務文法

[sharepoint]Rest api相關知識(轉)

dictionary<string, object> 表示為多值對象,命名為 keyedpropertyvalue,并具有以下字元串屬性:

key 多值對象的鍵

value 對象的值

valuetype 對象的值類型。對于映射到現有實體資料模型 (edm) 類型的簡單值類型,rest 服務傳回相應的 edm 類型字元串;例如,"edm.string"。如果不是,則 rest 服務傳回由 type.tostring 功能傳回的值類型。

在查詢字元串中指定參數值

如果您的 rest uri 以方法調用結束,則可以使用查詢字元串文法來指定方法的參數值。例如:

http://<server>/<site>/_api/web/applywebtemplate?template="sts#0"              

下圖顯示查詢字元串中參數的 rest 服務文法。

查詢字元串中參數的 rest 服務文法

[sharepoint]Rest api相關知識(轉)

指定靜态方法和屬性作為 rest 服務 uri

要構造與靜态方法或屬性對應的 uri,請使用 ecmascript 對象模型中的對應 api 名稱,該名稱以命名空間聲明開始并使用點記法 。例如,ecmascript 用戶端對象模型中的 sp.utilities.utility.getimageurl(imagename) 将具有以下 rest 等效物:

http://server/site/_api/sp.utilities.utility.getimageurl('imagename')            

但是,靜态屬性隻能直接通路,不允許作為較大 uri 組成的一部分。例如,允許直接通路 rest 中的 sp.utility.assetslibrary 方法,如下所示:

http://server/site/_api/sp.utility.assetslibrary/id            

但是,不允許将該資源位置用作更複雜 uri 的參數,如下面的示例所示:

http://server/site/_api/getlist(~sp.utility/assetslibrary/id)            

下圖顯示 sharepoint rest 服務靜态成員文法。

sharepoint rest 服務靜态成員文法

[sharepoint]Rest api相關知識(轉)

rest api對我來說,剛開始是個小白,到現在用的越來越順手,其中msdn對我來說幫了不少的忙,沒空就看msdn,覺得這篇文章講的還是比較清楚的,就收集到自己的部落格中了。

參考文章:

<a href="https://msdn.microsoft.com/zh-cn/library/office/dn292556.aspx" target="_blank">https://msdn.microsoft.com/zh-cn/library/office/dn292556.aspx</a>

<a href="https://msdn.microsoft.com/zh-cn/library/dn551366.aspx" target="_blank">https://msdn.microsoft.com/zh-cn/library/dn551366.aspx</a>

<a href="https://msdn.microsoft.com/zh-cn/library/office/fp142380(v=office.15).aspx" target="_blank">https://msdn.microsoft.com/zh-cn/library/office/fp142380(v=office.15).aspx</a>

<a href="https://msdn.microsoft.com/zh-cn/library/gg309461.aspx" target="_blank">https://msdn.microsoft.com/zh-cn/library/gg309461.aspx</a>

<a href="https://msdn.microsoft.com/en-us/library/jj860569.aspx" target="_blank">https://msdn.microsoft.com/en-us/library/jj860569.aspx</a>

部落格位址:

<a href="http://www.cnblogs.com/wolf-sun">http://www.cnblogs.com/wolf-sun/</a>

部落格版權:

本文以學習、研究和分享為主,歡迎轉載,但必須在文章頁面明顯位置給出原文連接配接。

如果文中有不妥或者錯誤的地方還望高手的你指出,以免誤人子弟。如果覺得本文對你有所幫助不如【推薦】一下!如果你有更好的建議,不如留言一起讨論,共同進步!

再次感謝您耐心的讀完本篇文章。

繼續閱讀