2016-11-09 42 views
1

我对Sharepoint知之甚少。我们有一个SP站点: http://host/sites/project/subproject/LIBRARY%20Name/Forms/AllItems.aspx使用REST API在Sharepoint 2010中访问文件

在浏览器中导航到此显示一个文件列表。 我想使用SPs REST API以编程方式访问这些文件。

我了解到,REST API通过http://host/_vti_bin/ListData.svc URL访问。在浏览器这个返回的XML包含了服务,文档,图像条目等

访问文件我尝试以下网址:

http://host/_vti_bin/ListData.svc/Documents 
    http://host/_vti_bin/ListData.svc/Documents('LIBRARY%20Name') 
    http://host/_vti_bin/ListData.svc/Documents?$select=NAME('LIBRARY%20Name') 

,和许多其他变化。

我的问题是,考虑到我们的网站的URL,将在REST API服务网址是什么样子?

感谢

回答

1

除了从SharePoint 2013及更高版本中,为SharePoint 2010 REST API支持相对限制的资源集合,特别是文件资源不被支持。

说了这么多,你可以考虑下载文件下列方法。

为了从库中下载特定文件,让我们假设 列表项ID是除了提供给网页URL库名

首先GET请求返回所谓使用以下端点文档项(Microsoft.SharePoint.DataService.DocumentsItem型):

https://<weburl>/_vti_bin/listdata.svc/<listname>(<itemid>) 

一旦文档项目被检索,文件URL可以从PathName性质萃取(见下面的例子),最后通过HTTP GET下载

C#示例

var webUrl = "https://intranet.contoso.com/"; 
var listName = "Documents"; //<-list name goes here 
var itemId = 1; //<-list item id goes here 
using (var client = new WebClient()) 
{ 
    client.BaseAddress = webUrl; 
    client.Credentials = credentials; 
    client.Headers.Add(HttpRequestHeader.Accept, "application/json;odata=verbose"); 
    //client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f"); 
    var url = String.Format("_vti_bin/listdata.svc/{0}({1})",listName,itemId); 
    var content = client.DownloadString(url); 
    var json = JObject.Parse(content); 
    //extract file url 
    var fileUrl = (string)json["d"]["Path"] + "/" + (string)json["d"]["Name"]; 
    Console.WriteLine(fileUrl); 
    //download a file 
    var fileName = Path.GetFileName(fileUrl); 
    client.DownloadFile(fileUrl,fileName); 
}