Skip to main content
Level 3
Oct 16, 2024 9:03 AM UTC
Solved

how to get references from a node.

Hello every one , I want to get references from a node whether if it is a page , asset , experience fragment. Is there a way to get references in java. I used APIs which will be trigerred when i click on publish button. but these apis are different for each node. It is different for page , experience fragment , asset e.t.c.

 

 

My requirement is to filter out some references. particular resource path should be stored in a variable. 

 

By using /bin/replicate I am getting all the references and resource path in path parameter . I cannot know which path is actual resource and which are references.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
SureshDhulipudi
SureshDhulipudiAccepted solution
Community Advisor
Oct 16, 2024 12:59 PM UTC

Use the Resource API to determine the node type (page, asset, experience fragment).

 

Use the ReferenceSearch API to get references for the node.

 

String nodeType = getNodeType(resource);
List<Reference> references = getReferences(resource);

 

Resource resource = resourceResolver.getResource(resourcePath);
if (resource == null) {
throw new IllegalArgumentException("Resource not found: " + resourcePath);
}
private String getNodeType(Resource resource) {
ValueMap properties = resource.getValueMap();
String primaryType = properties.get(JcrConstants.JCR_PRIMARYTYPE, String.class);

if (primaryType.equals("cq:Page")) {
return "Page";
} else if (primaryType.equals("dam:Asset")) {
return "Asset";
} else if (primaryType.equals("cq:ExperienceFragment")) {
return "Experience Fragment";
} else {
return "Unknown";
}
}

 

private List<Reference> getReferences(Resource resource) {
ReferenceSearch referenceSearch = new ReferenceSearch();
Map<String, Info> references = referenceSearch.search(resource);
return references.values().stream()
.map(Info::getReferences)
.flatMap(List::stream)
.toList();
}

2 replies

SureshDhulipudi
Community Advisor
SureshDhulipudiCommunity AdvisorAccepted solution
Community Advisor
Oct 16, 2024 12:59 PM UTC

Use the Resource API to determine the node type (page, asset, experience fragment).

 

Use the ReferenceSearch API to get references for the node.

 

String nodeType = getNodeType(resource);
List<Reference> references = getReferences(resource);

 

Resource resource = resourceResolver.getResource(resourcePath);
if (resource == null) {
throw new IllegalArgumentException("Resource not found: " + resourcePath);
}
private String getNodeType(Resource resource) {
ValueMap properties = resource.getValueMap();
String primaryType = properties.get(JcrConstants.JCR_PRIMARYTYPE, String.class);

if (primaryType.equals("cq:Page")) {
return "Page";
} else if (primaryType.equals("dam:Asset")) {
return "Asset";
} else if (primaryType.equals("cq:ExperienceFragment")) {
return "Experience Fragment";
} else {
return "Unknown";
}
}

 

private List<Reference> getReferences(Resource resource) {
ReferenceSearch referenceSearch = new ReferenceSearch();
Map<String, Info> references = referenceSearch.search(resource);
return references.values().stream()
.map(Info::getReferences)
.flatMap(List::stream)
.toList();
}
Translate
AliSyed1Author
Level 3
Oct 17, 2024 5:52 AM UTC

thanks for the answer I will look into it and inform you  if it works fine.I hope it works fine.

Translate