cancel
Showing results for 
Search instead for 
Did you mean: 

Webscript to retrieve Category Tree

purplesky
Champ in-the-making
Champ in-the-making
I need to retrieve a all categories from Alfresco (Languages, Regions…). I'm using REST. Can someone guide me into what script might help here?
1 REPLY 1

sufo
Star Contributor
Star Contributor
This is what I have created:

categories.get.desc.xml
<webscript>
   <shortname>Get categories</shortname>
   <description><![CDATA[Gets all the categories defined (aspect cm:generalclassifiable if aspect not specified) or if the path is specifed only categories under that path.]]></description>
   <url>/ta/categories?path={path?}&amp;recurse={recurse?}&amp;aspect={aspect?}</url>
    <arg>
        <shortname>path</shortname>
        <description><![CDATA[category path (for example - 'Software Document Classification/Software Descriptions')]]></description>
    </arg>
    <arg>
        <shortname>recurse</shortname>
        <description><![CDATA[if 0, return only direct subcategories of the category, otherwise return whole tree of subcategories]]></description>
        <default>0</default>
    </arg>
    <arg>
        <shortname>aspect</shortname>
        <description><![CDATA[classification aspect]]></description>
        <default>cm:generalclassifiable</default>
    </arg>
   <format default="json">argument</format>
   <authentication>user</authentication>
</webscript>
categories.get.json.ftl
<#escape x as jsonUtils.encodeJSONString(x)>
<#macro dumpCategories categories recurse>
   <#list categories as c>
      {
         "nodeRef": "${c.nodeRef}",
         "name": "${c.name}",
         "description": "${(c.properties.description!"")}",
         <#if c.children?size &gt; 0 && recurse != 0>"children": [<@dumpCategories c.children recurse/>],</#if>
         "userAccess":
         {
            "create": ${c.hasPermission("CreateChildren")?string},
            "edit": ${c.hasPermission("Write")?string},
            "delete": ${c.hasPermission("Delete")?string}
         }
      }<#if c_has_next>,</#if>
   </#list>
</#macro>
<#if categories.error?has_content>
{"error":"${categories.error.msg}"}
<#else>
{
   "results": ${categories.items?size?c},
   <#if categories.path?has_content>"path": "${categories.path}",</#if>
   <#if categories.recurse?has_content>"recurse": ${categories.recurse?c},</#if>
   "items":
   [
      <@dumpCategories categories.items categories.recurse!0/>
   ]
}
</#if>
</#escape>
categories.get.js
model.categories = getCategories();

function getCategories() {
   try {
      var aspect = args["aspect"] == null ? "cm:generalclassifiable" : args["aspect"];
      var nodes = roothome.childrenByXPath("/cm:categoryRoot/" + aspect);
      if(nodes != null && nodes.length == 1) {
         var path = args["path"];
         var recurse = args["recurse"] == null ? 0 : parseInt(args["recurse"]) == 0 ? 0 : 1;
         if(path == null || path == "") {
            return({"items" : nodes[0].children, "recurse" : recurse});
         } else {
            nodes = nodes[0].childByNamePath("/" + path);
            if(nodes != null) {
               return({"items" : nodes.children, "recurse" : recurse});
            } else {
               return({"error" : {"number" : 200, "msg" : "Subcategory with path " + aspect + "/" + path + " does not exist."}});
            }
         }
      } else {
         return({"error": {"number":100, "msg":"Root category with aspect " + aspect + " does not exist."}});
      }
   } catch(e) {
      status.setCode(status.STATUS_INTERNAL_SERVER_ERROR, e.toString());
      return;
   }
}