如何将文档从 Sharepoint 文档库返回给用户?

发布于 2024-11-02 07:29:04 字数 633 浏览 1 评论 0原文

我正在从 Sharepoint 库检索文档列表。假设我的任务是向用户检索该列表中的第一个文档,以便他可以打开 docx 文件。我该如何去做呢?

更复杂的是共享点服务器位于另一个域。我正在开发的 Web 项目将向客户展示文档,但不会公开对共享点服务器的直接访问。

    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];

I am retrieving a document list from a Sharepoint library. 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. 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.

    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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪失控 2024-11-09 07:29:04

您可以根据此查询的结果创建“A”元素,以便用户只需单击包含项目完整路径的链接(这大约是常规 SharePoint 列表的呈现方式)。

You can create "A" element based on result of this query so user can simply click on the link with full path to the item (this is approximately how regular SharePoint lists are rendered).

神妖 2024-11-09 07:29:04

该解决方案的执行摘要如下。生成锚标记时,请包含来自 FileRef 字段的信息,您的文档应为其提供值。这是您稍后将使用的参考字段。

您致电时将使用该参考

        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
            };
        }

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. 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
            };
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文