How to add cq:styleids manually and via code in Java | Community
Skip to main content
Level 6
October 26, 2021
Solved

How to add cq:styleids manually and via code in Java

  • October 26, 2021
  • 1 reply
  • 828 views

I am trying to add these things manually and via code both way I can't do it 

Can you please help with code and manual progress both

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 SantoshSai

Hi @ronnie09 

 

1. JCR API
You may  adapt the resource to Node and use the JCR API to change property. However, it's a good idea to stick to one abstraction layer and in this case we somehow break the Resource abstraction provided by Sling.

Node node = resource.adaptTo(Node.class);
node.setProperty("property", "value");
node.getSession().save();


2. Read Value through ValueMap

You can also use ValueMap and ModifiableValueMap APIs to read and update property respectively.

ValueMap valueMap = resource.getValueMap();
valueMap.get("yourProperty", String.class);


Write/Modify property through ModifiableValueMap

ModifiableValueMap modifiableValueMap = resource.adaptTo(ModifiableValueMap.class);
modifiableValueMap.put("NewProperty", "Your Value");  //write
modifiableValueMap.put("OldProperty", "Updated Value"); // Modify

After writing property to a node, save these values by committing the 'resourceResolver'
You'll need system/service user for admin resourceResolver.

Regards,

Santosh

1 reply

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
October 26, 2021

Hi @ronnie09 

 

1. JCR API
You may  adapt the resource to Node and use the JCR API to change property. However, it's a good idea to stick to one abstraction layer and in this case we somehow break the Resource abstraction provided by Sling.

Node node = resource.adaptTo(Node.class);
node.setProperty("property", "value");
node.getSession().save();


2. Read Value through ValueMap

You can also use ValueMap and ModifiableValueMap APIs to read and update property respectively.

ValueMap valueMap = resource.getValueMap();
valueMap.get("yourProperty", String.class);


Write/Modify property through ModifiableValueMap

ModifiableValueMap modifiableValueMap = resource.adaptTo(ModifiableValueMap.class);
modifiableValueMap.put("NewProperty", "Your Value");  //write
modifiableValueMap.put("OldProperty", "Updated Value"); // Modify

After writing property to a node, save these values by committing the 'resourceResolver'
You'll need system/service user for admin resourceResolver.

Regards,

Santosh

Santosh Sai