Get list of files within folder

Sep 17, 2010 at 5:50 AM

Hi,

I sort of think this has been answered by this discussion: http://spservices.codeplex.com/Thread/View.aspx?ThreadId=222154

But I can't figure out how...Basically, I'd like to be able to retrieve a list and for any folders in that list, retrieve its items as well...using "GetListItems"

I've tried:
- Putting the name of a folder in the listName variable
- Putting the name of a folder with the actual list name in the listName variable e.g. listName\Folder1

I'm sure it's simple, I just haven't seen an example or documentation that does this...and the previously mentioned discussion had no code mentioned either

Thanks heaps!

Coordinator
Sep 17, 2010 at 12:32 PM
David: If you check the docs for GetListItems, you'll see the QueryOptions. This is where you set the scope for the request. To see all of the items in the list "flattened" out of their folders, use this: CAMLQueryOptions: "<QueryOptions><ViewAttributes Scope="Recursive"/></QueryOptions>", M.
Coordinator
Sep 19, 2010 at 6:50 PM

You can also specify a folder name if you know the name of the folder. Also, "RecursiveAll" will bring back everything in the library in one call, regardless of what folder they're in.

Sep 20, 2010 at 1:47 AM

I've totally missed that I could use the actual WebService documentation!!! thanks heaps guys...

 

I should know the folder name as I'll be implementing a drill down using ajax so the initial display to the user will be the root of the document library, and clicking on a folder will then browse into the folder and list its items...

Dec 16, 2010 at 7:40 AM

Thanx alot it really helped but what if I want to retreive the items and folders only without being recursive, ive tried using

<QueryOptions><Folder>listname/foldername</Folder</QueryOptions>

 but this one returns all files and folders starting from the rootFolder to the last folders on the list, I only with to reutrn from a specific folder.

Zafer.

 

 

Coordinator
Dec 16, 2010 at 9:29 PM

Zafer:

I don't think I've done this myself, but I just dummied up something in the U2U CAML Builder which shows how it ought to work:

<QueryOptions>
<Folder>Test Document Library/test</Folder>
</QueryOptions>

M.

Dec 16, 2010 at 10:21 PM

Mark:

Thanx again for looking into this, I use U2U CAML Builder like everyday, however its funny that your query works on Document Libraries not the typical List we are all familiar with, and I pretty much want to see it work on lets say Links List.

But thanx a lot I appreciate your help.

Zafer.

Coordinator
Dec 21, 2010 at 4:33 AM

So is your goal to show the items which are in a specific folder in a Custom List, i.e., not a Document Library? Typically people don't use folders on Custom Lists, so I want to understand exactly what you're looking to do. Can you give more details?

M.

Dec 21, 2010 at 11:49 AM

Sorry  I should've replied before; my purpose is to show the kinda of treeview look with collapse expand functionality, I know using folders as a structure is not recommended from what I've heard but it was a requirement.

I've been trying to get to a specific folder and I figured out that <Folder> tag requires full RootFolder like this one  /sites/SiteName/Lists/Links/folder/subFolder and it worked like a charm.

<QueryOptions>
<Folder>/sites/SiteName/Lists/Links/folder/subFolder</Folder>
</QueryOptions>
and of course it also works on Document Libraries :)

and thank you for showing interest.

Zafer.

Coordinator
Dec 21, 2010 at 1:11 PM

Zafer:

Glad that you solved it!

M.

May 2, 2014 at 1:08 PM
Can you post the complete SPServices command that you used to display the list of files in a folder? I am trying to do the same, but cannot seem to retrieve any entries.
I've tried:
                               var folderName = '/xyz/SiteAssets/SitePages/junkanoo/images';
    $().SPServices({
        operation: "GetListItems",
        webURL: "https://xxxxxxxxxxxxxx/",
        CAMLViewFields: "<ViewFields><FieldRef Name='Title'/><FieldRef Name='Category'/></ViewFields>",
        CAMLQuery: "<Query><Where><Contains><FieldRef Name='FileRef' /><Value Type='Text'>" + folderName + "</Value></Contains></Where></Query>", 
        CAMLQueryOptions: "<QueryOptions><Folder>" + folderName + "</Folder></QueryOptions>",
        completefunc: function(xData2, Status) {
            $(xData.responseXML).find("[nodeName='z:row']").each(function(i) {
                var $node = $(this);
                newArr.push([$(this).attr('ows_Title')
                ]);
            });
        }
    });
    alert(newArr.length);
Coordinator
May 2, 2014 at 1:12 PM
Depending on the browser you are using .find("[nodeName='z:row']") may not work. Switch to .SPFilterNode("z:row") instead.

That may fix it. If not, post back.

M.
May 2, 2014 at 1:40 PM
Tried .SPFilterNode("z:row") , still no records get returned.
Coordinator
May 2, 2014 at 2:02 PM
Are you getting any errors? Have you looked at the network traffic?

Ah, you also have xData in one place and xData2 in another.

M.
May 2, 2014 at 6:09 PM

No errors are being generated, just 0 records returned. I changed the xData/xData2 issue and still no results.

Coordinator
May 2, 2014 at 6:31 PM
I'd suggest removing the filters to make sure your overall settings are right. The layer the filtering back in one piece at a time. If you can watch the network traffic, even better.

M.
Feb 4, 2015 at 6:05 AM
Edited Feb 4, 2015 at 6:07 AM
Hi,

I'm tryinig to list document library using this code:
<script>
$(document).ready(function() {
  $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Shared Documents",
    CAMLQueryOptions: "<QueryOptions><Folder></Folder></QueryOptions>",
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {
        var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>";
        $("#tasksUL").append(liHtml);
      });
    }
  });
});
</script>
<ul id="tasksUL"/>
It works, but I want to improve it a bit:
  1. Ussually when you upload document, you can specify "Name", and leave title empty.
    It leads to result, that you will recieve "undefined" value for items with missing Title.
    I tried to change Title in code to Name, but it doesnt work. (It gives "undefined" for everything)
  2. How can I improve output. For instance I have folders structure in Library.
    I want it to looks like that:
<ul>
<li> Test folder 1
    <ul>
        <li> Test folder 1 sub 1
            <ul>
                <li>Test document</li>
                <li>Test document</li>
                <li>Test document</li>
            </ul>
        </li>
        <li>Test folder 1 sub 2</li>
        <li>Test folder 1 sub 3</li>
    </ul>
</li>
<li>Test folder 2</li>
<li>Test document</li>
</ul>
Coordinator
Feb 4, 2015 at 2:29 PM
The StaticName for Name is FileLeafRef.

M.

p.s. Best to start a new thread rather than tacking onto an old one like this.