cancel
Showing results for 
Search instead for 
Did you mean: 

Accessing elements of custom content type properties in .ftl

slothrop
Champ in-the-making
Champ in-the-making
I have a custom content type with properties defined.  These properties have the standard elements, i.e.,

<property name="mol:modesVibration">
   <title>Vibrational Modes</title>
<type>d:boolean</type>
</property>

I would like to be able to access the values of the title and type in the Freemarker response template of a web script but have been unable to do so.  Is it possible?
8 REPLIES 8

openpj
Elite Collaborator
Elite Collaborator
If you use FreeMarker I suppose you can execute a Lucene query on Alfresco in this way to get all of your custom properties:


<#assign docs=companyhome.childrenByLuceneSearch[query]>
<#list docs as child>
<#if child.properties["cm:yourCustomProperty"]?exists && child.properties["cm:yourCustomProperty"]!="">
       ${message(child.properties["cm:yourCustomProperty"])}
</#if>

Hope this helps.

slothrop
Champ in-the-making
Champ in-the-making
Thanks for your reply Pj but this will get my the property when what I am after is the title and the type.  Once I have the property,

child.properties[cm:yourCustomProperty"].title 

gives the following error:

freemarker.template.TemplateException - Expected hash. child.properties[cm:yourCustomProperty"] evaluated instead to freemarker.template.SimpleScalar on line  …

child.properties[cm:yourCustomProperty"].type 

gives a similar error.

jayjayecl
Confirmed Champ
Confirmed Champ
you missed a double-quote :
child.properties[HEREcm:yourCustomProperty"]

slothrop
Champ in-the-making
Champ in-the-making
Thanks Jay but that's not the problem.

jayjayecl
Confirmed Champ
Confirmed Champ
The error is due to a problem of object type (ie Java Class).


Could you please provide us with the full log stacktrace, the whole freemarker template and the whole model ?

slothrop
Champ in-the-making
Champ in-the-making
The stack trace is:

Message: Wrapped Exception (with status template): Error during processing of the template 'Expected hash. node.properties["tst:testProperty"] evaluated instead to freemarker.template.SimpleScalar on line 13, column 15 in org/alfresco/test/test.get.html.ftl.'. Please contact your system administrator. 
  
Exception: freemarker.template.TemplateException - Expected hash. node.properties["tst:testProperty"] evaluated instead to freemarker.template.SimpleScalar on line 13, column 15 in org/alfresco/test/test.get.html.ftl.
  
freemarker.core.TemplateObject.invalidTypeException(TemplateObject.java:135)
freemarker.core.Dot._getAsTemplateModel(Dot.java:78)
freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
freemarker.core.Expression.getStringValue(Expression.java:93)
freemarker.core.DollarVariable.accept(DollarVariable.java:76)
freemarker.core.Environment.visit(Environment.java:208)
freemarker.core.MixedContent.accept(MixedContent.java:92)
freemarker.core.Environment.visit(Environment.java:208)
freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:179)
freemarker.core.Environment.visit(Environment.java:415)
freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
freemarker.core.Environment.visit(Environment.java:208)
freemarker.core.MixedContent.accept(MixedContent.java:92)
freemarker.core.Environment.visit(Environment.java:208)
freemarker.core.Environment.process(Environment.java:188)
freemarker.template.Template.process(Template.java:237)
org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:201)
org.alfresco.web.scripts.AbstractWebScript.renderTemplate(AbstractWebScript.java:505)
org.alfresco.web.scripts.DeclarativeWebScript.renderFormatTemplate(DeclarativeWebScript.java:241)
org.alfresco.web.scripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:147)
org.alfresco.repo.web.scripts.RepositoryContainer$2.execute(RepositoryContainer.java:319)
org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:320)
org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:227)
org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:368)
org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:390)
org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:273)
org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntime.java:261)
org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntime.java:139)
org.alfresco.web.scripts.servlet.WebScriptServlet.service(WebScriptServlet.java:116)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
java.lang.Thread.run(Thread.java:595)
  

The model is:

<?xml version="1.0" encoding="UTF-8"?>

<!– Test Model –>

<!– Note: This model is pre-configured to load at startup of the Repository.  So, all custom –>
<!–       types and aspects added here will automatically be registered –>

<model name="tst:testModel" xmlns="http://www.alfresco.org/model/dictionary/1.0">

   <!– Optional meta-data about the model –>  
   <description>Test Model</description>
   <author></author>
   <version>1.0</version>

   <imports>
        <!– Import Alfresco Dictionary Definitions –>
      <import uri="http://www.alfresco.org/model/dictionary/1.0" prefix="d" />
      <!– Import Alfresco Content Domain Model Definitions –>
      <import uri="http://www.alfresco.org/model/content/1.0" prefix="cm" />
   </imports>

   <!– Introduction of new namespaces defined by this model –>
   <namespaces>
      <namespace uri="http://www.alfresco.org/model/tst/1.0" prefix="tst"/>
   </namespaces>
   <constraints>
     
   </constraints>
   <types>
      <type name="tst:test">
         <title>Test Type</title>
         <parent>cm:content</parent>
         <properties>
            <property name="tst:testProperty">
               <title>Test Property</title>
               <type>d:text</type>
            </property>
         </properties>
      </type>
   </types>
   <aspects>
      
   </aspects>
</model>


The freemarker template is:

<html>
  <body>
    <img src="${url.context}/images/logo/AlfrescoLogo32.png" alt="Alfresco" />
   <br />
<p>${resultsetlength}</p>
<br />
<br />
    <table">
      <#list resultset as node>
        <tr>
        <td>${node.name}</td>
       <td><a href="${url.serviceContext}/api/node/content/${node.nodeRef.storeRef.protocol}/${node.nodeRef.storeRef.identifier}/${node.nodeRef.id}/${node.name?url}">Content</a></td><td>${node.properties["tst:testProperty"]}</td>
          <td>${node.properties["tst:testProperty"].type}</td></tr>
      </#list>
    </table>
  </body>
</html>

I don't get an error if I comment out the reference to node.properties["tst:testProperty"].type (or node.properties["tst:testProperty"].title).

rogier_oudshoor
Champ in-the-making
Champ in-the-making
Your tst:testProperty is a d:text. The stacktrace tries to tell you that you're treating it as a Hash (which is an object consisting of name-value pairs), but it is a scalar (i.e. a singular attribute). You will be able to use
child.properties[cm:yourCustomProperty"] 
only.

Now, i assume you want to retrieve the type and title as you specified in your content model. You can not do this directly in FTL. To access your content model, you need to code a Java Class to wrap around Alfresco's DictionaryService, and use Webscripting (not plain FTL!) to display. For this purpose, you should be able to use both a Java-back webscript or a BaseJavaScriptextension service class.

slothrop
Champ in-the-making
Champ in-the-making
Thanks Rogier.