0
嗨我想在MVC視圖中顯示文檔列表並能夠單擊下載它們。我自己嘗試了一個解決方案,如下所示。我一直在努力,很長一段時間,所以任何建議將不勝感激。使用MVC從SharePoint 2010文檔庫下載文檔
namespace SharePointMVC.Controllers
{
[HandleError]
public class HomeController : Controller
{
DefaultModel Lists = new DefaultModel();
string siteURL = "http://MYSPSSITE";
string documentListName = "MY DOCUMENT LIBRARY";
ListItemCollection listItems = null;
using (ClientContext clientContext = new ClientContext(siteURL))
{
List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);
CamlQuery camlQuery = new CamlQuery(); ;
camlQuery.ViewXml =
@"<View>
<Query>
<Where>
<Eq>
<FieldRef Name ='" + name + @"'/>
<Value Type ='" + type + "'>" + value + @"</Value>
</Eq>
</Where>
<RowLimit>" + rowLimit.ToString() + @"</RowLimit>
</Query>
</View>";
listItems = documentsList.GetItems(camlQuery);
clientContext.Load(documentsList);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
}
return listItems;
}
private static ListItem GetDocumentFromSP(String documentName)
{
ListItemCollection listItems = GetListItemCollectionFromSP("FileLeafRef", documentName, "Text", 1);
return (listItems != null && listItems.Count == 1) ? listItems[0] : null;
}
public Stream DownloadDocument(string SiteURL, string documentName)
{
ListItem item = GetDocumentFromSP(documentName);
if (item != null)
{
using (ClientContext clientContext = new ClientContext(SiteUrl))
{
FileInformation fileInformation = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, item["Outline.docx"].ToString());
return fileInformation.Stream;
}
}
return null;
}
您是否已經檢查過此網站? -http://social.msdn.microsoft.com/Forums/eu/sharepointdevelopmentlegacy/thread/88a07e77-61ef-42d8-802e-0079c27b3bf7 –
是的,我已經通過該網站,雖然我沒有發現它是非常清楚的。不管怎麼說,還是要謝謝你! –