2011-04-18 89 views
2

我從Sharepoint庫中檢索文檔列表。假設我的任務是將該列表中的第一個文檔檢索給用戶,以便他可以打開一個docx文件。我該如何去做呢?如何將Sharepoint文檔庫中的文檔返回給用戶?

更進一步的複雜性是Sharepoint服務器位於另一個域中。我正在處理的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]; 

回答

1

該解決方案的執行摘要如下。生成錨標籤時,請包含文檔應具有的值的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 
      }; 
     } 
3

您可以創建基於此查詢結果,以便用戶可以簡單地點擊與完整路徑項目的鏈接「A」元素(這是SharePoint列表大致如何定期渲染)。