how to get references from a node. | Community
Skip to main content
October 16, 2024
Solved

how to get references from a node.

  • October 16, 2024
  • 1 reply
  • 719 views

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.
Best answer by SureshDhulipudi

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

1 reply

SureshDhulipudi
Community Advisor
SureshDhulipudiCommunity AdvisorAccepted solution
Community Advisor
October 16, 2024

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();
}
AliSyed1Author
October 17, 2024

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