简体   繁体   English

使用REST API访问Sharepoint 2010中的文件

[英]Access files in Sharepoint 2010 using REST API

I know very little about Sharepoint. 我对Sharepoint知之甚少。 We have an SP site: http://host/sites/project/subproject/LIBRARY%20Name/Forms/AllItems.aspx 我们有一个SP网站: http://host/sites/project/subproject/LIBRARY%20Name/Forms/AllItems.aspx

Navigating to this in the browser shows me a list of files. 在浏览器中导航至此将显示文件列表。 I want to use SPs REST API to programatically access these files. 我想使用SPs REST API以编程方式访问这些文件。

I've learned that the REST API is accessed through the http://host/_vti_bin/ListData.svc URL. 我了解到,可以通过http://host/_vti_bin/ListData.svc URL访问REST API。 In the browser this returns XML that contains entries for Services, Documents, Images, etc. 在浏览器中,这将返回包含服务,文档,图像等条目的XML。

To access files i tried the following URLs: 要访问文件,我尝试了以下URL:

   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')

, and many other variations. ,以及许多其他变体。

My question is, given the URL of our site, what would the REST API service URL look like? 我的问题是,考虑到我们网站的URL,REST API服务URL会是什么样?

Thanks 谢谢

Apart from SharePoint 2013 and later versions, REST API for SharePoint 2010 supports a relatively restrictive set of resources, in particular File resource is not supported. 除了SharePoint 2013和更高版本,SharePoint 2010的REST API支持一组相对受限的资源,特别是不支持文件资源。

Having said that you could consider the following approach for downloading a file. 话虽如此,您可以考虑以下下载文件的方法。

In order to download a particular file from a library, lets assume list item id is provided in addition to web url and library name . 为了从库中下载特定文件,除了网络URL库名之外,还假设提供了列表项ID

First GET request returns so called document item ( Microsoft.SharePoint.DataService.DocumentsItem type) using the following endpoint: 第一个GET请求使用以下端点返回所谓的文档项( Microsoft.SharePoint.DataService.DocumentsItem类型):

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

Once document item is retrieved, file url could be extracted from Path and Name properties (see below example), and finally downloaded via HTTP GET 检索文档项目后,可以从“ Path和“ Name属性中提取文件url(请参见以下示例),最后通过HTTP GET下载

C# example 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM