JS-Console - How to update node properties without altering modifier/modified date?

cancel
Showing results for 
Search instead for 
Did you mean: 
neilecker
Active Member II

JS-Console - How to update node properties without altering modifier/modified date?

Jump to solution

I'd like to update a node property on all documents in a particular folder but would rather it not change the modifier to "admin" or update the modified date.  Can this be done directly from the Javascript Console?  It's a small thing so I would prefer not to have to create some server-side scripting for it.

Along the same lines, there's a parameter for "Run script as:" under the "Script execution parameters" tab in the console.  I've read that this should be set to "System" for elevated privileges but doing that also set the modifier to "admin" when the script updates node properties.  It would have been nice if using "System" would skip the audit part and leave the modified date and modifier alone.

The next best thing would be to have the modifier be set to a user that made sense rather than "admin" so I tried changing this to a specific user but again that sets the modifier to "admin" on the node.

Thanks,

Neil

1 Solution

Accepted Solutions
afaust
Master

Re: JS-Console - How to update node properties without altering modifier/modified date?

Jump to solution

The feature that sets the creator/modifier to the current user is a core part Alfresco and can / should generally not be disabled by any addon / extension. JavaScript Console is a "good example" in this regard as it does not alter / interfere with core parts unrelated to its own functionality.

The feature can be controlled like any other behaviour (though it is not implemented as one). By getting a reference to the policyBehaviourFilter bean, you can enable/disable the "cm:auditable" bound functionality for the current transaction. This enablement/disablement is checked by the core part handling creator/modifier, and those are only modified if the "cm:auditable" bound functionality has not been explicitly disabled.

You can retrieve any Spring bean from within JavaScript Console using the JavaScript-Java-interop capabilities of Rhino. I have a few examples of that in a Gist. Disable the functionality by passing "cm:auditable" as a proper Java QName instance.

View solution in original post

2 Replies
afaust
Master

Re: JS-Console - How to update node properties without altering modifier/modified date?

Jump to solution

The feature that sets the creator/modifier to the current user is a core part Alfresco and can / should generally not be disabled by any addon / extension. JavaScript Console is a "good example" in this regard as it does not alter / interfere with core parts unrelated to its own functionality.

The feature can be controlled like any other behaviour (though it is not implemented as one). By getting a reference to the policyBehaviourFilter bean, you can enable/disable the "cm:auditable" bound functionality for the current transaction. This enablement/disablement is checked by the core part handling creator/modifier, and those are only modified if the "cm:auditable" bound functionality has not been explicitly disabled.

You can retrieve any Spring bean from within JavaScript Console using the JavaScript-Java-interop capabilities of Rhino. I have a few examples of that in a Gist. Disable the functionality by passing "cm:auditable" as a proper Java QName instance.

neilecker
Active Member II

Re: JS-Console - How to update node properties without altering modifier/modified date?

Jump to solution

Thanks very much!  This pointed me in the right direction and between your explanation, the gist you linked to, and a few other resources I was able to do exactly what I needed.

Here's a sample of what I was trying to do for anyone who might be interested in the future.  The query is obviously specific to our own content model but the idea is there. 

var context = Packages.org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext();
var model = Packages.org.alfresco.model.ContentModel;
var policyBehaviourFilter = context.getBean('policyBehaviourFilter', Packages.org.alfresco.repo.policy.BehaviourFilter);

setProperty();

function setProperty() {

     var searchObj = {
          query: 'PATH:"/app:company_home/st:sites/cm:long-term-care/cm:documentLibrary/cm:Long_x0020_Term_x0020_Care_x0020_Manuals//*" AND TYPE:"gry:content" AND @gry:siteLocation:""',
          language: 'fts-alfresco',
          page: {maxItems: 10}
     }

     var results = search.query(searchObj);
     var len = results.length;
     logger.log("Total: " + len);

     for (var i = 0; i < len; i++) {

          var node = results[i];

          if (node.hasAspect("rma:record")) {
               logger.log("Record - Not Updating Property");
          } else {

               try {
                    logger.log("Setting site location: " + node.name + " " + node.nodeRef);
                    policyBehaviourFilter.disableBehaviour(node.nodeRef, model.ASPECT_AUDITABLE);
                    
                    node.properties['gry:siteLocation'] = 'long-term-care';
                    node.save();
               } catch (e) {
                    logger.log(e.message);     
               } finally {
                    policyBehaviourFilter.enableBehaviour(node.nodeRef, model.ASPECT_AUDITABLE);
                    
                    //Log modifier and modified date
                    logger.log("Modifier: " + node.properties['cm:modifier'] + " Modified: " + node.properties['cm:modified']);
               }

          }     
     }
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍