返回首页


为了使用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。下面显示的代码片段是
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();



}

}
下载从SharePoint文档列表中的文件
我可以下载文件,使用下面显示的代码片段
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;



}

回答

评论会员:游客 时间:2012/01/26
maloneb:|守则缺少变量和1个不正确的引用
。atverma
评论会员:游客 时间:2012/01/26
我已经更新了文章。感谢您的输入
maloneb
评论会员:游客 时间:2012/01/26
首先,让我说,这篇文章是非常有用的-它让我与客户端对象模型jumpstarted-。然而,我发现与代码的几个问题:在DownloadDocument方法,ClientOM不是一个有效的参考。至于我可以看到,它应该是Microsoft.SharePoint.ClientGetListItemCollectionsFromSP方法,有两个是没有定义的变量:SITEURL和documentListName。后-希望看到这家精致的准备使用代码。也会有所帮助,如果你包括项目文件,例如使用
。uskumar33
评论会员:游客 时间:2012/01/26
页面顶部添加下面的行使用ClientOM=Microsoft.SharePoint.Client;
atverma
评论会员:游客 时间:2012/01/26
感谢整顿。我已取代ClientOM命名空间,但在另一个地方忘了。我已经更新了的文章