Expand my Community achievements bar.

Get all the assets/tags/contentpolicy references on the page within a workflow process step

Avatar

Level 2

Hi,

I am writing a custom workflow step to publish a page and all the asset/tag/content-policy references on the page, so I can publish them as well. Currently, the servlet http://localhost:4502/libs/wcm/core/content/reference.json gets all the references, but I am unable to access the services used within that servlet in my code. 

I am able to get all the asset references using the AssetReferenceSearch API. Is there a similar service for tags & content policies on a page?

Please help!

 

Thanks!

5 Replies

Avatar

Employee Advisor

Could you please try with below APIs -

 

For tags - TagManager (The Adobe AEM Quickstart and Web Application.)

and try with 

Tag[]getTags(Resource resource)
Retrieves the tags set on the given resource.

 

For Content policies : ContentPolicyManager and sharing below sample code -

/**
 * 
 */
package com.aem.demo.core.models;

import java.util.Objects;

import javax.annotation.PostConstruct;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.OSGiService;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aem.demo.core.services.JcrUtility;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.policies.ContentPolicy;
import com.day.cq.wcm.api.policies.ContentPolicyManager;
import com.drew.lang.annotations.Nullable;

/**
 * @author debal
 *
 */

@Model(adaptables = SlingHttpServletRequest.class)
public class PageModel {

	private final Logger logger = LoggerFactory.getLogger(PageModel.class);

	@OSGiService
	JcrUtility jcrUtility;

	@ScriptVariable
	private Page currentPage;

	private String pageStyle;

	@PostConstruct
	protected void init() {
		ResourceResolver resourceResolver = jcrUtility.getResourceResolver();
		String currentPagepath = currentPage.getPath();
		logger.info(" **** Current Page Path **** {}", currentPagepath);
		@Nullable
		Resource resource = resourceResolver.getResource(currentPagepath);
		if (Objects.nonNull(resource)) {
			ContentPolicyManager contentPolicyManager = resourceResolver.adaptTo(ContentPolicyManager.class);
			ContentPolicy contentPolicy = contentPolicyManager.getPolicy(resource);
			pageStyle = contentPolicy.getPath();
			logger.info(" **** Page style **** {}", pageStyle);
		}
		jcrUtility.closeResourceResolver(resourceResolver);

	}

	/**
	 * @return the pageStyle
	 */
	public String getPageStyle() {
		return pageStyle;
	}

}

Avatar

Level 2

@DEBAL_DAS Thanks for the response.

The ContentPolicyManager will return the policies for just the current resource right? Is there an existing service/method to get the content policies of all the components & style systems referenced on that page?

Thanks!

Avatar

Level 2

@arunpatidar Thanks for the response.

The ReferenceSearch API doesn't work. It's returning null & not even returning the assets that I am getting through the AssetReferenceSearch API.