Expand my Community achievements bar.

SOLVED

Apply page policy through code

Avatar

Level 8

Hi,

I have a requirement to apply web resource clientlib in page policy through backend code, the template editors can choose but I want it through backend code

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @Vani1012,

On code level you can use following api to get policy definition and do some changes:

Below is a sample code snippet. It gets template definition, to retrieve information about specific policy that is used by Page Policy, and set value of appResourcesClientlib property that represents Web Resources Client Library on GUI level.

import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.Resource;

import com.day.cq.wcm.api.policies.ContentPolicy;
import com.day.cq.wcm.api.policies.ContentPolicyMapping;

// path to template definition
String templatePath = "/conf/we-retail/settings/wcm/templates/content-page";
Resource templateResource = resourceResolver.getResource(templatePath + "/policies/jcr:content");

// getting policy from given template resource
ContentPolicy contentPolicy = templateResource.adaptTo(ContentPolicyMapping.class).getPolicy();

// getting value map to set some properties
ModifiableValueMap modifiableValueMap = contentPolicy.adaptTo(Resource.class).adaptTo(ModifiableValueMap.class);
modifiableValueMap.put("appResourcesClientlib", "dam.gui.msm.properties");
//saving changes
resourceResolver.commit();

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @Vani1012,

On code level you can use following api to get policy definition and do some changes:

Below is a sample code snippet. It gets template definition, to retrieve information about specific policy that is used by Page Policy, and set value of appResourcesClientlib property that represents Web Resources Client Library on GUI level.

import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.Resource;

import com.day.cq.wcm.api.policies.ContentPolicy;
import com.day.cq.wcm.api.policies.ContentPolicyMapping;

// path to template definition
String templatePath = "/conf/we-retail/settings/wcm/templates/content-page";
Resource templateResource = resourceResolver.getResource(templatePath + "/policies/jcr:content");

// getting policy from given template resource
ContentPolicy contentPolicy = templateResource.adaptTo(ContentPolicyMapping.class).getPolicy();

// getting value map to set some properties
ModifiableValueMap modifiableValueMap = contentPolicy.adaptTo(Resource.class).adaptTo(ModifiableValueMap.class);
modifiableValueMap.put("appResourcesClientlib", "dam.gui.msm.properties");
//saving changes
resourceResolver.commit();