Using JSON to Populate CQ5 CMS Dialogs

| 11 Comments | 1 TrackBack

Although Adobe's CQ5 CMS has many quirks, it can be a powerful tool once you discover the how to achieve certain tasks. For example, one of my tasks recently was to obtain a list of users in a particular group in CQ5 and to display all users in a drop-down menu in a dialog. The Content Editor could then select a user, and the user's name and other details about the user would be displayed on the web page. (This was built to associate a particular user, that may or may not be the Content Editor, with specific content.) The component created would display automatically within the page template, and the Content Editor would simply be able to edit the component directly.

This article explains how the task can be achieved. This article also assumes that you have created a group and added users to the group.


Create the Component and Include it in the Template

1. Create a folder (type cq:Component) for the component, and name it mygroup-users.

2. In your template, include the new component directly, as below:
<cq:include path="users" resourceType="[my project]/components/mygroup-users"/>


Create the Dialog

3. In the mygroup-users component folder, add a new dialog (cq:Dialog) named 'dialog'. (Add a 'title' property for the dialog and an xtype of 'panel'.)

4. Underneath your dialog, add a new node - cq:WidgetCollection. Name it "items".

5. Underneath the 'items' node, add a cq:Widget, and name it 'users'. Add new properties, described in the list below:

  • fieldLabel = Select User: (String)
  • name = ./users (String)
  • xtype = selection (String)
  • type = select (String)
6. Add one last new property to the 'users' node. This will allow the dialog to look up the script that processes the data. The property is known as optionsProvider (String). The path is to the JSON code, which will be created in the next step, with a cachebreaker.
function(path, record){
return CQ.Util.formatData(
CQ.HTTP.eval(CQ.HTTP.noCaching(
 '/apps/[path to component]/users.list.json?' 
+ (new Date().getTime()))));
}

Create your JSON, JSP and a Reference

7. Now, you will create a JSP file (nt:file) to include the Java code that you will write to obtain the data. The file will be placed in the root of your mygroups-users component. (It should be named mygroup-users.jsp.) Here, you will write the code to output the data onto the web page. (I'll let you sort this yourseldf, but I used the UserManager class.)

8. Now, you will create the JSON code. This file (nt:file) should be named list.json.jsp, and it will be placed in the root of the component (mygroup-users). The naming is important to specify the component's default in CQ5; we're extending this list component. The code simply is a loop to list all users to print them out.

  • Initialise the content type: response.setContentType("application/json");
  • resolve the UserManager: final UserManager um = resourceResolver.adaptTo(UserManager.class);
  • Get the Groups: Iterator<Group> groups = um.getGroups();
  • You'll need a framework, as below:
%>[<%
String delim = "";
while (groups.hasNext()) {
   // get the name of the group, and extract all the members from that group.
  // the member is an Authorizable class.
%><%= delim %><%
%>{<%
%>"value":"<%=  member.getProfile().getAuthorizable().getID() %>",<%
%>"text":"<%= member.getProfile().getName() %>",<%
%>"qtip":""<%
%>}<%
if ("".equals(delim)) {
}
    delim = ",";
        }
    }
}
%>]

9. Now, you'll need a reference (sling:Folder) to your users. Underneath the mygroups-users component, create a new sling:Folder, and give it the name 'users'. Add a new property called sling:resourceType, and it should be the path to your component: [myproject]/components/mygroups-user


Test

10. Test your changes.

The JSON should be output to http://localhost:4502/apps/[path to component]/users.list.json.

The JSON will be returned in the browser, if working correctly. It will look like below:

[

  • -
    {
    • value: "john_doe"
    • text: "John Doe"
    • qtip: ""
    }
  • -
    {
    • value: "joebloggs"
    • text: "Joe Bloggs"
    • qtip: ""
    }
]


I hope that you have fun building your own custom components based on the JCR.

1 TrackBack

TrackBack URL: http://jenikya.com/cgi-bin/mt5/mt-tb.cgi/450

CQ5 Designs from Confluence: Web CMS on April 22, 2013 9:43 PM

CQ5 Designs Draft CQ5 supports defining and enforcing a consistent look and feel across a web site \ referred to as the site's design https://dev.day.com/docs/en/cq/current/developing/designer.html.... Read More

11 Comments

Hey could I use some of the content here in this entry if I provide a link back to your site?

Great post.

I've got a question though. When invoking http://localhost:4502/apps/[path to component]/users.list.json on my browser, I get the following exception:

Invalid recursion selector value 'list' (400)

The requested URL /apps/[path to component]/users.list.json resulted in an error in org.apache.sling.servlets.get.DefaultGetServlet.

If I'm not wrong, this is caused by the Sling default servlet for JSON. How to overcome this?

Hi jenn,

Thank you for such a good and gr8 explanation. I was looking for similar functionality.

Can you please attach a code package of this. This will be of grate help for me.

Thank you

Jun, check this http://dev.day.com/content/kb/home/cq5/Hotfixes/54/Hotfix_36357.html

Hello,

Can you please provide a copy of your package.

Thanks, Sam.

Hello Jenn,

Nice article for rookies like me. I have similar implementation. I have done exactly the same. Is the json file name in the component should be users.list.json.jso or list.json.jsp? I had tried both but came up with 404 error.

Thanks, Raj

Thank you! I was having JSON object caching issues in IE and the CQ.HTTP.noCaching tip really helped!

Hi, Thanks for above explanation. The above code to loop all the users that has been given is to be written in json.jsp or the jsp of the component?

Leave a comment

Archives

Recent Comments

  • jenn: jIt will be json.jsp. The tutorial was an older version read more
  • joseph: Hi, Thanks for above explanation. The above code to loop read more
  • Sara: Thank you! I was having JSON object caching issues in read more
  • Raj: Hello Jenn, Nice article for rookies like me. I have read more
  • Sam: Hello, Can you please provide a copy of your package. read more
  • jenn: I didn't have the issue, Jun. It worked without any read more
  • Chetanya: Jun, check this http://dev.day.com/content/kb/home/cq5/Hotfixes/54/Hotfix_36357.html read more
  • Rajesh: Hi jenn, Thank you for such a good and gr8 read more
  • Jun: Great post. I've got a question though. When invoking http://localhost:4502/apps/[path read more
  • jenn: Yes, if it is credited. Thanks. read more
OpenID accepted here Learn more about OpenID