Service using Content Fragement | Community
Skip to main content
Level 2
September 18, 2022
Solved

Service using Content Fragement

  • September 18, 2022
  • 3 replies
  • 1115 views

How to fetch all details from content fragment using services like OSGI services using sling model

 

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 Hemant_arora


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")
@7392697
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;

}
}
}
}

 

/**
* @2007960 the title
*/
public String getTitle() {
return title;
}

/**
* @2007960 the pageUrl
*/
public String getPageUrl() {
return pageUrl;
}

/**
* @2007960 the line1
*/
public String getLine1() {
return line1;
}

/**
* @2007960 the line2
*/
public String getLine2() {
return line2;
}


}

3 replies

Saravanan_Dharmaraj
Community Advisor
Community Advisor
September 18, 2022

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. 

 

Level 2
September 19, 2022

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

Hemant_arora
Hemant_aroraAccepted solution
Level 8
September 19, 2022


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")
@7392697
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;

}
}
}
}

 

/**
* @2007960 the title
*/
public String getTitle() {
return title;
}

/**
* @2007960 the pageUrl
*/
public String getPageUrl() {
return pageUrl;
}

/**
* @2007960 the line1
*/
public String getLine1() {
return line1;
}

/**
* @2007960 the line2
*/
public String getLine2() {
return line2;
}


}