iteratoraemjcrjcr-sql2

How iterate all Property of a node and update the value of each property and save in repository in cq5


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.


Solution

  • 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();}