Posts

Dynamic Options values in AEM dialog dropdown Using $PATH

In this post, I will explain a real project use-case, where I need to populate a Dropdown in my component dialog with dynamic values, but the condition are-

1. These values can only be fetched from the current resource or current page object.
2. Another condition is that this dropdown must be auto-populated when the dialog opened.

For example,  The drop down values can be the list of all parent pages of the page where this component was dropped.

Q1). How will you go for it?
You will think about ajax call because the dropdown can be filled with dynamic values using options property.

Q2). Yes, you are right then, what will you call from the options property?
Sling Servlet

If your answer is Sling Servlet then, I have some questions for you-

Q1). How will you get the current resource  or current page object in that servlet?
Q2). If you  are trying to pass the path of current resource then How will you do that?

OK, I think you are well understood what am I trying to say. How to get current resource or current page object into the called Sling Servlet.

Maybe you can do it using some other approach but in this post I will explain, How I complete this task?

In place of servlet I have created a JSP file under same component node with some selector (For ex. options.json). For Ex. My component structure is-
Component structure

Now, I have set some properties on my drop-down dialog node. These properties are shown in figure-

3

Here you will notice that, I have used $PATH, this will return the path of the component wherever it is dropped. Then I used Sling Script Resolution Principle to call my options.json.jsp file.

For writing this blog, I had created dummy data but in actual, I had created a Sling Service and from one of it’s methods, I was getting the JSON values.

Here, you should follow standard coding practices and should remove these scriptlets from JSP file to some java file. But the key point is that this JSP must return the values in JSON format. If you are using Sightly then same code will work or if you want to use HTML in place of JSP then it will work perfectly fine.

I am also sharing the Git repository link where you can find demo examples for these properties.
Git repository link is –

https://bitbucket.org/accunitysoft/accunity-blog-snippets

Happy Coding..!!

Ankur Chauhan
Tech Lead

 

ResourceResolver Object in AEM6.1/6.0 Sling Services

As we know that getAdministrativeResourceResolver() method has been deprecated from ResourceResolverFactory interface in AEM6 and above versions. Then the question is-

Q1). How to get ResourceResolver object in Sling services in AEM6.0 and above versions?
If you are working with AEM6 or AEM6.1 then you have two options-

  1. If you know the credentials of an User and want that user credential in your service then go with getResourceResolver() method.
  2. If you don’t have user credentials and want to access ResourceResolver object then you have to use
    getServiceResourceResolver() method. as shown below –

    Note : – In this case you have to add a configuration in your Felix Console to your AEM instance.

Q2). What configuration I need to do for AEM6.0?
For AEM6.0,  configuration steps are –

  1. Go to Felix Console configuration tab i.e. http://localhost:4502/system/console/configMgr
  2. Search for User Mapping as shown in figure-
    User Mapping Search
  3. Click on this service to edit it’s configuration.
  4. Here you have to add one entry and the syntax of the entry is –
    “Bundle symbolic Name”:”SubServieName”=”User Name”

    1. “Bundle symbolic Name :-  Here you have to add the bundle symbolic name where you are creating service.
    2. “SubServieName”:- This is the name you provided as a value of  ResourceResolverFactory.SUBSERVICE property i.e. “testService”.
    3. “User Name” :- This  is the user name for ex. “admin”So in my case this configuration field value becomes “com.blog.blog-bundles:testService=admin”
    After adding this entry your configuration looks like-
    second
  5. Now everything is set for AEM6.0.

Q3). will these configurations work for AEM6.1?
No

Q4). What configuration, do I need for AEM6.1?
If you are working with AEM6.1 that you have to complete all steps explained for AEM6.0. To make these configurations  working for AEM6.1, you have to ensure that the “jcr:PrimaryType” of your user is “rep:SystemUser” i.e if you are trying to use “admin” user. Then it will not work as it’s “jcr:PrimaryType” value is  “rep:User”.

Q5). What do you mean by “jcr:PrimaryType” as “rep:SystemUser”?
It means that user is just not a repository user. It must be system user.

Q6). How to create System User in AEM6.0?
For creating System User, follow these steps-

  1. Go to CRX Explorer http://localhost:6502/crx/explorer/index.jsp
  2. Login as Administrator i.e. using “admin:admin”
  3. Click on “User Administration”, you will see a screen just like this –
    4
  4. Here you will see a option of “Create System User”, Click on this button.
  5. Add a value for User Id field for ex. “testUser”
  6. Click on green button.
  7. Close the window.
  8. Go to CRXDE Lite, your “testUser” will be created under /home/system directory. If you are not able to find then search for “testUser” in home screen. You will get the location.

Q7). How to use this user?
In your “Apache Sling Service User Mapper Service” configuration change your entry from –
com.blog.blog-bundles:testService=admin to com.blog.blog-bundles:testService=testUser

Q8). Should I do this configuration at “Apache Sling Service User Mapper Service” configuration or should I create a “Apache Sling Service User Mapper Service Amendment” service configuration?
You can do it by both ways. But as “Apache Sling Service User Mapper Service” is a service factory in AEM6.1 so as best practices you should create another service configuration by clicking on plus button at the front of “Apache Sling Service User Mapper Service Amendment”.
When you click on “+” button, one new configuration will be created at the end. Click on that service and do this configuration there. Your code will work in same manner as working before.

Happy Coding..!!

Ankur Chauhan
Tech Lead.