获取包含文件夹的子文件' ID - 客户端对象模型

时间:2014-03-25 09:05:11

标签: c# sharepoint sharepoint-2010 caml client-object-model

我想获得一些文件夹 - 子文件。我在列表中包含所有文件夹SharePoint ID。我的代码正在运行,但它的性能非常差,因为有很多context.ExecuteQuery;

我想通过Caml查询来实现它。

using (var context = new ClientContext("http://xxx/haberlesme/"))
{
    var web = context.Web;
    var list = context.Web.Lists.GetById(new Guid(target));

    int[] siraliIdArray;
    //siraliIdArray = loadSharePointIDList(); think like this

    for (var j = 0; j < siraliIdArray.Count; j++)
    {
        var folderName = listItemCollection[j]["Title"].ToString();//Folder Name
        var currentFolder = web.GetFolderByServerRelativeUrl("/haberlesme/Notice/" + folderName);
        var currentFiles = currentFolder.Files;

        context.Load(currentFiles);

        //I don't want to execute for n folder n times. I want to execute n folder 1 time.
        context.ExecuteQuery();

        var ek = new LDTOTebligEk();

        //I don't want to add one - one 
        foreach (var file1 in currentFiles)
        {
            ek.DokumanPath = urlPrefix + folderName + "/" + file1.Name;
            ek.DokumanAd = file1.Name;

            ekler.Add(ek);

        }
    }
}

例如我有100个文件夹,但我想在一个执行

中获得10个文件夹子文件夹

2 个答案:

答案 0 :(得分:3)

由于CSOM API支持Request Batching

  

CSOM编程模型是围绕请求批处理构建的。当你   使用CSOM,您可以执行一系列数据操作   ClientContext对象。这些操作将提交给服务器   致电ClientContext.BeginExecuteQuery时的一个请求   方法

您可以重构您的代码,如下所示:

var folders = new Dictionary<string,Microsoft.SharePoint.Client.Folder>();
var folderNames = new[] {"Orders","Requests"};
foreach (var folderName in  folderNames)
{
   var folderKey = string.Format("/Shared Documents/{0}", folderName);
   folders[folderKey] = context.Web.GetFolderByServerRelativeUrl(folderKey);
   context.Load(folders[folderKey],f => f.Files);
}
context.ExecuteQuery();  //execute request only once




//print all files
var allFiles = folders.SelectMany(folder => folder.Value.Files);
foreach (var file in allFiles)
{
   Console.WriteLine(file.Name);
}

答案 1 :(得分:0)

使用Caml查询:

Microsoft.SharePoint.Client.List list = clientContext.Web.Lists.GetByTitle("Main Folder");

Microsoft.SharePoint.Client.CamlQuery caml = new Microsoft.SharePoint.Client.CamlQuery();

caml.ViewXml = @"<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Folder'>SubFolderName</Value></Eq></Where></Query></View>";

caml.FolderServerRelativeUrl = " This line should be added if the main folder is not in the site layer";

Microsoft.SharePoint.Client.ListItemCollection items = list.GetItems(caml);

clientContext.Load(items);

//Get your folder using items[0]