Expand my Community achievements bar.

SOLVED

Add a new property

Avatar

Level 4

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?

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@aemninja,

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

 

 

 

 

View solution in original post

2 Replies

Avatar

Community Advisor

Hi @aemninja 

 

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 

Avatar

Correct answer by
Community Advisor

@aemninja,

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