从文档库中的所有子文件夹中检索所有文档 - CSOM

时间:2013-05-08 18:59:52

标签: sharepoint sharepoint-2010

我正在使用客户端对象模型方法C#,以便从包含子文件夹的文档库中检索所有列表项。我检查了MSDN文档,我被困在为什么我无法获得字段属性,或者我甚至做到了这一点。

    NetworkCredential credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            ClientContext clientcontext = new ClientContext(Resources.defaultSPSite);
            clientcontext.Credentials = credentials;

            //Load Libraries from SharePoint
            //Web site = clientcontext.Web;
            clientcontext.Load(clientcontext.Web.Lists);
            clientcontext.ExecuteQuery();


            //List sharedDocumentsList = clientcontext.Web.Lists.GetByTitle("TestLDOCS");
            //CamlQuery camlQuery = new CamlQuery();
            //camlQuery.ViewXml = @"<View Scope='Recursive'><Query></Query></View>";

            foreach (List list in clientcontext.Web.Lists)
            {
                clientcontext.Load(list);
                clientcontext.ExecuteQuery();
                //list.TemplateFeatureId.ToString().Equals("") &&
                    string baseType =  list.BaseType.ToString();
                    string listTitle = list.Title.ToString();
                    if (list.BaseType.ToString().Equals("DocumentLibrary", StringComparison.InvariantCultureIgnoreCase) && list.Title.ToString().Equals("TestLDOCS", StringComparison.InvariantCultureIgnoreCase))
                    {
                        foreach (Folder subFolder in list.RootFolder.Folders)
                        {
                            foreach (File f in subFolder.Files)
                            {
                                Console.WriteLine((string) f.Title);                     
                            }
                        }
                    }
             }
        }

我收到的错误是“foreach(subFolder.Files中的文件f)”集合可能未初始化错误。无论如何使用CSOM获取文档库中每个子文件夹中所有文档的字段值?

我知道您可以使用列表项(listItem [“fieldName”])强烈键入字段值。我应该走这条路吗?

2 个答案:

答案 0 :(得分:10)

一些建议:

1)首选ClientRuntimeContext.LoadQuery method加载特定列表,例如:

var lists = ctx.LoadQuery(ctx.Web.Lists.Where(l => l.BaseType == BaseType.DocumentLibrary));
ctx.ExecuteQuery();

2)由于SharePoint SCOM支持Request Batching,因此建议最小化对服务器的请求数。以下示例演示了如何对服务器执行单个请求,以便从文档库加载所有文件:

foreach (var list in lists)
{
    var items = list.GetItems(CreateAllFilesQuery());
    ctx.Load(items, icol => icol.Include(i => i.File));
    results[list.Title] = items.Select( i=>i.File);
}
ctx.ExecuteQuery();

3)更喜欢通过CAML查询加载所有文件,如下所示:

public static CamlQuery CreateAllFilesQuery()
{
   var qry = new CamlQuery();
   qry.ViewXml ="<View Scope=\"RecursiveAll\"><Query><Where><Eq><FieldRef Name=\"FSObjType\" /><Value Type=\"Integer\">0</Value></Eq></Where></Query></View>";
   return qry;
}

然后以下示例将返回库中的所有文件:

var items = list.GetItems(CreateAllFilesQuery());
ctx.Load(items, icol => icol.Include(i => i.File));
ctx.ExecuteQuery();
var files = items.Select( i=>i.File).ToList();

从性能角度加载特定列表是更优化的方式

完整示例

如何使用SharePoint CSOM从文档库加载所有文件:

using (var ctx = new ClientContext(webUri))
{

     var results = new Dictionary<string, IEnumerable<File>>();
     var lists = ctx.LoadQuery(ctx.Web.Lists.Where(l => l.BaseType == BaseType.DocumentLibrary));
     ctx.ExecuteQuery();
     foreach (var list in lists)
     {
         var items = list.GetItems(CreateAllFilesQuery());
         ctx.Load(items, icol => icol.Include(i => i.File));
         results[list.Title] = items.Select( i=>i.File);
     }
     ctx.ExecuteQuery();

     //Print results
     foreach (var result in results)
     {
         Console.WriteLine("List: {0}",result.Key);
         foreach (var file in result.Value)
         {
             Console.WriteLine("File: {0}", file.Name);
         }
      }        
}

答案 1 :(得分:1)

foreach (List list in clientcontext.Web.Lists)
        {
            clientcontext.Load(list);
            clientcontext.ExecuteQuery();
            //list.TemplateFeatureId.ToString().Equals("") &&
                string baseType =  list.BaseType.ToString();
                string listTitle = list.Title.ToString();
                if (list.BaseType.ToString().Equals("DocumentLibrary", StringComparison.InvariantCultureIgnoreCase) && list.Title.ToString().Equals("TestLDOCS", StringComparison.InvariantCultureIgnoreCase))
                {
                    foreach (Folder subFolder in list.RootFolder.Folders)
                    {
           clientcontext.Load(subFolder.Files);
                   clientcontext.ExecuteQuery();
                        foreach (File f in subFolder.Files)
                        {
                            Console.WriteLine((string) f.Title);                     
                        }
                    }
                }
         }
    }