Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Programatically find out if a page is configured with vanities AEM 6.5

Avatar

Level 8

Hi

I would like to find out programatically if a page with Vanity was called. 

This is what I tried:

public boolean getVerifyResourceVanity(Resource resource) {
if (resource == null) {
return false;
}

final String[] vanities = resource.getValueMap().get(VANITY_PROPERTY, new String[0]);
if (vanities.length == 0) {
return false;
}

final Matcher matcher = PREFIX_PATTERN.matcher(resource.getPath());
if (!matcher.find()) {
return false;
}
return true;
}

This information would help me disable a disclaimer when a page with Vanity was called.

Thanks.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

You can try

 

import com.day.cq.wcm.api.Page;
import org.apache.sling.api.resource.ValueMap;

// Assuming you have the Page object
Page page = // Obtain the Page object here

// Get the page's properties
ValueMap properties = page.getProperties();

// Get the cq:vanityUrl property (which can be single or multivalued)
String[] vanityUrls = properties.get("cq:vanityUrl", String[].class);

if (vanityUrls != null && vanityUrls.length > 0) {
    for (String vanityUrl : vanityUrls) {
        System.out.println("Vanity URL: " + vanityUrl);
    }
} else {
    System.out.println("No vanity URLs configured for this page.");
}


Arun Patidar

View solution in original post

3 Replies

Avatar

Community Advisor

@anasustic AEM Pagemanager API already have method to fetch vanity url. Please check and also try to use highest abstraction level api where possible AEM > Sling > JCR

 

https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/wcm/api/Pa...

Avatar

Level 8

Thank you so much for your answer @Shashi_Mulugu .

The getVanityUrl() method in AEM Pagemanager API returns a String but as you can see in AEM -> Page Property -> Vanity one can configure an array of strings for Vanities. 

 

anasustic_0-1692019640160.png

 

That is why I used 

final String[] vanities = resource.getValueMap().get(VANITY_PROPERTY, new String[0]);

 

Avatar

Correct answer by
Community Advisor

You can try

 

import com.day.cq.wcm.api.Page;
import org.apache.sling.api.resource.ValueMap;

// Assuming you have the Page object
Page page = // Obtain the Page object here

// Get the page's properties
ValueMap properties = page.getProperties();

// Get the cq:vanityUrl property (which can be single or multivalued)
String[] vanityUrls = properties.get("cq:vanityUrl", String[].class);

if (vanityUrls != null && vanityUrls.length > 0) {
    for (String vanityUrl : vanityUrls) {
        System.out.println("Vanity URL: " + vanityUrl);
    }
} else {
    System.out.println("No vanity URLs configured for this page.");
}


Arun Patidar