Expand my Community achievements bar.

How to get AEM data using AEM project.

Avatar

Level 8

Hi 

I am working on getting aem data. For that I have setup AEM project using maven but I am not getting how I can use resourcereolver instance to get the aem data. 

Can you please help in which approach should I go .

I am stuck at this point.

11 Replies

Avatar

Community Advisor

Are you referring to get content pages as AEM data ?

Kindly elaborate the use case . 

Himanshu Jain

Avatar

Level 8

Hi @Himanshu_Jain 

Basically I want to crawl all content from aem like assets, sites, pages, forms, users/groups data.

I have tried using rest api but there is not api for getting users and groups data so I am trying using sdk api provided by aem.

To use this api we compulsarily require osgi multimodule project so I have created one AEM project using maven but got stuck at how to go forward.

I am completely new to AEM. 

 

Avatar

Community Advisor

@akshaybhujbale,
Are you creating maven aem project, which will be deployed to aem
Or you are writing standalone java project outside aem and trying to access aem repo.
If you have aem project, It is straight forward. I can give you some example code. But if you are having standard java project and making rest call to aem, then we have to look how you can get required information.

Avatar

Level 8

Hi @sunil_kumar_ 

I am looking either way.

But with aem project how to do I am not sure.

I have created aem project using maven but then after I stuck.

what will be my approach to crawl aem data.

If you have any sample code please share.

 

Avatar

Community Advisor

@akshaybhujbaleWriting in aem maven project is simple. There tons of sample code available here and over internet. I can try to share some code examples sometime

Avatar

Community Advisor

Hi @akshaybhujbale 

In one of my past project I'm fetching JCR data in multiple ways and returning in JSON format - please refer to my github code here: https://github.com/sansai2011/demo/blob/master/demo.demo/core/src/main/java/demo/core/servlets/JCRIt...

Hope that helps you!

Regards,

Santosh

Avatar

Level 8

HI @SantoshSai 

I have refered this tutorial you shared by doing so I am getting below error.

akshaybhujbale_0-1655183473183.png

 

Please help me to let know where I am getting wrong.

Thanks

Avatar

Community Advisor

@akshaybhujbale 

Why are you writing main method here? We don't write main method. What are you trying to achieve here?

Avatar

Community Advisor

@akshaybhujbale Can you please refer my code as I shared above? https://github.com/sansai2011/demo/blob/master/demo.demo/core/src/main/java/demo/core/servlets/JCRIt...

Also please check if you have created Service User as it is mentioned in article.

Or you can clone my project and deploy on your AEM instance, then visit http://localhost:4502/editor.html/content/we-retail/us/en/men.txt servlet which should return JCR data in JSON. (make sure you create service user as "writeService", as mentioned in code)

Avatar

Level 1

Hi @SantoshSai

The github link is showing error "404: This is not the web page you are looking for". Can you check if requires providing access to the repository. In this case, can you please add my id(poojajaiswal96@deloitte.com) to access it as I also need to fetch all data from AEM in my standalone maven project.
Thanks!

Avatar

Community Advisor

Hi @akshaybhujbale , As you did not mention, what are you trying to achieve using this data. If you are trying to achieve search, Probabilty you should go for third party search like solar search. But for sake of answer. 
There are two ways you can do it. 

1. Iterate pages/assets/users and prepared result. 

2. Use query either query builder to SQL2. Write service to execute query in service code and get result. 


I am sharing example of both.  But first you have to get Resource resolver as below. 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-initialize-resource...

1. Giving some example code for iteration. Make sure your Resource Resolver object has proper permission to access required data/content/pages/users
Getting pages

 Page page = resourceResolver.adaptTo(PageManager.class).getPage("/content");
            Iterator<Page> childPages = page.listChildren(null,true);
            while (childPages.hasNext()) {
                Page childPage = childPages.next();
           }

Getting User and Groups. Printing in logs. You can use as per your need.

            ResourceResolver resourceResolver = ResolverUtil.newResolver(resourceResolverFactory);
            Session session = resourceResolver.adaptTo(Session.class);
            UserManager userManager = ((JackrabbitSession) session).getUserManager();
            Iterator<Authorizable> userIterator = userManager.findAuthorizables("jcr:primaryType", "rep:User");
            LOG.info("\n ----------GETTING USERS-------------");
            while (userIterator.hasNext()) {
                Authorizable user = userIterator.next();
                    LOG.info("\n User : {}", user.getPath());
            }
            Iterator<Authorizable> systemUserIterator = userManager.findAuthorizables("jcr:primaryType", "rep:SystemUser");
            LOG.info("\n ----------GETTING System USERS-------------");
            while (systemUserIterator.hasNext()) {
                Authorizable serviceUser = systemUserIterator.next();
                LOG.info("\n Service User : {}", serviceUser.getPath());
            }

            Iterator<Authorizable> groupIterator = userManager.findAuthorizables("jcr:primaryType", "rep:Group");
            LOG.info("\n ----------GETTING Groups-------------");
            while (groupIterator.hasNext()) {
                Authorizable group = groupIterator.next();
                LOG.info("\n Group : {}", group.getPath());
            }

2. Sharing some sample queries and code implementations. 
Query Builder query to get page and assets. I am sharing simplest one. create as per your need. 

/* ---To get Assets----*/
path=/content/dam
type=dam:Asset
p.limit=-1

/* ---To get Pages----*/
/* ---Adjust type as per your content----*/
path=/content
type=cq:PageContent
p.limit=-1

How to implement in backend 

@Reference
QueryBuilder queryBuilder;
       
 Map<String,String> queryMap=new HashMap<>();
   queryMap.put("path","/content/dam/we-retail");
   queryMap.put("type","dam:Asset");
   queryMap.put("p.limit",Long.toString(-1));
   final Session session = resourceResolver.adaptTo(Session.class);
   Query query = queryBuilder.createQuery(PredicateGroup.create(queryMap), session);
    SearchResult result = query.getResult();
    int perPageResults = result.getHits().size();
    long totalResults = result.getTotalMatches();
     List<Hit> hits =result.getHits();
        for(Hit hit: hits){
            Asset asset=hit.getResource().adaptTo(Asset.class);
           LOG.info("\n Page {} ",asset.getPath());
        }

In Case you use SQL 2

            String searchPath="/content/we-retail";
            String sql2Query = "SELECT * FROM [cq:PageContent] AS node WHERE ISDESCENDANTNODE ("+searchPath+") ORDER BY node.[jcr:title]";
            ResourceResolver resourceResolver = ResolverUtil.newResolver(resourceResolverFactory);
            final Session session = resourceResolver.adaptTo(Session.class);
            final javax.jcr.query.Query query = session.getWorkspace().getQueryManager().createQuery(sql2Query,javax.jcr.query.Query.JCR_SQL2);
            final QueryResult result = query.execute();
            NodeIterator pages=result.getNodes();
            JSONArray resultArray=new JSONArray();
            while(pages.hasNext()){
                Node page=pages.nextNode();
            }

These are just sample codes. Get Resource Resolver with proper permissions.