为了使用ClientContext,我们需要添加两个dll引用到我们的项目中。Microsoft.SharePoint.Client.dll和Microsoft.SharePoint.Client.Runtime.dll。在本博文中,我们将学习如何:
从SharePoint文档库中通过CAML获取ListItemCollection上载一个文档到SharePoint 文档库从SharePoint文档库下载一个文档我们可以像下面这样获取ListItemCollection:
ListItemCollection listItems = GetListItemCollectionFromSP("FileLeafRef", documentName, "Text", 1);GetListItemCollectionFormSP方法可以用来获取列表项,其中第一个参数Name - 为FieldRef的名称,第二个参数value=FieldRef的值,第三个参数type - 是值的类型,最后一个参数rowLimit - 是返回最多多少条记录。
private static ListItemCollection GetListItemCollectionFromSP(string name, string value, string type, int rowLimit) { 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; }本例中我们需要上载文档到SharePoint文档库,同时更新该文档的元数据信息。比如通过ClientContext将一个说明字段设置为“核心内容”。代码如下:
public static void UploadDocument(string siteURL, string documentListName,string documentListURL, string documentName, byte[] documentStream) { using (ClientContext clientContext = new ClientContext(siteURL)) { //获取文档库 List documentsList = clientContext.Web.Lists.GetByTitle(documentListName); var fileCreationInformation = new FileCreationInformation(); //指定内容 byte[]数组,这里是 documentStream fileCreationInformation.Content = documentStream; //允许文档覆盖 fileCreationInformation.Overwrite = true; //上载 URL地址 fileCreationInformation.Url = siteURL + documentListURL + documentName; Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation); //更新元数据信息,这里是一个显示名为“描述”的字段,其字段名为“Description0” uploadFile.ListItemAllFields["Description0"] = "核心内容"; uploadFile.ListItemAllFields.Update(); clientContext.ExecuteQuery(); } }我们可以用如下代码下载一个文档
public static 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) { //这个方法上面讨论过了,用于从SharePoint文档库获取列表项集合 ListItemCollection listItems = GetListItemCollectionFromSP("FileLeafRef", documentName, "Text", 1); return (listItems != null && listItems.Count == 1) ? listItems[0] : null; }上载到SharePoint中的效果如下:
希望对你有帮助!
参考资料
How to upload/download a document in SharePoint 2010 using Client Context Object Model
Microsoft.SharePoint.Client.ListItem Class
Microsoft.SharePoint.Client.File Class
Programmatically Upload document using Client object model – SharePoint 2010
转载于:https://www.cnblogs.com/Sunmoonfire/archive/2010/08/24/1806938.html
相关资源:数据结构—成绩单生成器