[opencms-dev] Get folder list ordered by title

Paul-Inge Flakstad flakstad at npolar.no
Tue Jan 15 16:21:46 CET 2013


Hi,

I think there are several ways to go about it. I'm describing the method that comes to mind.

Start with the CmsObject#readResources method: 
http://files.opencms.org/javadoc/core/org/opencms/file/CmsObject.html#readResources(java.lang.String, org.opencms.file.CmsResourceFilter, boolean)

The filter is important here. Also, be prepared to do the sorting manually using a comparator (see code below).

The code should read something like this (not tested):

CmsAgent cms = new CmsJspActionElement(pageContext, request, response);
CmsObject cmso = cms.getCmsObject();

final CmsObject CMSO = OpenCms.initCmsObject(cmso); // A CmsObject is needed
boolean readTree     = true; // or false
String rootFolder    = "/my/folder/"; // The folder to list subfolders of
CmsResourceFilter rf = CmsResourceFilter.DEFAULT_FOLDERS; // A filter to match folder resources
// Get a list of all subfolders
List<CmsResource> subfolders = cmso.readResources(rootFolder, rf, readTree);

if (!subfolders.isEmpty()) {
// Comparator
final Comparator<CmsResource> TITLE_ORDER = new Comparator<CmsResource>() {
                                                                public int compare(CmsResource one, CmsResource another) {
                                                                    String oneTitle = "";
                                                                    String anotherTitle = "";
                                                                    try {
                                                                        oneTitle = CMSO.readPropertyObject(one, "Title", false).getValue("");
                                                                        anotherTitle = CMSO.readPropertyObject(another, "Title", false).getValue("");
                                                                    } catch (Exception e) {
                                                                        oneTitle = "";
                                                                        anotherTitle = "";
                                                                    }
                                                                    return oneTitle.compareTo(anotherTitle);
                                                                }
                                                            };
Collections.sort(subfolders, TITLE_ORDER);

Iterator<CmsResource> i = subfolders.iterator();
while (i.hasNext()) {
	CmsResource folder = i.next();
	// Do something, for example:
	out.println("<br />" + cmso.readPropertyObject(folder, "Title", false).getValue("NO TITLE"));
}
}

Hope this helps. :)

Cheers,
Paul

-----Original Message-----
From: opencms-dev-bounces at opencms.org [mailto:opencms-dev-bounces at opencms.org] On Behalf Of gnorrus at libero.it
Sent: 14. januar 2013 13:03
To: opencms-dev at opencms.org
Subject: [opencms-dev] Get folder list ordered by title

Hi

Which is the scriptlet code to get the list of folders under a specific directory, ordered by title?

thanks
_______________________________________________
This mail is sent to you from the opencms-dev mailing list To change your list options, or to unsubscribe from the list, please visit http://lists.opencms.org/cgi-bin/mailman/listinfo/opencms-dev






More information about the opencms-dev mailing list