2

I want to get some folders - sub files. I have all folders SharePoint ID in a list. My code is working but it's performance very bad because there is a lot of context.ExecuteQuery;

I want to make it maybe with a Caml Query.

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);

        }
    }
}

For example I have 100 folder but I want to get 10 folders sub folder in one Execution

Burak Kalafat
  • 68
  • 2
  • 15
  • Is `cLientcontext` an entity file generated by SPMetal.exe? – Marco Mar 25 '14 at 09:08
  • no it is a class like 'public class ClientContext : ClientRuntimeContext' at 'Microsoft.SharePoint.Client.dll' – Burak Kalafat Mar 25 '14 at 09:15
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Mar 28 '14 at 12:44

2 Answers2

4

Since CSOM API supports Request Batching:

The CSOM programming model is built around request batching. When you work with the CSOM, you can perform a series of data operations on the ClientContext object. These operations are submitted to the server in a single request when you call the ClientContext.BeginExecuteQuery method.

you could refactor your code as demonstrated below:

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);
}
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
0

Use Caml Query:

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]
Tony Wu
  • 1,040
  • 18
  • 28