Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Access project data via sling servlet.

usmankhalidbutt
Level 2
Level 2

Hi,

I'm trying to access project data in AEM via a sling servlet. The servlet is working and I'm able to make an ajax POST call to retrieve a simple JSON object on the client side. As the next step, I'd like to be able to iterate over projects under /content/projects/<foldername> using the project API. So far, I'm just trying to get the titles for all projects as a String, but it doesn't seem to be working. If I remove the project iterator in the getProjectList() method, it returns the JSON object correctly, otherwise, there's an empty response. I tried passing a filter into the projectManager.getProjects() as well, that didn't seem to help either.

I am a Java newbie, so any pointers would be helpful.

This is what I have so far in my servlet:

package ca.rogers.core.servlets;

import org.apache.felix.scr.annotations.Reference;

import org.apache.felix.scr.annotations.sling.SlingServlet;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

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

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

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

import org.apache.sling.commons.json.JSONException;

import org.apache.sling.commons.json.JSONObject;

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

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

import javax.servlet.Servlet;

import javax.jcr.Repository;

import javax.servlet.ServletException;

import java.io.IOException;

import org.osgi.framework.Constants;

import org.apache.sling.jcr.api.SlingRepository;

import java.rmi.ServerException;

import java.util.UUID;

import com.adobe.cq.projects.api.Project;

import com.adobe.cq.projects.api.ProjectFilter;

import com.adobe.cq.projects.api.ProjectManager;

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

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

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

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

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import java.util.Iterator;

@Component(service=Servlet.class,

property={

        Constants.SERVICE_DESCRIPTION + "=Project Servlet",

        "sling.servlet.methods=" + HttpConstants.METHOD_POST,

        "sling.servlet.paths="+ "/bin/cceprojectservlet",

        "sling.servlet.metatype=" + true,

})

public class ProjectServlet extends SlingAllMethodsServlet {

     

     @Reference

     private SlingRepository repository;

     

     public void bindRepository(SlingRepository repository) {

            this.repository = repository;

            }

   

    @Reference

    private ProjectManager projectManager;

          

     @Override

     protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {

      try{

         

          String PROJECT_REQUEST_PATH = request.getParameter("rootFolder");

          String projectList = getProjectList();         

         

          JSONObject obj = new JSONObject();

         

          obj.put("rootFolder", PROJECT_REQUEST_PATH);

          obj.put("projectsList", projectList);

         

          String jsonData = obj.toString();

         

          response.getWriter().write(jsonData);

      }

      catch(Exception e){

          e.printStackTrace();

      }

    }

   

    private String getProjectList(){

        Iterator<Project> projects = projectManager.getProjects(null, 0, 10);

        String projectsList = "";

        while(projects.hasNext()){

            Project px = projects.next();

            projectsList += px.getTitle();

        }

        return projectsList;

    }

}

1 Accepted Solution
neeleshkumar
Correct answer by
Employee
Employee

usmankhalidbutt

Every project has a jcr:content and contains all details like created date, created by user, project path, experience fragments associated with project etc

Resource parent = resourceResolver.getResource(rootPath);

Iterator<Resource> children = parent.listChildren();
 
while (children.hasNext()) {
 
Resource child = children.next();

  Resource jcrResource=child.getChild("jcr:content");

  ValueMap vm = jcrResource.getValueMap();

//Use valuemap object to pass propertyname(go through jcr:content of that project ) and get value for each property

  String title = vm.get("jcr:title","defaultValue");

//Please use the same way to get other property values

}

Please let me know ,if this helps.

View solution in original post

0 Replies
neeleshkumar
Employee
Employee

Hi Usman,

Try this code

List<String> projectsList=new ArrayList<String> ();

ProjectManager projectManager = resourceResolver.adaptTo(ProjectManager.class);

ProjectFilter filters = new ProjectFilter();

List<String> templates = new ArrayList<String>();

templates.add("/libs/cq/core/content/projects/templates/default");//Add templates  of your projects

filters.setProjectTemplates(templates);

Iterator<Project> projects = pm.getProjects(filters, 0, 10);

while (projects.hasNext()) {

Project project = projects.next();

projectsList.add(project.getTitle());

}

Add projectsList to your json response.

Let me know, if it works.

Regards,

Neelesh

usmankhalidbutt
Level 2
Level 2

Thanks neeleshkumar

Any ideas on how I could get the remaining properties (created date, created by user, project path, experience fragments associated with project etc)?

neeleshkumar
Correct answer by
Employee
Employee

usmankhalidbutt

Every project has a jcr:content and contains all details like created date, created by user, project path, experience fragments associated with project etc

Resource parent = resourceResolver.getResource(rootPath);

Iterator<Resource> children = parent.listChildren();
 
while (children.hasNext()) {
 
Resource child = children.next();

  Resource jcrResource=child.getChild("jcr:content");

  ValueMap vm = jcrResource.getValueMap();

//Use valuemap object to pass propertyname(go through jcr:content of that project ) and get value for each property

  String title = vm.get("jcr:title","defaultValue");

//Please use the same way to get other property values

}

Please let me know ,if this helps.

View solution in original post

neeleshkumar
Employee
Employee

usmankhalidbutt

rootPath is your project folder path i.e "rootFolder".