Apply page policy through code | Community
Skip to main content
New Participant
March 18, 2023
Solved

Apply page policy through code

  • March 18, 2023
  • 2 replies
  • 874 views

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

 

 

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 lukasz-m

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

2 replies

arunpatidar
New Participant
March 21, 2023
lukasz-m
lukasz-mAccepted solution
New Participant
March 18, 2023

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