This sample javascript iterates the current node and child nodes. It also creates a simple log file of the process.
此示例javascript迭代当前节点和子节点。它还创建进程的简单日志文件。

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.
在这个示例中,将为{custom.model}mytype或{custom.model}myothertype类型的文档添加新的切面{custom.model}myaspect。

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.
使用适当的名称(如UpdateDocType.js)将脚本保存在数据字典/脚本中。
导航到要开始运行脚本的空间,选择“查看详细信息”,选择“管理规则”,然后按照向导使用选定的操作执行脚本。

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.
注意:这个脚本在一个大型存储库上运行需要很长时间,特别是从公司总部运行时。如果在大型repo上使用它,请首先在测试环境中进行尝试。也可以考虑分批运行,也就是从子空间到公司总部多次启动。

//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);
 }
}
文档更新时间: 2020-02-16 16:32   作者:凌云文档