Dynamic Value in DropDown in Page Properties With Json URL | Community
Skip to main content
ashikg3
Level 2
February 21, 2024

Dynamic Value in DropDown in Page Properties With Json URL

  • February 21, 2024
  • 2 replies
  • 1727 views

I have Create a servlet which is returning JSON data like this:

 

[
{
"jcr:primaryType":"nt:unstructured",
"value":"option1",
"text":"option1"
},
{
"jcr:primaryType":"nt:unstructured",
"value":"option12",
"text":"option12"
},
{
"jcr:primaryType":"nt:unstructured",
"value":"option123",
"text":"option123"
}
]




I want to use Dropdown in page properties which will dynamically populate itself with data from JSON servlet

 

endpoint of JSON: /app/authordata.json

 

in .cq_dialog.xml

                                                     <%authorName

                                                        cq:showOnCreate="{Boolean}true"

                                                        jcr:primaryType="nt:unstructured"

                                                        sling:resourceType="granite/ui/components/coral/foundation/form/select"

                                                        fieldDescription="Get Author Name"

                                                        fieldLabel="Author Name"

                                                        name="./authorName">

                                                        <datasource

                                                            jcr:primaryType="nt:unstructured"

                                                            sling:resourceType="x/datasources/dropdown"

                                                            options="/app/authordata.json"

                                                            repoSource="{Boolean}true"/>

                                                    <%authorName>
 

But the data return empty or null

 

here is my Servlet

 

package com.x.core.servlets;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.models.factory.ModelFactory;

import org.apache.sling.api.servlets.HttpConstants;

import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

import org.apache.sling.api.servlets.ServletResolverConstants;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Reference;

import org.osgi.framework.Constants;

import javax.servlet.Servlet;

import javax.servlet.ServletException;

import java.io.IOException;

import java.util.List;

import com.x.core.models.AuthorsListPass;

import com.google.gson.JsonArray;

import com.google.gson.JsonObject;

@Component(service = Servlet.class,

           property = {

               Constants.SERVICE_DESCRIPTION + "=Author Names DataSource Servlet",

               ServletResolverConstants.SLING_SERVLET_PATHS + "=/apps/authordata.json",

               ServletResolverConstants.SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_GET

           })

public class AuthorListDataSourceServlet extends SlingSafeMethodsServlet {

    @Reference

    private ModelFactory modelFactory;

    @Override

    protected void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws ServletException, IOException {

        response.setContentType("application/json");

        // Use the request's resource to adapt to the AuthorsListPass model

        AuthorsListPass authorsListPass = request.getResource().adaptTo(AuthorsListPass.class);

        if (authorsListPass == null) {

            response.getWriter().write("['Ash Didn't Find Anything']");

            return;

        }

        List<String> authorNames = authorsListPass.getAuthorNames();

        JsonArray options = new JsonArray();

        for (String authorName : authorNames) {

            JsonObject option = new JsonObject();

            option.addProperty("value", authorName);

            option.addProperty("text", authorName);

            options.add(option);

        }

        response.getWriter().write(options.toString());

    }

}

 

can anyone suggest to me how I can achieve that? Need a Super Urgent Solution.
Adobe Experience Manager, Version 2023.1.1091

 

Thanks

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

ashikg3
ashikg3Author
Level 2
February 21, 2024

Brother. As I'm having the data in JSON on Servlet. I need to proceed with that. and the Main thing is I need to show the dropdown data in Page properties, Some reason I don't know Javascript don't work on page properties page. 

 

If you can recommend me other solution that will be great

Imran Khan
Community Advisor
Community Advisor
February 21, 2024

@ashikg3 wrote:

Brother. As I'm having the data in JSON on Servlet. I need to proceed with that. and the Main thing is I need to show the dropdown data in Page properties, Some reason I don't know Javascript don't work on page properties page. 

 

If you can recommend me other solution that will be great



Your JSON is fix, the only way to follow below link for JS. Please try to debug in browser.

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem-6-4-how-to-read-and-set-the-select-dropdown-value-in/m-p/323984

arunpatidar
Community Advisor
Community Advisor
February 21, 2024
ashikg3
ashikg3Author
Level 2
February 21, 2024

Tried. Data return empty

 

arunpatidar
Community Advisor
Community Advisor
February 22, 2024

Hi @ashikg3 
You need to modify your json , similar to sample json and use servlet flag on

Sample Json : https://github.com/arunpatidar02/aem63app-repo/blob/master/java/dropdown/sample.json

Servlet : https://github.com/arunpatidar02/aem63app-repo/blob/master/java/dropdown/PopulateTouchUIDropdownFromJsonResource.java 

 

 

Servlet that use to populate Touch UI dropdown options dynamically from json file or servlet
*
* datasource node properties
* - options (String) - path of json file or Servlet, which returns json examples :
/apps/commons/utils/json/dropdown/color.json
/bin/utils/json/dropdown/style
* - sling:resourceType (String)
utils/granite/components/select/datasource/json
* - repoSource (Boolean) - optional property to specify file in repository or servlet json source
* true(Default) : to read JSON file stored in repository
* false : to get JSON from Servlet


 

Arun Patidar