嗨,我想在 MVC 视图中显示文档列表,并能够在单击时下载它们。我自己尝试了一个解决方案,如下所示。我已经为此苦苦挣扎了很长时间,因此任何建议都将不胜感激。
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;
}