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 –
是的,我已经通过该网站,虽然我没有发现它是非常清楚的。不管怎么说,还是要谢谢你! –