Add a new property | Community
Skip to main content
Level 6
April 14, 2021
Solved

Add a new property

  • April 14, 2021
  • 2 replies
  • 1244 views

Hello All - I am looking for Curl command to add a property in all the pages under the specific folder in JCR. if the property exists, it should ignore it else it should add it.

 

Path:/content/test/en

Property: testprop = true

 

Also is there any utility provided by ACS to add a specific property without using CURL command?

 

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 BrianKasingli

@test1234567,

There is no AEM out of the box curl command that allows you to update a sling resource inside of the crx/de, however, you can create a Sling Servlet that can accept specific parameters, which allows you to manipulate and change properties using the Sling CRUD API.
Sling CRUD API - https://sling.apache.org/documentation/the-sling-engine/sling-api-crud-support.html

The servlet will accept parameters as "resourcePath", "prop", "val".
example: [POST] /bin/my-site/update-resource [REQUEST-BODY]resourcePath=/content/dam/my-site/folder;prop=testprop;value=true

Servlet doPost example:

 

@Override protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException { ResourceResolver resourceResolver = getResourceResolver(); String path = extractPostRequestBody("resourcePath"); String propName = extractPostRequestBody("prop"); String propVal = extractPostRequestBody("val"); Resource myResource = resourceResolver.getResource(path); ModifiableValueMap properties = myResource.adaptTo(ModifiableValueMap.class); properties.put(propName, propVal); resourceResolver.commit(); }

 

 

 

 

2 replies

Asutosh_Jena_
Community Advisor
Community Advisor
April 14, 2021

Hi @test1234567 

 

Unfortunately there is no such utility available that can update any additional property to all the pages under a specific folder.

How frequently you need this property to be updated?

  1. If this is going to be one time update, I will suggest to go with a servlet which will just take the source folder path as a parameter and iterate over all the pages available under the folder. If any page found, try to look for the property you are trying to add and if it does not exist then add it. If it exists, then skip it.
  2. If it's going to be done more frequently, then you can go by authoring by adding another field into the page property dialog and then using the inheritanceValueMap, in this way you can author only on the parent page and all the child page will inherit the same property. If you want to change in future you can change at one place, or if you want to override at a child page level also can be done.

Hope this helps!

Thanks 

BrianKasingli
Community Advisor and Adobe Champion
BrianKasingliCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
April 14, 2021

@test1234567,

There is no AEM out of the box curl command that allows you to update a sling resource inside of the crx/de, however, you can create a Sling Servlet that can accept specific parameters, which allows you to manipulate and change properties using the Sling CRUD API.
Sling CRUD API - https://sling.apache.org/documentation/the-sling-engine/sling-api-crud-support.html

The servlet will accept parameters as "resourcePath", "prop", "val".
example: [POST] /bin/my-site/update-resource [REQUEST-BODY]resourcePath=/content/dam/my-site/folder;prop=testprop;value=true

Servlet doPost example:

 

@Override protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException { ResourceResolver resourceResolver = getResourceResolver(); String path = extractPostRequestBody("resourcePath"); String propName = extractPostRequestBody("prop"); String propVal = extractPostRequestBody("val"); Resource myResource = resourceResolver.getResource(path); ModifiableValueMap properties = myResource.adaptTo(ModifiableValueMap.class); properties.put(propName, propVal); resourceResolver.commit(); }