Expand my Community achievements bar.

SOLVED

possible to programmatically update component property using JS?

Avatar

Level 3
Is there a way to programmatically add/update any property and its value to a component, using Javascript in the page editor?
 
sean12341_0-1683524395451.png
 
I know that it probably involve a POST request with the property name and value to be added/updated, but I don't know which endpoint to use, or if there are any other ways.

Thank you,
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello @sean12341 ,

You can involve a POST request to a servlet and in that servlet, you can set the custom property to the node in this manner,

Session session = request.getResourceResolver().adaptTo(Session.class);
Node itemNode = JcrUtil.createPath("your-targeted-path", JcrConstants.NT_UNSTRUCTURED, session);
itemNode.setProperty("propertyName", "propertyValue");

 You can also achieve it using a Sling Model.

 

Thanks

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

Hello @sean12341 ,

You can involve a POST request to a servlet and in that servlet, you can set the custom property to the node in this manner,

Session session = request.getResourceResolver().adaptTo(Session.class);
Node itemNode = JcrUtil.createPath("your-targeted-path", JcrConstants.NT_UNSTRUCTURED, session);
itemNode.setProperty("propertyName", "propertyValue");

 You can also achieve it using a Sling Model.

 

Thanks

Avatar

Community Advisor

Yes, you can use JavaScript Use-API, but it isn't easy to debug code, and it's server-side script.

Instead, use Java API for add/update property in AEM.

https://experienceleague.adobe.com/docs/experience-manager-learn/foundation/development/understand-j...

 

Avatar

Community Advisor

Hi @sean12341 ,

 

Could you please share the use case?

 

It is possible to some extend but it is rather a complicated process. 

Other option would be add all sort of attributes in the dialog, hide them initially and make it available for the component using js based on the use case.