possible to programmatically update component property using JS? | Community
Skip to main content
Level 3
May 8, 2023
Solved

possible to programmatically update component property using JS?

  • May 8, 2023
  • 4 replies
  • 1950 views
Is there a way to programmatically add/update any property and its value to a component, using Javascript in the page editor?
 
 
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,
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 AsifChowdhury

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

4 replies

AsifChowdhury
Community Advisor
AsifChowdhuryCommunity AdvisorAccepted solution
Community Advisor
May 8, 2023

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

manjunathdj
Level 1
May 8, 2023

@sean12341  

Check article here -https://www.linkedin.com/pulse/aem-automatically-set-properties-nestor-de-dios

 Hopes help you with updating textual content on text component.

 

Thanks,

-Manjunath DJ
rawvarun
Community Advisor
Community Advisor
May 8, 2023

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-java-api-best-practices.html?lang=en

 

Rohit_Utreja
Community Advisor
Community Advisor
May 9, 2023

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.