How to change document type via javascript console

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

How to change document type via javascript console

Jump to solution

Hi Team,

I have created one document(test.txt) as cm:content type via share UI.

I have changed its type to one of the custom type "dc:whitePaper" using change type action on document details page.

Now, i want to convert the document to cm:content type via javascript.

I have written below js code for this.

var query = '(TYPE:"dc:whitePaper")';
var queryDef = {query: query, store: "workspace://SpacesStore", language: "fts-alfresco", page: {maxItems: 200000, skipCount: 0}};
var results = search.query(queryDef);

logger.log(results.length);

for each(var document in results) {
	logger.log(document.properties.name);
	
	logger.log(document.type);
	
	logger.log(document.typeShort);

	
	if(document.type == "{http://www.democo.com/model/document/1.0}whitePaper")
	{
		document.typeShort = "cm:content";
		logger.log("after  "+document.typeShort);
		document.save();
		
		logger.log("saved");
	}
}

I didn't find any API for javascript that can change the content type of the document.

If javascript doesn't provide this API, can we do it via java based webscript?

Any other suggestions please.

Thanks in advance!

1 Solution

Accepted Solutions
abhinavmishra14
Advanced

Re: How to change document type via javascript console

Jump to solution

I would try this way instead @hardik_thakkar :

var query = '(TYPE:"dc:whitePaper")';
var queryDef = {query: query, store: "workspace://SpacesStore", language: "fts-alfresco", page: {maxItems: 200000, skipCount: 0}};
var results = search.query(queryDef);

logger.log("Total count: "+ results.length);
for each(var document in results) {
	logger.log("Name: " + document.properties.name);
	
	logger.log("Type: " + document.type);
	
	if(document.type == "{http://www.democo.com/model/document/1.0}whitePaper")
	{
		var ctxt = Packages.org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext();
		//make sure to use lower case bean id for nodeService or else you will get auth errors. 
var nodeService = ctxt.getBean('nodeService', org.alfresco.service.cmr.repository.NodeService); var QName = Packages.org.alfresco.service.namespace.QName; var nodeTypeContent = QName.createQName("{http://www.alfresco.org/model/content/1.0}content"); nodeService.setType(document.nodeRef, nodeTypeContent); } }

 

You can also check bottom of this post: https://javaworld-abhinav.blogspot.com/2020/06/change-type-of-custom-content-types.html

 

~Abhinav
(ACSCE, AWS SAA, Azure Admin)

View solution in original post

3 Replies
angelborroy
Alfresco Employee

Re: How to change document type via javascript console

Jump to solution

Use the specializeType method of the document object:

http://dev.alfresco.com/resource/docs/java/org/alfresco/repo/jscript/ScriptNode.html#specializeType-...

Hyland Developer Evangelist
hardik_thakkar
Active Member II

Re: How to change document type via javascript console

Jump to solution

Hi @angelborroy 

 

I have tried the below code, but its not allowing to change the type.

var node = search.findNode("workspace://SpacesStore/af0e5e5e-6ced-4c6c-92ce-1e45a2a37853");

logger.log(node.name);
logger.log(node.specializeType("{http://www.democo.com/model/document/1.0}whitePaper"));

I have created one document of cm:content type and now, i want to change it with custom one.

It gives output as false.

If possible, can you please share any example for that?

Thanks in advance!

abhinavmishra14
Advanced

Re: How to change document type via javascript console

Jump to solution

I would try this way instead @hardik_thakkar :

var query = '(TYPE:"dc:whitePaper")';
var queryDef = {query: query, store: "workspace://SpacesStore", language: "fts-alfresco", page: {maxItems: 200000, skipCount: 0}};
var results = search.query(queryDef);

logger.log("Total count: "+ results.length);
for each(var document in results) {
	logger.log("Name: " + document.properties.name);
	
	logger.log("Type: " + document.type);
	
	if(document.type == "{http://www.democo.com/model/document/1.0}whitePaper")
	{
		var ctxt = Packages.org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext();
		//make sure to use lower case bean id for nodeService or else you will get auth errors. 
var nodeService = ctxt.getBean('nodeService', org.alfresco.service.cmr.repository.NodeService); var QName = Packages.org.alfresco.service.namespace.QName; var nodeTypeContent = QName.createQName("{http://www.alfresco.org/model/content/1.0}content"); nodeService.setType(document.nodeRef, nodeTypeContent); } }

 

You can also check bottom of this post: https://javaworld-abhinav.blogspot.com/2020/06/change-type-of-custom-content-types.html

 

~Abhinav
(ACSCE, AWS SAA, Azure Admin)