I have the following folder structure.

Folder1 attr1="cat1"
>>  file1 attr2="subcat1"
>>  file2 attr2="subcat2"
Folder2 attr1="cat2"
>>  file3 attr2="subcat1"
>>  file4 attr2="subcat2"
Folder3 attr1="cat1"
>>  file5 attr2="subcat1"
>>  file6 attr2="subcat2"

I want to get all files(only) which is under folder having field attr1 = cat1, i.e., file1, file2, file5, and file6. Note that attr1 is not available for files. A possible solution is to get all folders, then in a loop, query for files using FolderServerRelativeUrl. But I don't think it is optimal because there will be too much delay for the retrieving the final list, as each ExecuteQuery will take some time. I am using Client Object Model for querying. The CAML query I use only retrieves Folders at Present.

string caml = @"<View Scope='RecursiveAll'>
                                            <FieldRef Name='attr1' />
                                            <Value Type='Text'>cat1</Value>
  • Use Scope = Recursive instead of RecusriveAll, Recursive only give you the items inside a folder not the folder – Akarsh Gupta Mar 25 '15 at 9:10
  • I tried both but same result. I believe difference between recursive and recursiveAll is whether to include subfolders in the result. Well the issue here is there is no field attr1 for files(or they are null or not set). So the query will fail for the files. I need to get files for which the folder has attr1 as cat1. – Jose Tuttu Mar 25 '15 at 9:48
  • I can't think of any optimal query based on your current data structure. You should modify the file meta data properties and add parent folder attr1. Else you will end up using the loop. – Amal Hashim Mar 26 '15 at 12:57
  • I would have done that, if I was the site owner. Unfortunately, I have only limited access and SPsite is in production and running fine. So owners are reluctant to change anything. I ended up listing all files and folders. In a nested loop check whether the file's FileDirRefmatches with and 'FileRef' of a folder with attr cat1. Nested looping in c# seems faster compared to looping folders and hitting the site for files. Of course ,it is limited to single level of heirarchy – Jose Tuttu Mar 26 '15 at 13:30
  • The nested looping is only good for small result sets i believe. – Jose Tuttu Mar 26 '15 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.