cancel
Showing results for 
Search instead for 
Did you mean: 

Creating categories from Javascript

samuel_penn
Champ in-the-making
Champ in-the-making
Hi,

I'm currently having a look at what can be done with categories, and from I can see, it should be possible to create a new classification scheme outside of cm:generalclassifiable, and add categories and sub-categories to this with the Javascript API. However, I'm having issues getting my head around exactly how the APIs should be used. Is there any example code on how to do this? I'm using Alfresco 2.2.

So, far, I've created an aspect which extends cm:classifiable, which appears in the list returned by getAllClassificationAspects(). This is defined as:


        <aspect name="test:classifications">
            <title>Test Classifications</title>
            <parent>cm:classifiable</parent>
            <properties>
                <property name="test:categories">
                    <title>Categories</title>
                    <type>d:category</type>
                    <mandatory>false</mandatory>
                    <multiple>true</multiple>
                    <index enabled="true">
                        <atomic>true</atomic>
                        <stored>true</stored>
                        <tokenised>true</tokenised>
                    </index>
                </property>
            </properties>
        </aspect>

If I call classification.createRootCategory("test:classifications", "TestCats") from a web script then I get:


Failed to execute script 'workspace://SpacesStore//Company Home/Data Dictionary/Web Scripts/com/centrom/test.get.js': Failed to execute script 'workspace://SpacesStore//Company Home/Data Dictionary/Web Scripts/com/centrom/test.get.js': Wrapped org.alfresco.error.AlfrescoRuntimeException: Missing classification: {http://centrom.com/testing}classifications (AlfrescoScript#20)

Not sure if I've missed a step, or made a mistake. The wiki page on Classification_And_Categories seems to imply that category roots are created in a particular location, but createRootCategory() doesn't have a means of specifying this, which makes me wonder whether it is the right thing to use.

Pointers to examples, or mistakes I've made, equally welcomed.

Thanks,
Sam.
3 REPLIES 3

merelv
Champ in-the-making
Champ in-the-making
I managed to populate my tree using a webscript by first creating the category root node in a separate transaction:


var check = search.luceneSearch("@cm\\:name:\"MYCLASSIFICATION\" AND TYPE:\"category\"");
if(check.length == 0) {
    // /{http://www.alfresco.org/model/content/1.0}categoryRoot
    var root = search.luceneSearch("@cm\\:name:\"categories\" AND TYPE:\"category_root\"")[0];
    root.createNode("MYCLASSIFICATION", "{http://www.alfresco.org/model/content/1.0}category", "cm:categories");
}

I first check if this initialization was already correctly done, if not I create a childnode in the category_root node with the name categories with the associationtype categories.

edwin_tampubolo
Champ in-the-making
Champ in-the-making
hi,
is it possible to create category with the description from javascript?

edwin_tampubolo
Champ in-the-making
Champ in-the-making
this part should be work
var props = new Array();
                  props["cm:name"] = categoryName;
                  if(categoryDescription!=null){
                     props["cm:description"] = categoryDescription
                  }
                  model.newNode = nodeParent.createNode(categoryName, "{http://www.alfresco.org/model/content/1.0}category", props, "cm:subcategories");

and here's the complete javascript
var parentNodeRef = args.categorynoderef;
var categoryName = args.name;
var categoryDescription = args.description;
var rootNodeRef = args.rootcategorynoderef;
var resultString = "Action failed";
var resultCode = 501;
try {
   if ((categoryName == null) || (categoryName == "")) {
      resultString = "Category must have a Name";
   }  else {
      if ((parentNodeRef != null) && (parentNodeRef != "")) {
         var nodeParent = search.findNode(parentNodeRef);
         if (nodeParent != null) {
            //Check if already exists a category with the same name as the given one
            var root = search.findNode(rootNodeRef);
            if (root != null){
               var query = "+PATH:\""+root.qnamePath+"//*\" +TYPE:\"cm:category\"";
               query += " +@cm\\:name:\""+categoryName+"\"";
               var nodes = search.luceneSearch(query);
               var nodeExists = false;
               for each(node in nodes) {
                  if (node.name == categoryName) {                   
                     nodeExists = true;
                     break;
                  }
               }
               if (nodeExists){
                  //Already exists with same name
                  resultString = "Category with the name '"+categoryName+"' already exists.";
               } else {
                  //Doesn't exist, then create it
                  var props = new Array();
                  props["cm:name"] = categoryName;
                  if(categoryDescription!=null){
                     props["cm:description"] = categoryDescription
                  }
                  model.newNode = nodeParent.createNode(categoryName, "{http://www.alfresco.org/model/content/1.0}category", props, "cm:subcategories");
                  resultString = "New category created";
                  resultCode = 200;
               }
            } else{
               resultString = "Invalid root node";
            }
         } else {
            resultString = "Invalid parent node";
         }
      } else {
         resultString = "Invalid param";
      }  
   }
} catch (e) {
   resultString = "Action failed due to an exception: "+e.toString();
}
model.resultString = resultString;
model.resultCode = resultCode;