I have a requirement to write a Windows application which will walk through a folder structure of a Document Library or OneDrive that it is pointed to and produce an audit of the contents. I'm pretty new to SharePoint development, so I was thinking of using CSOM / CAML to walk through the structure from the root level so as to build output something like this:

Root folder: 10 files, 3 folders File 1 ... File 2 ... File 10...

Folder 1: 0 files, 2 folders Folder 1: Folder 2:

Folder 2: ...

Folder 3: ...

Etc. But I'm having trouble working out how to enumerate / walk through folder structures... This kind of thing is dead simple on a standard disk, but I'm struggling to get my head around how this works in SharePoint. Many thanks for any help :)


Here is one strategy using REST:

Loop/recurse the folder structure:

https://yourDomain/sites/yourSite/_api/Web/GetFolderByServerRelativeUrl('Shared Documents')/Folders 

then repeat for each child folder:

https://yourDomain/sites/yourSite/_api/Web/GetFolderByServerRelativeUrl('Shared Documents/Test Folder')/Folders 

you can get the files at each path using /Files instead of /Folders.

Here's another approach that starts by returning a list of all folders in a library using a CAML query:

/sites/workflowtest/_api/web/lists/getbytitle('Documents')/GetItems(query=@v1)?@v1={'ViewXml':'<View Scope=\'RecursiveAll\'><Query><Where><Eq><FieldRef Name=\'FSObjType\' /><Value Type=\'LookUp\'>1</Value></Eq></Where></Query></View>'}&$select=FileDirRef,fileleafref

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.