Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Sling Model - Setting Node Property @PostConstruct


Level 3


AEM: 6.2

I have a use case where on inject properties of a component, my sling model executes a @PostConstruct method to configure a state of content by setting a node property value. For example:

    protected void setProperty() {
            try {
            ModifiableValueMap map = this.resource.adaptTo(ModifiableValueMap.class);
(*)         map.put("customProperty", "initalStateValue");      
        } catch (PersistenceException e) {

Now, when i publish a page which uses this AEM component, the anonymous user on the published site gets an error message:

Post-construct method has thrown an exception for model class 

On further analysis: I have found that since "anonymous" user does not have permissions to write on /content nodes, the statement marked above with an (*) sets map object to null.

My Question is: Is there a better implementation pattern to achieve the scenario of configuring the state of content node such that the state of the content can be set through node property only for authors?

1 Reply


Level 10

Hi , 

This forum is dedicated for queries related to Adobe DTM. Hence we re-posting your query to AEM Forum

Thanks & Regards

Parit Mittal


Community Advisor

Dear Varun,

Usually, when you do content authoring in AEM, you do set all of the properties such as your "customProperty" via the dialog's[0]. Which automatically send your data to SlingPostServlet[1] that handles saving data to the database.

You normally, should not write data when you generate SlingModel.

In case if you do need to do it, due to your unique and awesome! requirements.

Kindly refer to the following guide[2] on how to allow 'anonymous' to write into your database.



[2] (look at accepted answer)


Kindest Regards,



Level 3

The "customProperty" is not part of the dialog and hence not added via authoring interface.

Question: "You normally, should not write data when you generate SlingModel."

What if I require to set/unset a property (such as "customProperty") whenever the page is loaded? In such circumstance, without apply business logic of updating properties via SlingModel, is there any other scheme or design I could follow?


Community Advisor

Dear Varun,

Correct, normally as per SlingModel design goals:

  • Entirely annotation driven. "Pure" POJOs.
  • Use standard annotations where possible.
  • Pluggable
  • OOTB, support resource properties (via ValueMap), SlingBindings, OSGi services, request attributes
  • Adapt multiple objects - minimal required Resource and SlingHttpServletRequest
  • Client doesn't know/care that these objects are different than any other adapter factory
  • Support both classes and interfaces.
  • Work with existing Sling infrastructure (i.e. not require changes to other bundles).

However, it fit's well with your 'customProperty' approach then go for it.

You can add an if check to make sure that you are adding custom property only for Author and get run mode(whether it's Publisher or Author) via the SlingSettingsService[0].