Question

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?

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top