2014-03-25 35 views
1

我想获得一些文件夹 - 子文件。我有一个列表中的所有文件夹SharePoint ID。我的代码正在工作,但它的性能非常糟糕,因为有很多context.ExecuteQuery;获取带文件夹ID的子文件 - 客户端对象模型

我想使它可能与一个Caml查询。

using (var context = new ClientContext("http://bizimyerimiz/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个文件夹子文件夹中的一个执行

+0

是'cLientcontext'通过的spmetal.exe生成的实体文件? – Marco

+0

不,它是'公共类ClientContext:ClientRuntimeContext'在'Microsoft.SharePoint.Client.dll' – karamba61

+0

我已编辑您的标题。请参阅:“[应该在其标题中包含”标签“](http://meta.stackexchange.com/questions/19190/)”,其中的共识是“不,他们不应该”。 –

回答

2

由于CSOM API支持Request Batching

CSOM编程模型是围绕请求批处理建立的。当您的 与CSOM一起使用时,您可以在对象上执行一系列数据操作。当您调用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); 
} 
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] 
相关问题