Rename document with custom model with javascript

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
castelsarrasin
Active Member

Rename document with custom model with javascript

Jump to solution

Hi,

I have alfresco community 5.2f

 

Actually i have files uploaded with custom models.

I want with a javascript rename it with metadata value.

Is someone can tell me how i can do this ?

My exemple is this :

var docObjet = get value of custom model;
var docName = document.name;
document.name = "BC" + "_" + docObjet + ".pdf";
document.save()

 

In red what i need.

Exemple i have metadata on test:BDC-propertie

I need the javascript get the value of the test:BDC-propertie to rename with that value the file.

 

Can you help me plz ?

I can give more details if needed.

1 Solution

Accepted Solutions

Re: Rename document with custom model with javascript

Jump to solution

document.properties["test:BDC-propertie"]

Software Engineer in Alfresco Search Team.

View solution in original post

4 Replies

Re: Rename document with custom model with javascript

Jump to solution

document.properties["test:BDC-propertie"]

Software Engineer in Alfresco Search Team.

View solution in original post

castelsarrasin
Active Member

Re: Rename document with custom model with javascript

Jump to solution

You're my miracle !

Thank you very much !!

 

I i can abuse, do you know how can i create folder containing the name of some properties and push the files who get the same properties value in this new folder ?

Exemple :

File 1 have properties-test= XXX

File 2 have properties-test= XXX

then create folder "XXX" 

then push those files in "XXX" folder

 

Tell me if i abuse but actually i'm just happy to have this script working like a charm !!!

Thank you so much !!!

 

P.S : For others my javascript is like this now.

var numero = document.properties["test:BDC-SEDIT-propriete"];
var tiers = document.properties["test:BDC-SEDIT-LibelleTiers"];
var libelle = document.properties["test:BDC-SEDIT-ObjetCommande"];
document.name = "BC" + "_" + libelle + "_" + tiers + "_" + numero + ".pdf";
document.save()

 

 

Moderator
Moderator

Re: Rename document with custom model with javascript

Jump to solution

You can write a repository js webscript to search for all the documents within your scope and then get the name property from each document, change the value and save each docuement. Note that, if any document fails to save due to any run time issues e.g. Integrity Violations etc. then whole operation will fail. It may happen only if a document has corrupted metadata otherwise you would not see this problem.

Here is a sample script e.g. update-property-value.get.js: This script is generic in nature and can be used to update any existing properrty value from any site, any folder path.

You can create your own webscript by taking the reference of this one as per your need. This one is just a sample to guide you. 

main();

function main(){
	var propertyName =  args["propertyName"];
    var existingPropertyVal = args["existingPropertyVal"];
    var newPropertyVal = args["newPropertyVal"];
    var objectTypes = args["objectTypes"];
	var siteShortName = args["siteShortName"];
    var folderPath = args["folderPath"]; //Optional param
	var skipCount = (args["skipCount"]==null || args["skipCount"]==undefined)?0:args["skipCount"]; //Optional param, default to 0
	var maxCount = (args["maxCount"]==null || args["maxCount"]==undefined)?1000:args["maxCount"]; //Optional param, default to 1000
    var additionalQuery = args["additionalQuery"]; //Optional param
  	
	var successNodes = [];
	var failedNodes = [];
  
    var query = buildQuery (siteShortName, objectTypes, folderPath, propertyName, existingPropertyVal, additionalQuery)
	var page = {
			skipCount : parseInt(skipCount),
			maxItems : parseInt(maxCount)
	};

	var searchQuery = {
			query : query,
			language : "fts-alfresco",
			page : page
	};
	
 	logger.log("Executing SearchQuery: "+query)
 	var nodes = search.query(searchQuery);
 	logger.log("Total Nodes: "+nodes.length)
      
    for each(node in nodes) {
		try {
          
            if(node.name.endsWith(".pdf")) {
              logger.log("Updating the "+propertyName+" of node: "+node.name);
              node[propertyName] = newPropertyVal;
              node.save();
              successNodes[node.nodeRef] = node.name+" - Success";
            }
		} catch(ex) {
			logger.log("Exception occurred: "+ex.message);
			failedNodes[node.nodeRef] = node.name+" - "+ex.message;
		}
	}
	model.successNodes = successNodes;
	model.failedNodes = failedNodes;
}

function buildQuery(siteShortName, objectTypes, folderPath, propertyName, existingPropertyVal, additionalQuery) {
    var query = 'PATH:"/app:company_home/st:sites/cm:' +siteShortName;
  
    if(folderPath=="") {
       query = query + '/cm:documentLibrary//*"';
    } else {
       query = query + '/cm:documentLibrary/';
       var pathTokens = folderPath.split('/');
       for (var each=0; each<pathTokens.length; each ++) {
         query = query +'cm:'+search.ISO9075Encode(pathTokens[each].trim())+'/';
       }
       query = query + '/*"';

    }
  
    var objectTypeArr = objectTypes.split(',');
	var arrayLength = objectTypeArr.length;

	if(arrayLength == 1) {
		query = query +' AND (TYPE:"' +objectTypeArr+'")';
	} else {
		query = query +' AND (';
		for (var each=0; each<arrayLength; each ++) {
			query = query + 'TYPE:"'+objectTypeArr[each].trim()+'"';
			if(each != arrayLength-1) {
				query = query + ' OR ';
			}
		}
      
		query = query +')';
	}
  
    //Append property and value query.
    if(!!existingPropertyVal) {

        var propertyParts = propertyName.split(':');
        if (propertyParts.length ==1 && (propertyName=="mimetype" || propertyName=="size")) {
           
           query = query +' AND @content.'+propertyName+':"'+existingPropertyVal+'"';
        } else {
           query = query +' AND @'+propertyParts[0]+'\\:'+propertyParts[1]+':"'+existingPropertyVal+'"';
        }
    }
	
    //Append additionalQuery query if any
	if (!!additionalQuery) {//if not null then append
		query = query +' AND '+additionalQuery;
	}
    return query;
}

 In your case you want to update name property ends with "pdf". By passing the params as given below:

var propertyName = "cm:name";
var existingPropertyVal = "xyz";
var newPropertyVal = "abc";
var objectTypes = "test:BDC-propertie";
var siteShortName = "test-site"; //
var folderPath = "docs/docs1" // A folder path under document library
var additionalQuery = '=@cm\\:name:"*.pdf"' //File name ends with pdf

You would be able to update the existing value of name property.

 

 

Moderator
Moderator

Re: Rename document with custom model with javascript

Jump to solution

[EDIT:] i did quick reply and did not notice that it was already resolved. I don't see option to delete comment.