Iterate Nodes

Document created by resplin Employee on Jun 6, 2015
Version 1Show Document
  • View in full screen mode

Obsolete Pages{{Obsolete}}

The official documentation is at: http://docs.alfresco.com




Iterate nodes in repository


This sample javascript iterates the current node and child nodes. It also creates a simple log file of the process.
It is useful for a mass update or conversion when you want to make sure you touch each document present in the repository.

In this specific sample, it is used for updating documents of custom type {custom.model}mytype or {custom.model}myothertype with a new aspect {custom.model}myaspect.
But it can be used in many other cases, just replace what is inside if(n.isDocument) statement with your custom logic.

Save the script in Data Dictionary/Scripts with a suitable name like UpdateDocType.js.
The navigate to the space you want to start running the script from, select View Details, select Run Action, and follow the wizard to using selected action Execute a Script.

NOTE: This script will take a very long time to run on a large repository, especially if run from Company Home. If you use it on a large repo, first try it in a test environment. Also consider running in batches, that is start it several times from child spaces to Company Home.



//This script iterates the current folder and subfolders
//and creates a simple log file in userhome directory of the process.
var counter=0;
var logmessage='LOG OF TASKS\n';
var errmessage='ERRORS\n';

if(space.isContainer)
{
update(space);
}
logger.log(logmessage);
logfile = userhome.createFile('Repo Iterator Log ' + userhome.children.length +'.txt');
logfile.content=logmessage + 'UPDATED: '+counter.toString() +'\n' + errmessage;
logfile.properties.encoding = 'UTF-8';
logfile.properties.mimetype = 'text/plain';
logfile.properties.title = 'Repo Iterator Log';
logfile.properties.description = 'This is a repo iterator log file';
logfile.save();

function update(node)
{
for each (n in node.children)
{
  if(n.isDocument)
  {
   //Start your check
   if(n.type=='{custom.model}mytype'||n.type=='{custom.model}myothertype')
   {
    //Check if it is checked out, if so we cannot update
    var toupdate=true;
    if(n.hasAspect('{http://www.alfresco.org/model/content/1.0}lockable'))
    {
     if(n.properties['{http://www.alfresco.org/model/content/1.0}lockType']=='READ_ONLY_LOCK')
     {
      toupdate=false;
     }
    }
    if(!n.hasPermission('Write'))
     //The user running the script needs write access to document needing update.
     toupdate=false;
    if(toupdate)
    {
     try
     {
      if(!n.hasAspect('{custom.model}myaspect'))
      {
       n.addAspect('{custom.model}myaspect');
       //Counter to keep log of how many has been updated
       counter+=1;
       //Changes that adds aspects are persisted immediately
       //Any other changes, call n.save()
       //n.save();
      }
     }
     catch(err)
     {
      errmessage+=n.displayPath + ' ' + n.name +' \n';
      errmessage+=err.description + '\n\n';
     }
    }
    else
    {
     logmessage+='NOT UPDATED\:' + n.displayPath + ' ' + n.name +' \n';
    }
   }
  }
  if(n.isContainer)
   update(n);
}
}

JavaScript APIExamples

Attachments

    Outcomes