Apply page policy through code | Adobe Higher Education
Skip to main content
Level 6
March 18, 2023
解決済み

Apply page policy through code

  • March 18, 2023
  • 2 の返信
  • 875 ビュー

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

 

 

このトピックへの返信は締め切られました。
ベストアンサー 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 の返信

lukasz-m
Community Advisor
lukasz-mCommunity Advisor回答
Community Advisor
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();
arunpatidar
Community Advisor
Community Advisor
March 21, 2023