How to fetch all details from content fragment using services like OSGI services using sling model
Solved! Go to Solution.
Views
Replies
Total Likes
package com.myorg.core.models;
import java.util.UUID;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Optional;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import com.adobe.cq.dam.cfm.ContentFragment;
import static java.util.Objects.nonNull;
@Model(adaptables = { SlingHttpServletRequest.class, Resource.class })
public class ContentFragModel {
@ScriptVariable
private Resource resource;
@ValueMapValue(name = "fragmentPath")
@Deleted Account
private String fragmentPath;
private String title;
private String pageUrl;
private String line1;
private String line2;
@PostConstruct
protected void init() {
if(fragmentPath != null) {
Resource contentRes = resource.getResourceResolver().getResource(fragmentPath);
if (nonNull(contentRes)) {
ContentFragment cf = contentRes.adaptTo(ContentFragment.class);
if (nonNull(cf)) {
title = cf.hasElement("name") ? cf.getElement("name").getContent() : StringUtils.EMPTY;
pageUrl = cf.hasElement("page-url") ? cf.getElement("page-url").getContent() : StringUtils.EMPTY;
line1 = cf.hasElement("line1") ? cf.getElement("line1").getContent() : StringUtils.EMPTY;
line2 = cf.hasElement("line2") ? cf.getElement("line2").getContent() : StringUtils.EMPTY;
}
}
}
}
/**
* @Return the title
*/
public String getTitle() {
return title;
}
/**
* @Return the pageUrl
*/
public String getPageUrl() {
return pageUrl;
}
/**
* @Return the line1
*/
public String getLine1() {
return line1;
}
/**
* @Return the line2
*/
public String getLine2() {
return line2;
}
}
You can write a service with query builder API to fetch the content fragments. You can map the results to Content Fragment model and read the contents.
https://aemsimplifiedbynikhil.wordpress.com/2020/10/11/content-fragment-as-java-apis/ - Check this out.
Hey @ABINESH76 you can use this solution as well but you have to make few changes for it
https://aem4beginner.blogspot.com/fetch-content-fragment-using-sling-model
package com.myorg.core.models;
import java.util.UUID;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Optional;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import com.adobe.cq.dam.cfm.ContentFragment;
import static java.util.Objects.nonNull;
@Model(adaptables = { SlingHttpServletRequest.class, Resource.class })
public class ContentFragModel {
@ScriptVariable
private Resource resource;
@ValueMapValue(name = "fragmentPath")
@Deleted Account
private String fragmentPath;
private String title;
private String pageUrl;
private String line1;
private String line2;
@PostConstruct
protected void init() {
if(fragmentPath != null) {
Resource contentRes = resource.getResourceResolver().getResource(fragmentPath);
if (nonNull(contentRes)) {
ContentFragment cf = contentRes.adaptTo(ContentFragment.class);
if (nonNull(cf)) {
title = cf.hasElement("name") ? cf.getElement("name").getContent() : StringUtils.EMPTY;
pageUrl = cf.hasElement("page-url") ? cf.getElement("page-url").getContent() : StringUtils.EMPTY;
line1 = cf.hasElement("line1") ? cf.getElement("line1").getContent() : StringUtils.EMPTY;
line2 = cf.hasElement("line2") ? cf.getElement("line2").getContent() : StringUtils.EMPTY;
}
}
}
}
/**
* @Return the title
*/
public String getTitle() {
return title;
}
/**
* @Return the pageUrl
*/
public String getPageUrl() {
return pageUrl;
}
/**
* @Return the line1
*/
public String getLine1() {
return line1;
}
/**
* @Return the line2
*/
public String getLine2() {
return line2;
}
}