1

In my project, I need to get the item from SharePoint library which contain many folder level.

However, I don't know how to write the query to get the file from particular folder and inside the sub folder.

For now, I can get the file in one folder level. My code now:

 query.Folder = web.GetFolder(folderLevelTwoId);

 query.ViewXml = @"<View Scope=\'RecursiveAll\'><Query><Where>
                                        <Or>
                                            <Contains>
                                                <FieldRef Name='FileLeafRef' />
                                                <Value Type='Text'>.jpg</Value>
                                            </Contains>
                                            <Contains>
                                                <FieldRef Name='FileLeafRef' />
                                                <Value Type='Text'>.png</Value>
                                            </Contains>
                                        </Or>
                                    </Where></Query></View>";
 items = docLib.GetItems(query);

My Folder level:

Library         
-item1            <=== if i remove query.Folder = web.GetFolder(folderLevelTwoId);
-item2            <=== i can only get these three items
-item3            <=== the items inside folder will not pass to me
  folder 1
  -item1
  -item2
  -item3 
      folder 2    <--- the GUID i used in code above
      -item1
      -item2
      folder 3
      ...
      folder 4
          folder 5
            folder 6

By using the above code, I can get the SPListItemCollection for folder 2.

How can I get SPListItemCollection in folder 3,4,5,6 by using the folder 2?

1

Use following code

    SPQuery spQuery = new SPQuery();  
    spQuery.Folder = web.GetFolder(folderLevelTwoId);  
    spQuery.ViewAttributes = "Scope=\"Recursive\"";
    spQuery.Query="<Where><Or>
                          <Contains>
                               <FieldRef Name='FileLeafRef' />
                                <Value Type='Text'>.jpg</Value>
                            </Contains>
                             <Contains>
                                <FieldRef Name='FileLeafRef' />
                                 <Value Type='Text'>.png</Value>
                             </Contains>
                            </Or>
                     </Where>";
     SPListItemCollection items = docLib.GetItems(spQuery);
| improve this answer | |
  • If that, i cannot get any SPListItem in folder of subFolder, please check my update – king yau Sep 3 '16 at 8:42
  • Check my updated code. remove following code. query.Folder = web.GetFolder(folderLevelTwoId); query.ViewXml = @"*******************"; – Akshay Belure Sep 3 '16 at 8:51
  • So, is it possible to get the SPListItem in folder 2 and its sub folder only? i don't want to receive the item on above level (folder 2 is the sub folder of folder 1) – king yau Sep 3 '16 at 8:55
  • then insert, query.Folder = web.GetFolder(folderLevelTwoId); – Akshay Belure Sep 3 '16 at 8:56
  • -_- How about folder 3, 4 ,5 ,....? :o) – king yau Sep 3 '16 at 9:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.