[英]How do I return a document from a Sharepoint Document library to the user?
I am retrieving a document list from a Sharepoint library. 我正在从Sharepoint库中检索文档列表。 Let's say my task is to retrieve the very first document in that list to the user so he can open a docx file. 假设我的任务是向用户检索该列表中的第一个文档,以便他可以打开docx文件。 How do I go about doing that? 我该怎么做?
A further complication is that the sharepoint server is located on another domain. 更为复杂的是,共享点服务器位于另一个域上。 The web project that I am working on will surface the documents to the customer, but not expose direct access to the sharepoint server. 我正在处理的Web项目会将文档显示给客户,但不会公开直接访问Sharepoint服务器。
ClientContext clientContext = new ClientContext(URL);
List list = clientContext.Web.Lists.GetByTitle("My Documents");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = XML;
ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(
listItems,
items => items.Include(item => item["FileRef"]));
clientContext.ExecuteQuery();
// return this file to the user
// listItems[0];
您可以基于此查询的结果创建“ A”元素,以便用户只需单击具有该项目完整路径的链接即可(这大约是呈现常规SharePoint列表的方式)。
The executive summary of the solution is as follows. 该解决方案的执行摘要如下。 When producing the anchor tags, include information from the FileRef field which your document should have a value for. 生成锚标记时,请包含来自FileRef字段的信息,您的文档应为此值。 This is a reference field that you will use later. 这是一个参考字段,您将在以后使用。
you will use the reference when you call 致电时将使用参考
FileInformation fileInformation = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, reference);
Stream stream = fileInformation.Stream;
if (stream != null)
{
documentName = Path.GetFileName(reference);
return new FileStreamResult(stream, "unknown")
{
FileDownloadName = documentName
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.