sharepoint 2010客户端对象模型如何获取指定文档库里面的文件和文件夹
ListItemCollection listItems = null;
using (ClientContext clientContext = new ClientContext(siteURL))
{
clientContext.Credentials = new NetworkCredential("admin", "!@#", "ad");
List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml =
@"<View>
<Query>
<RowLimit>" + rowLimit.ToString() + @"</RowLimit>
</Query>
</View>";
listItems = documentsList.GetItems(camlQuery);
clientContext.Load(documentsList);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
}
return listItems;
}
为什么只能取到第一级的取不到下面的,请高手帮我看一下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
namespace SComTest
{
class Program
{
static void Main(string[] args)
{
try
{
string siteUrl = "http://www.k2hongkong.com/WOMS";
//string siteUrl = "http://inet.hysan.com.hk/site/woms/";
using (ClientContext myContext = new ClientContext(siteUrl))
{
//myContext.Load(myContext.Site);
//myContext.ExecuteQuery();
//Console.WriteLine(myContext.Site.Url);
List list = myContext.Web.Lists.GetByTitle("QE Attachments");
myContext.Load(list);
myContext.ExecuteQuery();
//Console.WriteLine(list.ItemCount);
//vary query = from listItems in list.
int Number = 2013300047;
for (int i = 0; i < 20; i++)
{
//string folderName = string.Format(@"/site/WOMS/QE Attachments/{0}", Number + i);
string folderName = string.Format(@"/WOMS/QE Attachments/{0}", Number + i);
Console.WriteLine("SubFolder {0}", folderName);
DateTime startTime = DateTime.Now;
GetFolderItems(folderName, list, myContext);
DateTime endTime = DateTime.Now;
Console.WriteLine("Duration: {0}", endTime.Subtract(startTime).TotalMilliseconds);
}
}
}
catch (System.Exception ex)
{
Console.WriteLine("{0}", ex.ToString());
}
}
static public void GetFolderItems(string folderServerRelativeUrl, List list, ClientContext clientContext)
{
try
{
var query = new CamlQuery();
query.FolderServerRelativeUrl = folderServerRelativeUrl;
query.ViewXml = "<View Scope=\"RecursiveAll\"> " +
//query.ViewXml = "<View Scope=\"FilesOnly\"> " +
"<Query>" +
"<Where>" +
"<Eq>" +
"<FieldRef Name=\"FileDirRef\" />" +
"<Value Type=\"Text\">" + folderServerRelativeUrl + "</Value>" +
"</Eq>" +
"</Where>" +
"</Query>" +
"</View>";
var folderItems = list.GetItems(query);
clientContext.Load(folderItems);
clientContext.ExecuteQuery();
foreach (ListItem item in folderItems)
{
// item[ "..." ];
Console.WriteLine("{0}", (string)item["FileRef"]);
}
}
catch (Exception ex)
{
Console.WriteLine("{0}", ex);
}
}
}
}
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.SharePoint.Client; namespace SComTest { class Program { static void Main(string[] args) { try { string siteUrl = "http://www.k2hongkong.com/WOMS"; //string siteUrl = "http://inet.hysan.com.hk/site/woms/"; using (ClientContext myContext = new ClientContext(siteUrl)) { //myContext.Load(myContext.Site); //myContext.ExecuteQuery(); //Console.WriteLine(myContext.Site.Url); List list = myContext.Web.Lists.GetByTitle("QE Attachments"); myContext.Load(list); myContext.ExecuteQuery(); //Console.WriteLine(list.ItemCount); //vary query = from listItems in list. int Number = 2013300047; for (int i = 0; i < 20; i++) { //string folderName = string.Format(@"/site/WOMS/QE Attachments/{0}", Number + i); string folderName = string.Format(@"/WOMS/QE Attachments/{0}", Number + i); Console.WriteLine("SubFolder {0}", folderName); DateTime startTime = DateTime.Now; GetFolderItems(folderName, list, myContext); DateTime endTime = DateTime.Now; Console.WriteLine("Duration: {0}", endTime.Subtract(startTime).TotalMilliseconds); } } } catch (System.Exception ex) { Console.WriteLine("{0}", ex.ToString()); } } static public void GetFolderItems(string folderServerRelativeUrl, List list, ClientContext clientContext) { try { var query = new CamlQuery(); query.FolderServerRelativeUrl = folderServerRelativeUrl; query.ViewXml = "<View Scope=\"RecursiveAll\"> " + //query.ViewXml = "<View Scope=\"FilesOnly\"> " + "<Query>" + "<Where>" + "<Eq>" + "<FieldRef Name=\"FileDirRef\" />" + "<Value Type=\"Text\">" + folderServerRelativeUrl + "</Value>" + "</Eq>" + "</Where>" + "</Query>" + "</View>"; var folderItems = list.GetItems(query); clientContext.Load(folderItems); clientContext.ExecuteQuery(); foreach (ListItem item in folderItems) { // item[ "..." ]; Console.WriteLine("{0}", (string)item["FileRef"]); } } catch (Exception ex) { Console.WriteLine("{0}", ex); } } } }
递归遍历子项
具体怎么做呢
请说下方法,我是新手
你这查询本来就只查询的是第一级的。需要递归
请说下方法,我是新手
CAML
可以给SPQuery对像设置一个属性
query.ViewAttributes = "Scope=\"FilesOnly\"";
query.ViewAttributes = "Scope=\"Recursive\"";
query.ViewAttributes = "Scope=\"RecursiveAll\"";
或者直接写到CAML语句里面