Expand my Community achievements bar.

SOLVED

How to add a property to existing AEM node

Avatar

Level 2

Hi I want to add a property in some nodes in AEM, those nodes were created using a template and I already added that property to the template. The question is how can I change the existing nodes created using that template?

Here's my code:

String queryString = "SELECT [jcr:path] FROM [cq:Page] AS s WHERE ISDESCENDANTNODE(/content/pagefolder1])";

ResourceResolver resourceResolver = request.getResourceResolver();

Session session = resourceResolver .adaptTo(Session.class);

QueryManager queryManager =sessopm.getWorkspace().getQueryManager();

Query query = queryManager.createQuery(queryString . Query.JCR_SQL2);

QueryResult queryResult = query.execute();

NodeIterator nodes= queryResult.getNodes();

While(nodes.hasNext()){

Node node = nodes.nextNode();

if(!node.hasProperty("NewProperty")){

JcrUtil.setProperty (node, "NewProperty", true); } }

when I run the code, I am getting javax.jcr.nodetype.ConstraintViolationException : No matching property defintion: NewProperty = true

Please advise, thanks

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @HanL 

Search jcrContent node and restrict serach for the template type
e.g. 

String queryString = "SELECT * FROM [cq:PageContent] AS s WHERE ISDESCENDANTNODE(s, '/content/pagefolder1') AND s.['cq:template']='<template-path>'";

 

you are trying setting the property to cq:Page, please update your query which will serach and set the property on jcr:content node.

node.setProperty ("NewProperty", true);

 
Note : Please check the syntax , the above query is just for illustration purpose.



Arun Patidar

View solution in original post

3 Replies

Avatar

Community Advisor

Check the Node Type - String, Boolean or else

 

If String - set in quotes.

 

JcrUtil.setProperty (node, "NewProperty", true); } }

 

javax.jcr.nodetype.ConstraintViolationException is thrown when an operation would violate a constraint on repository structure.

The node type definition doesn't allow a property with the name "NewProperty". You can check the node type definition in the CRXDE Lite.

 

The property "NewProperty" is expected to be of a different type. In your case, you're setting it to a boolean value (true). If the property is expected to be of a different type (like String, Date, etc.), it would throw this exception.

Avatar

Community Advisor

@HanL ,

 

Try to add the "NewProperty" = true to the result node coming from the query via CRXDe to make sure the node can accept this property.

Avatar

Correct answer by
Community Advisor

Hi @HanL 

Search jcrContent node and restrict serach for the template type
e.g. 

String queryString = "SELECT * FROM [cq:PageContent] AS s WHERE ISDESCENDANTNODE(s, '/content/pagefolder1') AND s.['cq:template']='<template-path>'";

 

you are trying setting the property to cq:Page, please update your query which will serach and set the property on jcr:content node.

node.setProperty ("NewProperty", true);

 
Note : Please check the syntax , the above query is just for illustration purpose.



Arun Patidar