Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How to modify the Page property in the code and set it back

Avatar

Level 2

How to modify the Page property in the Page component jsp  and set the new modified value back to the Page property  by setProperty(..,...) method? Can we  set the Page property back?

 

My Situation is I want to set the Unique Page ID for every page I create, So I would like this to be a Page Property.

I am considering the the current datetime  is unique feature, So I want to convert this current datetime to unix time value a long value and want to set  this PageID property   to that page.

I can access the Page properties in the Page component jsp file, But when I try to set the Page property it wont throw error nor it sets my new modified value.

 

My Code in Component JSP is as below:

SlingRepository repository = resourceResolver.adaptTo(SlingRepository.class);
                Session session = repository.loginAdministrative(null);
                Node node= session.getNode("Absolute path of the Page");                
 
                node.setProperty("jcr:Pageid",Unix Datetime );  
                session.save();

Any help to create a Unique Page ID will be really appreciated

 

Thanks a ton in Advance

1 Accepted Solution

Avatar

Correct answer by
Level 10

What s your exact use case?    Page property stored at  "Absolute path of the Page"+/jcr:content  so modfying it should fix the issue.  You can use [1] & very bad idea to use admin session. Also logic should be part of template not at component level.

[1]

  1. Path itself is unique
  2. Node.getIdentifier
  3. may be Long.toString(Calendar.getInstance().getTimeInMillis())

View solution in original post

4 Replies

Avatar

Level 10

You cannot call repository.loginAdministrator from a component to modify the JCR. Only fron an external Java app. You need to call adaptto method. See http://helpx.adobe.com/experience-manager/using/querying-experience-manager-data-using1.html.

Avatar

Correct answer by
Level 10

What s your exact use case?    Page property stored at  "Absolute path of the Page"+/jcr:content  so modfying it should fix the issue.  You can use [1] & very bad idea to use admin session. Also logic should be part of template not at component level.

[1]

  1. Path itself is unique
  2. Node.getIdentifier
  3. may be Long.toString(Calendar.getInstance().getTimeInMillis())

Avatar

Level 2

Thanks for your quick response.

My exact scenario is I need to create a unique page id for all my pages created using my page template.

this page id property should get generated  automatically on page creation.

I want to access this pageid  based on the google analytics result and store those Page id's against the loggedin user id.

When the user login again after 24 hours, I will query the JCR  with all the PageId's stored for this user and list out all those pages for the user.

For this process first step i need to have a Unique PageID generated for each page on page creation.

Any help in this regard is really appreciated.

Thanks

Avatar

Level 2

Hey Sham,

Thanks a lot . I get the issue fixed.

I used Page Path as the unique ID for every page .