Expand my Community achievements bar.

SOLVED

how to get references from a node.

Avatar

Level 3

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.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

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

Avatar

Level 3

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