为了使用ClientContext,我需要添加两个DLL是我的项目即Microsoft.SharePoint.Client.dll和Microsoft.SharePoint.Client.Runtime.dll参考。
在这个博客中,我将展示如何从SharePoint文档列表中使用CAML的ListItemCollection上传文件到SharePoint文档列表从SharePoint文档列表下载文件从SharePoint文档列表中使用CAML的ListItemCollection
我可以得到ListItemCollection在代码片段低于显示ListItemCollection listItems = GetListItemCollectionFromSP("FileLeafRef",
documentName, "Text", 1);
GetListItemCollectionFromSP返回列表项和方法的参数
名字吗??FieldRef,值的名称=值,FieldRef,类型匹配?的价值和rowLimit???,以获取最大行数{C}上传文件到SharePoint文件清单:
在这种情况下我想上传文件到SharePoint文档列表,并更新场场元数据,即"DOCTYPE??"我的最爱??在这个例子中使用ClientContext。下面显示的代码片段是
下载从SharePoint文档列表中的文件public void UploadDocument(string siteURL, string documentListName,
string documentListURL, string documentName,
byte[] documentStream)
{
using (ClientContext clientContext = new ClientContext(siteURL))
{
//Get Document List
List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);
var fileCreationInformation = new FileCreationInformation();
//Assign to content byte[] i.e. documentStream
fileCreationInformation.Content = documentStream;
//Allow owerwrite of document
fileCreationInformation.Overwrite = true;
//Upload URL
fileCreationInformation.Url = siteURL + documentListURL + documentName;
Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(
fileCreationInformation);
//Update the metadata for a field having name "DocType"
uploadFile.ListItemAllFields["DocType"] = "Favourites";
uploadFile.ListItemAllFields.Update();
clientContext.ExecuteQuery();
}
}
我可以下载文件,使用下面显示的代码片段public Stream DownloadDocument(string siteURL, string documentName)
{
ListItem item = GetDocumentFromSP(documentName);
if (item != null)
{
using (ClientContext clientContext = new ClientContext(siteURL))
{
FileInformation fInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext,
item ["FileRef"].ToString());
return fInfo.Stream;
}
}
return null;
}
private static ListItem GetDocumentFromSP(string documentName)
{
//This method is discussed above i.e. Get List Item Collection from SharePoint
//Document List
ListItemCollection listItems = GetListItemCollectionFromSP("FileLeafRef",
documentName, "Text", 1);
return (listItems != null && listItems.Count == 1) ? listItems[0] : null;
}