How to add a property to existing AEM node | Community
Skip to main content
Level 2
February 6, 2024
Solved

How to add a property to existing AEM node

  • February 6, 2024
  • 3 replies
  • 1745 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

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.

3 replies

SureshDhulipudi
Community Advisor
Community Advisor
February 6, 2024

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.

Sudheer_Sundalam
Community Advisor
Community Advisor
February 6, 2024

@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.

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
February 6, 2024

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