[C#.NET][Sharepoint 2013] 使用 REST API / OData API 準備動作
由下列連結可以看出個端倪,REST API 的服務是目前數量最少的,對前端來講應該就夠用了,假若不夠用的話也可以自己寫。
http://msdn.microsoft.com/en-us/library/office/dn268594%28v=office.15%29.aspx
本文連結:
如何取得檔案 for Advanced REST client
http://msdn.microsoft.com/en-us/library/office/dn292556%28v=office.15%29.aspx
/_api/web 涵蓋了大部份的操作,以下連結是使用手冊,相當的重要
- Lists and list items REST API reference
- Fields REST API reference
- Files and folders REST API reference
- Users, groups, and roles REST API reference
搜尋類
SharePoint Search REST API overview
社群類
Social feed REST API reference for SharePoint 2013
Following people and content REST API reference for SharePoint 2013
OData Api
http://msdn.microsoft.com/en-us/library/office/fp142385%28v=office.15%29.aspx#sectionSection7
Open Data Protocol(OData),是由微軟定義的開放式數據協定,主要用來資料查詢、排序,OData 協定更新的速度很快,真是太嚇人了。
OData query operators supported in the SharePoint REST service 章節描述在 Sharepoint 要如何使用 OData
用法很簡單只要在 URL 加上 ?$ ,便可輕鬆進行查詢、排序
簡單範例:
select
http://sps2013/CsomApi/_api/web/folders?$select=ServerRelativeUrl
orderby
http://sps2013/CsomApi/_api/web/folders?$orderby=ServerRelativeUrl asc
詳細的 HttpRequest 請參考
http://msdn.microsoft.com/en-us/library/office/jj164022%28v=office.15%29.aspx#bk_requestElements
程式開發前,可以利用該工具進行測試,確認 API 語法沒有問題
安裝
調用方式
1.REST endpoint address
2.Header
3.Response Status
4.Return result
如何取得檔案 for Advanced REST client
http://msdn.microsoft.com/en-us/library/office/dn450841%28v=office.15%29.aspx 裡的 Explore the SharePoint 2013 files and folders REST syntax 章節會告訴我們怎麼用 REST API
假設我們要找檔案就從 Files 關鍵字下手,往前面找我們會需要『folder url』,『 getFolderByServerRelativeUrl』,
假設我們要下載檔案就從 $value 關鍵字下手,往前面找
很容易的就可以把完整的 URL 找出來了
1.找出所有的 Folder,http://sps2013/CsomApi/_api/web/folders
它列出可用的方法,其中有一項是 http://sps2013/CsomApi/_api/Web/GetFolderByServerRelativeUrl('/CsomApi/Shared%20Documents')/Files
2.找出所有的檔案
http://sps2013/CsomApi/_api/Web/GetFolderByServerRelativeUrl('/CsomApi/Shared%20Documents')/Files
3.操作特定檔案
4.取得檔案
前面幾篇有提過操作 REST API 的幾個類別,請參考 http://www.dotblogs.com.tw/yc421206/archive/2013/11/11/127593.aspx
這裡將使用 .NET 4.5的 HttpClient
操作物件步驟
1.調用 HttpClientHandler 物件建立身份
2.調用 HttpClient 物件,建立服物請求
3.加入Header ,調用 ttpClient.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose"),為了回傳 json string format
4.調用 HttpClient.GetAsync("Web/GetFolderByServerRelativeUrl('/CsomApi/Shared%20Documents')/Files").Result 取得回應內容
{ HttpClientHandler httpClientHandler = new HttpClientHandler() { Credentials = new NetworkCredential(YourId, YourPassword, YourDomain) }; using (var httpClient = new HttpClient(httpClientHandler)) { httpClient.BaseAddress = new Uri("http://sps2013/csomapi/_api/"); httpClient.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose"); HttpResponseMessage response = httpClient.GetAsync( "Web/GetFolderByServerRelativeUrl('/CsomApi/Shared%20Documents')/Files").Result; if (response.StatusCode == HttpStatusCode.OK) { string result = response.Content.ReadAsStringAsync().Result; Console.WriteLine(result); } Console.ReadKey(); } }
{ HttpClientHandler httpClientHandler = new HttpClientHandler() { Credentials = new NetworkCredential(YourId, YourPassword, YourDomain) }; using (var httpClient = new HttpClient(httpClientHandler)) { httpClient.BaseAddress = new Uri("http://sps2013/csomapi/_api/"); HttpResponseMessage response = httpClient.GetAsync( "Web/GetFolderByServerRelativeUrl('/CsomApi/Shared%20Documents')/Files('myDoc.docx')/$value").Result; if (response.StatusCode == HttpStatusCode.OK) { using (var result = response.Content.ReadAsStreamAsync().Result as MemoryStream) using (FileStream file = new FileStream("download-myDoc.docx", FileMode.Create, FileAccess.Write)) { result.WriteTo(file); } } Console.WriteLine("successful down file,please press any key contiune"); Console.ReadKey(); } }
本文出自:http://www.dotblogs.com.tw/yc421206/archive/2014/06/12/145512.aspx
若有謬誤,煩請告知,新手發帖請多包涵
Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET