开发者

How do I return a document from a Sharepoint Document library to the user?

I am retrieving a docum开发者_StackOverflow社区ent 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];


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜