May 5, 2020
Estimated Post Reading Time ~

Creating AEM JCR valid & Unique name programmatically

The objective of this post is to elaborate on how AEM Page Manager API is being used to create a unique JCR Node or Page name. And if there is a requirement for you to create the same page name programatically then what API you should be using?

Scenario
Let’s consider a scenario where article pages are being created automatically in AEM content hierarchy and some of the promotional content under those pages. And, there is a product detail page which renders promotional content based Product data.

The problem occurs when you to have determine promotional content programatically with some information at the product level. To match appropriate promotional content & render onto product detail pages isn’t straight forward.

Solution
In order to solve above problem, all you have to do is to resolve promotional content page as resource. And that is possible if Your code is able to find content page in JCR Contrent hierarchy.

Let’s consider this example. Title of promotional/Article content: “This is a dummy content article page”. The Same page created in AEM would have hierarchy like this.

/content/<your-website>/en/articles/this-is-a-dummy-content-article-page

Below example shows how you can create valid JCR Resource/Node/Page Name out of given Title of the article.

#Simple Example Class
Public class UninqueValidPageName{
 public static String getValidPageName(final String articleTitle){
     return JcrUtil.createValidName(StringUtils.trim(articleTitle), JcrUtil.HYPHEN_LABEL_CHAR_MAPPING);
}
}

Final Thoughts
This solution seems very simple however it has significant important of solving problem.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.