How to Programmatically Create a New Folder and Relocate a File Into It

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

How to Programmatically Create a New Folder and Relocate a File Into It

Hi Team,

I need some help regarding my following query.

Problem Statement:

Currently around 1500 files are wrongly placed in 255 folders with some misleading value of the metadata attributes as well.

Target State:

Relocate these 1500 files FROM their existing 255 folders TO 255 NEWLY CREATED folders and update the value of some of the medatadata attributes as well. 

Query

Is there any programatic way to achieve this target state by using Alfresco out of the box tools or programing interface?

Possible Options

  1. Writing SQL (Create, Update, Delete) to directly update Aflresco database (Create New Folder, Relocate Files, Update Metadata).
  2. Write a small program or utility which will call Alfresco APIs (If there are any) to acheive the target state.

Please dont hesitate to ask in case you need any further detail. Thanks.

 

3 Replies
Community Manager
Community Manager

Re: How to Programmatically Create a New Folder and Relocate a File Into It

Is this a one-time deal or something that you will be doing more than that? If you have access to the Share interface, you can write JSON to do this and then associate a rule with the parent folder of the original 255 folders and then run the rule to create the folders and move the files into them. Not sure if this would work, but it can be done.

Highlighted
MuhammadBashir
Member II

Re: How to Programmatically Create a New Folder and Relocate a File Into It

Thank you for your help, it is really appriciated.

Yes, this is one-time deal.

Can we alter the meta deta attribute values of the moving file as well with this technique?

If you dont mind can you please write a sample JSON rule which is going to meet my requirment?

In case there is any helpful article which descrites how to write JSON rule then please share it.

roberto_gamiz
Established Member II

Re: How to Programmatically Create a New Folder and Relocate a File Into It

Hello,

Using out of the box tools you could create a JavaScript and executing it via a rule in the parent of the existing folders. Here are a very simple approach:

// this is the root of the target
var targetRoot = companyhome.childByNamePath("Venzia");

function getTargetName(name) {			
	// Here you have to add the logic to found the name of the correct space.
	return 'Target ' + name;
}

function getTargetSpace(name) {			
	var targetSpaceName = getTargetName(name);	
	var newSpace = targetRoot.childByNamePath(targetSpaceName);
	
	if(newSpace === null) {
		newSpace = targetRoot.createFolder(targetSpaceName);
	}
	
	newSpace.properties.title = 'Creado';
	newSpace.save();
	
	return newSpace;
}

// document is the node in wich the rule is executing
var currentSpace = document;

var targetSpace = getTargetSpace(document.properties["cm:name"]);
var files = currentSpace.getChildren();

for(var i in files){
	var newFile = files[i].copy(targetSpace);	
	
	logger.log(newFile.properties["cm:name"]);
	
	newFile.properties.title = 'Copiado';
	newFile.save();
}

Save this code in a file script.js and upload it in the Alfresco space:

Company Home > Dictionary > Scripts.

Follow this instructions to create a rule in the parent  of the old folders:

https://docs.alfresco.com/5.2/tasks/library-folder-rules-define-create.html.

Once you have the rule execute it from Manage Rules page.

Of course here we are doing some assumptions:

  • All the old spaces are have the same parent.
  • The files are placed in the old spaces not in any subspace.
  • Both old and new root folder are placed in Company Home.

Regards