cancel
Showing results for 
Search instead for 
Did you mean: 

Listing documents in specified space in webscript FTL

benvercammen
Champ in-the-making
Champ in-the-making
I've been looking for a way to list all the documents in a space with a specified name ("User Homes" / "This Is The Space") in a freemarker template. My ultimate goal is to have a webscript that displays an overview of all the documents for a certain space (eg: go to /alfresco/service/overview and get a listing of the contents of "This Is The Space"). I was thinking that this shouldn't be too difficult, just fetch the space, list the children that are documents, and I'm done. That was over 2 hours ago…

These are some of my attempts:

overview.get.html.ftl:

    <#list companyhome.childByNamePath("User Homes/This Is The Space") as node>
      <tr>
        <td>${node.id}</td>
      </tr>
    </#list>
Which results in the following error:
freemarker.template.TemplateException - Expected method. companyhome.childByNamePath evaluated instead to freemarker.template.SimpleHash on …

So my reasoning is that the "companyhome.childByNamePath()" method returns a SimpleHash in this case, and tried to work with it as a SimpleHash, but to no avail. So after a while I gave up on this approach and started looking for an alternative…

A second and very different approach was by using XPath to find the space and list the documents. I found the following example:
http://wiki.alfresco.com/wiki/FreeMarker_Template_Cookbook#Using_XPath
and tried to modify it to serve my needs and eventually wound up with something like this:

overview.get.html.ftl:

  <#list companyhome.childrenByXPath["*//*[@cm:name='This Is The Space]"] as child>
    <tr><td><img src="/alfresco${child.icon16}"> ${child.properties.name}</td></tr>
  </#list>
[/code]
… also to no avail. So I've tested the (downgraded) example from the wiki:
[code]
<table>
<#list companyhome.childrenByXPath["*[@cm:name='Data Dictionary']/*"] as child>
   <#if child.isContainer>
      <tr><td><img src="/alfresco${child.icon32}"> ${child.properties.name}</td></tr>
   </#if>
</#list>
</table>
…and it turned out that it only displays the "RSS Templates" folder, and nothing else! This is strange, because there are a lot more spaces inside the "Data Dictionary" space, and it's the same when I leave out the "child.isContainer" check. For other base spaces, such as "User Homes" or "Web Projects", I didn't receive any results!

So now I am at a loss! Clearly something like this (displaying a list of items in an FTL file from a webscript) should be fairly easy. However, I didn't find any example, searching for the resulting Freemarker errors didn't bring up any solutions, and now it seems that even the basic example from the Alfresco wiki doesn't deliver on it's promise… Can anyone please tell me what I'm doing wrong or how I should go about this?

Thanks a lot!
5 REPLIES 5

mikeh
Star Contributor
Star Contributor
Try

    <#list companyhome.childByNamePath("User Homes/This Is The Space").children as node>
      <tr>
        <td>${node.id}</td>
      </tr>
    </#list>

Thanks,
Mike

benvercammen
Champ in-the-making
Champ in-the-making
Hi Mike,

I still get the same error:

Exception: freemarker.template.TemplateException - Expected method. companyhome.childByNamePath evaluated instead to freemarker.template.SimpleHash on line 21, column 12 in overview.get.html.ftl. 

Perhaps I should mention that I'm usting the 2.1 community edition?

Thanks for the reply though!

mikeh
Star Contributor
Star Contributor
Whoops! Sorry, we're in Freemarker not JavaScript…
<#list companyhome.childByNamePath["User Homes/This Is The Space"].children as node>
   <tr>
     <td>${node.id}</td>
   </tr>
</#list>

benvercammen
Champ in-the-making
Champ in-the-making
Hey Mike,

The syntax works better now, however, I still seem to have some sort of problem… I keep getting the following error:
freemarker.core.InvalidReferenceException - Expression companyhome.childByNamePath["User Homes"] is undefined
I've already stripped the path down to just "User Homes" but it seems that the space can not be found? Any further ideas?

benvercammen
Champ in-the-making
Champ in-the-making
Okay, never mind my last remark… It seems that I've set the authentication of my webscript to "guest", while somehow to reach "companyhome" (and/or the subsequent spaces) obviously the authentication should have been set set to "user", requiring me to log in as an existing user in order to run the webscript… After having changed the authentication and logging in, I do see the listings!

Well, another lesson learned the hard way..  Smiley Happy  Thanks for your help on the syntax Mike!