0

My cq5 Content Structure is....

Content
  ---mywebsite
           ------base
                    -----us
                          --- en
                                ----pageOne
                                ----pageTwo
                                ----pageThree
                                ----pageFour
                                         ----cq:content
                                                  ----par
                                                        ----pageFourNew

"pageFourNew" has around 500 Properties. Now I need to get all the properties of "pageFourNew" and to update their value.

For example if I have:

property=prop1 
value = prop1 value

I want to do value = value+"some string value append" and save it on the repository.

I want to do this in a programmatically way.

Please share if you have any solution or idea.

Mauri Lopez
  • 2,864
  • 1
  • 17
  • 19
codePlayer
  • 15
  • 1
  • 3

2 Answers2

2

You can use PropertyIterator to iterate through all the properties, setProperty() method of node api to set the new value and jcr session to persist the value to get this done. Sample code:

PropertyIterator propertyIterator = pageFourNew.getProperties();
while (propertyIterator.hasNext()) {
    Property property = propertyIterator.nextProperty();
    pageFourNew.setProperty(property.getName(),
        property.getValue().getString() + "");
jcrSession.save();}
SubSul
  • 2,523
  • 1
  • 17
  • 27
1

You can easily do this as suggested above at JCR level. But as per CQ practices and this blog

It is better practice to operate at Sling level and NOT JCR level, just to avoid overhead of managing the resources. You can use below code which works:

        Resource resource = pageFourNew; // assuming you are getting sling resource properly
        ModifiableValueMap valueMap = resource.adaptTo(ModifiableValueMap.class);
        for(String key : valueMap.keySet()) {
            String value = valueMap.get(key, String.class);
            value = value + "additional texts";
            valueMap.put(key, value);
        }
        resource.getResourceResolver().commit();

This is cleaner approach.

Saravana Prakash
  • 1,321
  • 13
  • 25