Using a CAML Query to Show Files From a Folder

There is a thread on social.technet where someone asks how to “Create a webpart to show subfolder of existing document library.” I had posted a reply to that explaining how to modify a library view in SharePoint Designer to show files from just one folder. I can illustrate the process here with some screenshots so that it is easier to follow.

The first step is done using the browser. You need to create the view for your library so that it shows files inside the folders of your library. That option ignores the folders and shows all of the files, regardless of what folder they are in. When you create the view, select the columns you want to display, how you want them sorted and select the Folders option “show all items without folders”

Show all items without folders

Then in SharePoint Designer (SPD) open the site, on the left is the Navigation and Site Objects pane. Under Site Objects click on Lists and Libraries, select the library. The Views will be displayed on the right, click on the view you’ve created.

Views panel in SharePoint Designer

In the code view, scroll down until you see the XsltListViewWebPart (a library view is a page with one ListView web part). In the XML you’ll see a <Query> tag inside the <View> tag. It will have an <OrderBy> tag (for sorting).

Query tag inside XSLTListViewWebpart

Add a <Where> tag and then use either the <BeginsWith> option, which would return you files from a folder and its sub-folders.

BeginsWith tag inside XSLTListViewWebpart

Or the <Eq> option, which will return files from just one single folder

Eq tag inside XSLTListViewWebpart

Remember to use closing tags as shown.

If you want to check the folder names before adding the <Where> clause you can add the <FieldRef Name="FileDirRef"/> to the <ViewFields> tag.

FieldRef tag inside XSLTListViewWebpart

Then save and preview your view – use it to get the correct value for the folder names.

FieldRef return path values

9 responses to “Using a CAML Query to Show Files From a Folder

  • Thanks for a very helpful tutorial! If I’m using your steps correctly, this method only works if you show all items without folders. Is there a way to make this work while showing all items inside folders?

    • Tom Benjamin
      4 years ago

      Hi Dan, you can still have a default view of the library that shows all the folders. The example here is specifically to show items from inside just one folder.

  • Gareth Reeves
    3 years ago

    Great Article, Is there a way to do this but show subfolders from multiple top level folders rather than one? I have say ten folders at root level all different names each with identical named subfolders, I require a view that shows all the subfolders from the various root folders in one view. As one department only uses the subfolders under each root folder?

    • Tom Benjamin
      3 years ago

      Hi Gareth, you might be able to do that by using the <in> CAML operator instead of <eq>. The syntax would go <in><fieldref name="FileDirRef"><values><value type="Computed">Folder1</value><value type="Computed">Folder2</value></values></fieldref></in>

  • Rhett Martens
    3 years ago

    I am attempting to use this solution to view files and folders within a large library of documents. The best example I could give is If I have a top level folder “Cats” and I want a view looking inside this folder, but inside that folder I have other folders, I have “2018 Pics”, “2017 Pics”, and “2016 Pics” as well as some free floating documents like myCatChart.xls. Using this example, how can I set up a view to look inside the “Cats” folder and see both my subfolders and documents within that top level folder?

    • Tom Benjamin
      3 years ago

      Hi Rhett, you should be able to do that using the <BeginsWith> tag inside the <Where> tag. Using your example of “Cats” (always a good choice), you would have <Where><BeginsWith><FieldRef Name="FileDirRef"/><Value Type="Computed">path to cats folder</Value></BeginsWith></Where>. To get that path you may need to follow the example in the last screenshot of this post, which you would do before adding the <Where> tag.

  • Rhett Martens
    3 years ago

    I’m feeling a bit stupid, but I am not getting anything. I have combed over this and feel confident enough that I am entering it in exactly as directed, but something is hanging up. Basically, I’m trying to create views like “2018 Cats”. I want the view to show the 2018 Cats top folder’s contents by default in this view. Inside 2018 Cat’s folder, I might have a folder called lounging cats, excited cats, and some loose pictures. Using the solution above, why am I unable to get a view that shows the inside folders and docs of a top level folder?


    “View 1 = 2018 Cats”

    2018 Cats
    “2018 Cats views this” (
    Lounging Cats (folder)
    Excited Cats (Folder)
    Random Cat 1.jpg
    Random Cat 2.jpg );
    2017 Cats
    2016 Cats

    Is this possible, and if so, what am I doing wrong? Also, your blog is amazing, very helpful, I really appreciate it.

    • Tom Benjamin
      3 years ago

      Hi Rhett,
      Are you able to show me the code of the CAML query you’re using? Or is the site it’s on publicly accessible? I will email you directly.

Trackbacks & Pings

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.