Expand my Community achievements bar.

How to get dam asset references in aem programmatically?

Avatar

Employee

Hi,

I would like to know if there is a way to get the list of references of an asset through APIs.

Keerthana_H_N_0-1737462671004.png


Thank you.

 

Topics

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

6 Replies

Avatar

Community Advisor

Hi, 

 

You can use the ReferenceSearch API https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/wcm/common... or depending on what you need, you can use the AssetSearchReference https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/dam/common...  as well.

 

Hope this helps



Esteban Bustamante

Avatar

Employee

Hi @EstebanBustamante ,

 

Tried both api not getting the result. Which asset path has to passed as params? can you please share the example code snippet.

Avatar

Community Advisor

Avatar

Employee

Hi @EstebanBustamante ,

 

This asset is actually spinset. Using api it's returning spinset members as a results instead of reference asset path. Is this api applicable for spinset/imageset/mixedmediaset assets? 

Avatar

Community Advisor

What @EstebanBustamante  said, and here's some very simple example code, this is sandbox code for learning only:

package com.sourcedcode.core.services.impl;

import com.day.cq.commons.ReferenceSearch;
import com.day.cq.commons.ReferenceSearch.Info;
import com.example.core.services.AssetReferenceFinder;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.component.annotations.Component;

import java.util.List;
import java.util.Map;

@Component(service = AssetReferenceFinder.class, immediate = true)
public class AssetReferenceFinderImpl implements AssetReferenceFinder {

    @Override
    public List<Info> getReferences(ResourceResolver resourceResolver, String assetPath) {
        Resource assetResource = resourceResolver.getResource(assetPath);
        if (assetResource == null) {
            throw new IllegalArgumentException("Asset not found at path: " + assetPath);
        }

        ReferenceSearch referenceSearch = new ReferenceSearch();
        Map<String, String> params = ReferenceSearch.getDefaultParams();
        return referenceSearch.search(assetResource, params);
    }
}
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
....

List<Info> references = assetReferenceFinder.getReferences(resourceResolver, assetPath);

// Use Gson to build JSON response
Gson gson = new Gson();
JsonArray referencesJsonArray = new JsonArray();

for (Info info : references) {
    JsonObject referenceJson = new JsonObject();
    referenceJson.addProperty("nodePath", info.getNodePath());
    referenceJson.addProperty("propertyName", info.getPropertyName());
    referenceJson.addProperty("type", info.getType());
    referencesJsonArray.add(referenceJson);
}

// Write JSON array to the response
response.setContentType("application/json");
response.getWriter().write(gson.toJson(referencesJsonArray));

 

Avatar

Administrator

@Keerthana_H_N Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!



Kautuk Sahni