Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Fetching jcr:content properties

v1101
Level 5
Level 5

Hello Community - I am using this model for the component to read the properties and perform some action, it is working and I am able to read the properties of the component.

 

@Model(adaptables = { Resource.class, SlingHttpServletRequest.class })

public class Test {
@PostConstruct
public void init() {

Node curNode = this.request.getResource().adaptTo(Node.class);
try {
if (curNode.hasProperty("compId")) {
String compId = curNode.getProperty("compId").getString();
}
}
}
}

I would like to read some additional properties which are required and only available in jcr:content(cq:PageContent) node of the page. I was trying to read using the below but it is not working. Am I doing anything wrong here? Can someone advise on this?

 

ResourceResolver resourceResolver=request.getResourceResolver();
Resource resource=resourceResolver.getResource("/content/testsite/en/homepage/jcr:content");
ValueMap properties = resource.adaptTo(ValueMap.class);
String title = properties.get("jcr:title", (String) null);

 

 

1 Accepted Solution
Anudeep_Garnepudi
Correct answer by
Community Advisor
Community Advisor

Hi @v1101 

You can do that by simply injecting Page Properties into Model. Check the below code.

@Model(adaptables = {Resource.class,SlingHttpServletRequest.class})
public class TestModel {
    @ScriptVariable
    ValueMap pageProperties;

    private pageTitle;

    public String getPageTitle() {
        return pageTitle;
    }
   @PostConstruct
    protected void init() {
        pageTitle = pageProperties.get("jcr:title", "No Title");
    }

}

I am simply returning Page Title.

AG

View solution in original post

5 Replies
Shubham_borole
Community Advisor
Community Advisor

Can you try something like this below?

@Model(adaptables = { Resource.class, SlingHttpServletRequest.class })

public class Test {
@ScriptVariable
private Page currentPage;
@PostConstruct
public void init() {
Resource currentPageRes = currentPage.adaptTo(Resource.class);

Resource currentPageContentRes = currentPageRes.getChild(JcrConstants.JCR_CONTENT)
ValueMap vm = currentPageContentRes.getValueMap();
String title = vm.get("jcr:title", String.class);



Node curNode = this.request.getResource().adaptTo(Node.class);
try {
if (curNode.hasProperty("compId")) {
String compId = curNode.getProperty("compId").getString();
}
}
}
}

 

Anudeep_Garnepudi
Correct answer by
Community Advisor
Community Advisor

Hi @v1101 

You can do that by simply injecting Page Properties into Model. Check the below code.

@Model(adaptables = {Resource.class,SlingHttpServletRequest.class})
public class TestModel {
    @ScriptVariable
    ValueMap pageProperties;

    private pageTitle;

    public String getPageTitle() {
        return pageTitle;
    }
   @PostConstruct
    protected void init() {
        pageTitle = pageProperties.get("jcr:title", "No Title");
    }

}

I am simply returning Page Title.

AG

View solution in original post