Tuesday, July 24, 2018

How to Remove Versioning for a Share Site (and Keep it Enabled Elsewhere)

Some use cases may require versioning to be disabled in some sections of your Alfresco repository while enabled in other areas. A use case that comes to mind involves a Share site where many documents are constantly being written while there is no requirement to hold on to older versions.

By default, Alfresco has versioning enabled. Alfresco uses the versionable aspect (cm:versionable) as a flag of sorts to assign to documents that will retain older versions of content as it is changed. As long as versioning is enabled, Alfresco will require that the versionable aspect be retained for any document on creation and on update. If a document has the versionable aspect, all new updates to the document will be versioned out of the box with 1.0+ for major versions and 0.1+ for minor versions. In order to ensure the document however does not increment by version, you'll need to make sure the versionable aspect is not assigned to the document.

In fact, it's very simple to create a Share rule that looks like this:





Here we create a rule that removes the versionable aspect as soon as the document is created. This is very simple to do and works on Share UI upload or if you use one of Alfresco's file servers to add the document. But, the real test for this process is when the document is updated with new content. When a document has its content updated Alfresco adds the versionable aspect back. In order to counteract this behavior, you have to create another rule that removes the versionable aspect on *update* like so:



At this point, whenever a document is updated via the "Upload New Version" action or via FTP or WebDAV, a new "version" is created but this document no longer retains its old versions. The old versions then become orphan nodes. At this point, when the Contentstore Cleaner job is run (out of the box this runs every morning at 4:00 am local time) the orphan nodes are then removed. All orphaned content at this point are then moved to the contentstore.deleted folder where they can either be safely removed from the file system or restored if needed.

Now, it's very likely that you may have pre-existing documents that will already have the versionable aspect added along with multiple versions at this point. If you would like to keep these from being versionable going forward and delete the old versions you can write a JS script like so:


// Set the name of the Share Site:
var siteName = "site1";

logger.log("This script removes versionable aspects for all documents in the " + siteName + " Share site.");

// Query used to return all documents (cm:content) from the documentLibrary folder and all subfolders:
var query = 'PATH:"/app:company_home/st:sites/cm:'+siteName+'/cm:documentLibrary//*" AND TYPE:\"cm:content\"';
logger.log("The query being used is: " + query);

// "documents" is our array of document results
var documents = search.luceneSearch(query);

logger.log("The number of documents found is: " + documents.length + ".");

// Iterate through the document list:
for (var i=0; i < documents.length; i++) {
  logger.log("Document name: " + documents[i].name);

  // If the document has the versionable aspect, then remove it:
  if (documents[i].hasAspect("cm:versionable")) {
    logger.log(" * " + documents[i].name + " has the versionable aspect. It will be removed.");
    documents[i].removeAspect("cm:versionable");
  } else {
    logger.log(" * " + documents[i].name + " does *not* have the versionable aspect.");
  }
}


This script should have the .js extension and placed in the /Data Dictionary/Scripts folder. It can be run once using a Share rule. Just make sure the mimetype is set to JavaScript in the advanced properties for the document details for the file.

This process will allow for you to keep certain parts of your repository from keeping unneeded versions and keep your file system from filling up.