How to list the file assets in a folder

106 views
Skip to first unread message

Luke Robinson

unread,
Feb 9, 2017, 10:16:12 AM2/9/17
to dotCMS User Group


I was seeking high-and-low for how to list the file assets in a folder. This velocity script  – written for dotCMS 3.2.4 – is the results of that search and my first plug-in (I'm sure there's room for improvement).
Special thanks to Brad Rice-3, Mark Pitely, and Chris Falzone for their contributions in this forum relating to this topic. I hope others will benefit from this code share.


Demo: Link and screen grab

https://calvin.edu/offices-services/human-resources/documents/



Velocity file: document-listing.vtl

The bare-bones code for finding and listing file assets found within a folder:

 ## Get the page's URI and make its URL relative to the parent folder.
 
#set($pageUrl = ${VTLSERVLET_URI})

 
## Remove index.html from URL and replace it with a backslash.
 
#if ($pageUrl.contains('index.html'))
     
#set ($relativeUrl = $pageUrl.replaceAll('/index.html','/'))
 
#end
   
 
## Loop across the entire directory.
 
#foreach($file in $dotcontent.pull("+structureName:FileAsset +path:$relativeUrl*",100,"title asc"))
     
<li><a href="/dotAsset/${file.identifier}.${file.fileAsset.extension}" title="Download: $file.fileName">
     
<img src="/icon?i=${file.fileAsset.extension}" alt="${file.fileAsset.extension} file" style="width: 16px; height: 16px;" />$file.title, $math.add($math.ceil($math.div(${file.metaData.fileSize}, 1024)), 1) KB</a></li>
 
#end


Below is a screen grab of the finished production code:



File structure: /documents/index.html

Reference the document-listing.vtl plug-in from a directory's index.html file. This will be the same directory that contains your documents (i.e. file assets such as .pdf, .doc, .xlsx).

 ## List the file assets in the directory and its subdirectories
 
#dotParse("/application/velocity/document-listing.vtl")




Keywords

fileAssets, $dotContent, pull, folder, directory, files, metadata, file URL, pdf, doc, jpg, docx, list, icons, friendly URL

Reply all
Reply to author
Forward
0 new messages