Iterate Node Properties in Alfresco Freemarker File

In my past projects most of the times alfresco being used in backend to store contents and front end is built using portal or other web application now whenever this kind of architecture exist there is going to be requirement where we need to expose alfresco repository to front-end system. Most of the times when we pull alfresco contents from alfresco repository through webscript based on some business logic we get list of nodeRefs or nodes.

For extracting information from this list of nodes we have two options

1)    Iterate through nodes in javascript or java controller of webscript and pass it to output ftl file.

2)    Pass list of nodes directly to ftl file and iterate through those nodes in ftl

I normally prefer second approach reason is that in that approach we could produce multiple different output formats from that list like xml,csv,json etc… which is not possible in first approach as we have already prepared that output content in javascript itself so we need to create new webscript if we want to have different output format.

Now whenever we are having list of noderef we need to extract set of properties from those nodes, Most of the times we have to deal with out-of-box properties as well as custom properties.

Here is a piece of code which you can use to iterate through all those properties in ftl file.

 

  <table>
      <#-- Get a list of all the property names for the document -->
      <#assign props = document.properties?keys>
      <#list props as t>
         <#-- If the property exists -->
         <#if document.properties[t]?exists>
            <#-- If it is a date, format it accordingly -->
            <#if document.properties[t]?is_date>
               <tr><td>${t} = ${document.properties[t]?datetime}</td></tr>
            
            <#-- If it is a boolean, format it accordingly -->
            <#elseif document.properties[t]?is_boolean>
               <tr><td>${t} = ${document.properties[t]?string("yes", "no")}</td></tr>
            
            <#-- If it is a collection, enumerate it -->
            <#elseif document.properties[t]?is_enumerable>
               <tr><td>${t} = <#list document.properties[t] as i>${i} </#list></td></tr>
            
            <#-- Otherwise treat it as a string -->
            <#else>
               <tr><td>${t} = ${document.properties[t]}</td></tr>
            </#if>
         </#if>
      </#list>
   </table>

Summary: Above piece you can inject in your ftl files and it is generic enough to be reusable across different output formats.

Looking for quality Alfresco Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , , , | Posted under Alfresco | RSS 2.0

Author Spotlight

mitpatoliya

mitpatoliya

I love opensource technologies working with those technologies from the time I have stepped in to the Software Industry. Alfresco CMS is my area of expertise. I have worked on various complex implementations which involved integration of Alfresco with other technologies, extensively worked with JBPM workflows and Webscripts.

Leave a Reply

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


− 2 = 7

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>