首页 新闻 搜索 专区 学院

sharepoint 2010客户端对象模型如何获取指定文档库里面的文件和文件夹

0
悬赏园豆:100 [已解决问题] 解决于 2013-03-22 16:39

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;

}

为什么只能取到第一级的取不到下面的,请高手帮我看一下

leon1990的主页 leon1990 | 初学一级 | 园豆:114
提问于:2013-02-20 23:22
< >
分享
最佳答案
0

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);
            }
        }
    }
}
leon1990 | 初学一级 |园豆:114 | 2013-03-04 16:09
其他回答(3)
0

递归遍历子项

收获园豆:20
corsica | 园豆:224 (菜鸟二级) | 2013-02-21 10:29

具体怎么做呢

支持(0) 反对(0) leon1990 | 园豆:114 (初学一级) | 2013-02-21 14:33

请说下方法,我是新手

支持(0) 反对(0) leon1990 | 园豆:114 (初学一级) | 2013-02-21 14:43
0

你这查询本来就只查询的是第一级的。需要递归

收获园豆:20
hardy_Wang | 园豆:53 (初学一级) | 2013-02-21 13:37

请说下方法,我是新手

支持(0) 反对(0) leon1990 | 园豆:114 (初学一级) | 2013-02-21 15:02
0

CAML

可以给SPQuery对像设置一个属性

query.ViewAttributes = "Scope=\"FilesOnly\"";

query.ViewAttributes = "Scope=\"Recursive\"";

query.ViewAttributes = "Scope=\"RecursiveAll\"";

或者直接写到CAML语句里面

收获园豆:60
T-John | 园豆:273 (菜鸟二级) | 2013-02-22 11:28
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册