How to get the list of all assets and content fragments used in a page? | Community
Skip to main content
thatsmeadarsh
Level 3
October 6, 2021
Solved

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

  • October 6, 2021
  • 2 replies
  • 4704 views

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

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

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());

2 replies

Level 2
October 6, 2021

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
thatsmeadarshAuthorAccepted solution
Level 3
October 6, 2021

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());
walterch
October 6, 2021
thatsmeadarsh
Level 3
October 6, 2021

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

 

Level 2
October 6, 2021

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