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

How to get the list of all assets and content fragments used in a page?

thatsmeadarsh
Level 3
Level 3

I need to get the list of all Content fragments and assets used in a page in my sling servlet. Which API should I use for this.

 

Thanks

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution
thatsmeadarsh
Correct answer by
Level 3
Level 3

Thanks @shaileshb584084  and @walterch  for your time. I tried with AssetReferenceSearch and now I am able to get the list of assets and content fragment paths in the page. Not sure if I get nested CF list from this. I need to work on it. I am adding my code fragment below. Thanks again for your time.

Node node = resource.adaptTo(Node.class);
	AssetReferenceSearch reference = new AssetReferenceSearch(node,"/content/dam/custom-project-path",resourceResolver);
	Map<String,Asset> referenceList = new HashMap<String,Asset>();
	   referenceList.putAll(reference.search());

View solution in original post

0 Replies
shaileshb584084
Level 3
Level 3

Hi,

 

Just create a query in your servlet to get the list of the assets/contentFragment.

 

Query for testing it on http://localhost:4502/libs/cq/search/content/querydebug.html

type=dam:Asset

path=<page-path>

 

Finally create the same using 

	Session session = resourceResolver.adaptTo(Session.class);

	if(null!=session)
	{
		try {
		QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class);
		Map<String, String> queryParameterMap = new HashMap<>();
		queryParameterMap.put("type", DamConstants.NT_DAM_ASSET);
		queryParameterMap.put("path",<page-path>;



		Query query = queryBuilder.createQuery(PredicateGroup.create(queryParameterMap), session);
		SearchResult searchResult = query.getResult();
		if (null != searchResult) {
		for (Hit hit : searchResult.getHits()) {

		Resource assetResource = hit.getResource();
	
		//you can add your code for further processing as per business logic
	
		}catch(Exception e) {
			
		}

	}

In the query you can get the page-path from the servlet. One you get the results, add an additional check to check if the asset reference is present in the page or not & finally you will get the list of all valid assets.

 

Thanks 

thatsmeadarsh
Correct answer by
Level 3
Level 3

Thanks @shaileshb584084  and @walterch  for your time. I tried with AssetReferenceSearch and now I am able to get the list of assets and content fragment paths in the page. Not sure if I get nested CF list from this. I need to work on it. I am adding my code fragment below. Thanks again for your time.

Node node = resource.adaptTo(Node.class);
	AssetReferenceSearch reference = new AssetReferenceSearch(node,"/content/dam/custom-project-path",resourceResolver);
	Map<String,Asset> referenceList = new HashMap<String,Asset>();
	   referenceList.putAll(reference.search());
thatsmeadarsh
Level 3
Level 3

Hi @walterch 

 

I need the reference list when a user click on a custom button within the page. I tried to use the class ReferenceProvider but it seems like there are lot of implementations for the same. Also I am not able to Refer most of these implementation. I am just curious to understand how the reference list is getting populated in /libs/wcm/core/content/sites/publishpagewizard.html?item="custom-path" while the default publication is invoked. I am just trying to understand if there is any AEM specific class to do this functionality.

Thanks

 

Screenshot 2021-10-06 at 5.54.09 PM.png

shaileshb584084
Level 3
Level 3

Just take the request param i.e. the page path and use the same in the query described in the comment above. It will resolve your business requirement.

 

There is no direct API to find the assets & content fragment used on the page & which are still valid.

 

Thanks

shaileshb584084
Level 3
Level 3

 ACS AEM Commons Report Builder - This is used just for reporting. It is not possible to do automatic processing with the result of the report.

 

If only reporting is to be considered, then only one should go with the " ACS AEM Commons Report Builder".

thatsmeadarsh
Level 3
Level 3

Still it doesn't solve my issue. Say, if I have a page A. The page A is included by a CF A. The CF A is referred to another CF B. My requirement is when the user clicks the page A, I should be able to fetch both CF A and CFB as references. I an not sure if ReferenceAggregator can be used for that purpose.