You are here
Creating a SiteTree webscript
One of the things
we needed to do was create a script that displayed the site hierarchy. I tried in vain to get this working with the built in webscripts that come with Alfresco, so in the end, decided to build my own. The steps to reproduce are as follows.
On the Alfresco side
First of all we need a script that will return the site structure. Unfortunately the only script I could find within Alfresco to do this, returned information back in CMIS format. Now whilst that should be useful, for a newbie, it was a pit of woe. Hopefully once Dave Caruana's CMIS parser comes in, this will become a lot easier to handle. So basically, what this script does is read in the site structure, and return back a JSON stream for processing at the other end.
A quick word about the above Freemarker template. As there may be an unlimited number of children to a folder, I needed some kind of recursive function to keep reading until there were no children left and display that in JSON format. The #macro section accomplishes this.
Once we've completed that, we then need to utilise that in our SpringSurf application. The scripts are as follows.
Again, the above freemarker recursively reads the site structure and formats it into HTML unordered lists for displaying within a site.
With everything that you develop, there's always going to be something that you miss that causes you endless hours of heartache.
- If there are no child nodes, nothing gets returned.
As you'll see in the Alfresco side script, I check to see whether the current node being processed has any children, if it does, then a new element called nodes is inserted and the children listed within it. However, if no children are found, then it doesn't get created. This caused a problem on the SpringSurf end in that the template failed everytime it tried to recurse. Enter the ?? operator in Freemarker that checks for the existence of a variable. If it exists, the if statement equates to true and everything within the tags gets actioned, otherwise, it is ignored. Simple enough fix for two days of head scratching!
- Make sure you are passing the same type of variable at each recursion.
All was going well with the top level, but I was getting confused when passing the next level into the same procedure. Therefore make sure that you are passing what the function is expecting to get!
The SpringSurf script requires a user level of authentication in order to run. The SpringSurf application deals with this nicely (if configured to do so!). However the template I was working with hid much of the errors I was getting, so I decided to try and hit the script directly via the console. However, because no authentication was taking place when trying to retrieve the JSON stream, I was getting XML errors back that the script was complaining about. Of course, because I had no visibility to what was being returned, it made debugging very difficult.