Search code examples
categoriesalfrescorules

Alfresco Rule to Remove (unlink) Category from File


In Alfresco users can use rules to link categories to files. There isn't an option to unlink categories from a file.

How do I programmatically remove (unlink) categories from a file without removing the classification aspect?

If a script is required, do you have an example?

I am using Alfresco 7.0 Share/Community version


Solution

  • take a look into the node browser: Alfresco stores the categories on a node as an array of nodeRefs to the category nodes.

    If you want to remove specific category from a node you need to save the array without the nodeRef for that category.

    To illustrate that: the following example removes the category /Regions/EUROPE from a given document node:

    var categories= document.properties["cm:categories"];
    
    for (var i = 0; i < categories.length; i++) {
        var categoryPath = categories[i].displayPath + '/' + categories[i].name;
        logger.log(categoryPath);
        
        if (categoryPath == '/categories/General/Regions/EUROPE'){
            categories.splice(i, 1)
        }
    }
    
    document.properties["cm:categories"]= categories;
    document.save();