Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Script for editing page properties

Avatar

Level 1

Hello together,

is it possible to edit the page properties via Java Script? I would have to do this for 4000 pages, which would take a very long time manually.

The script must go to the page properties --> Advanced tab --> Break inheritance --> Check an option --> Close inheritance and activate the page.

Can you provide such a possibility via a script that can be executed in CRX?

best
CK

1 Accepted Solution

Avatar

Correct answer by
Level 6

Hi @Darwin 

 

You can use groovy scripts to achieve this. Here are some of the articles that might help you.

 

https://labs.tadigital.com/index.php/2018/12/18/groovy-script-in-aem/

https://hashimkhan.in/aem-adobecq5-code-templates/groovy-script/

 

Thanks

View solution in original post

3 Replies

Avatar

Correct answer by
Level 6

Hi @Darwin 

 

You can use groovy scripts to achieve this. Here are some of the articles that might help you.

 

https://labs.tadigital.com/index.php/2018/12/18/groovy-script-in-aem/

https://hashimkhan.in/aem-adobecq5-code-templates/groovy-script/

 

Thanks

Avatar

Community Advisor

Hey @JeevanRaj Did you get a chance to explore ACS Commons Data Importer Utility. It allows you to import bulk data using csv into AEM. 

Data Importer (adobe-consulting-services.github.io)

Avatar

Community Advisor

Hi @Darwin, I think using JS will not be the proper option. Groovy console as @JeevanRaj suggest is the way to go. Below you can find simple groovy script that I believe do what you need.

import com.day.cq.wcm.msm.api.LiveRelationshipManager
import org.apache.sling.api.resource.ModifiableValueMap

// array of properties that should be changed
def propertyNames = ["jcr:title"] as String[]

// map of propertyName:newValue
def propertyValues = ["jcr:title":"new value"]

// array of page paths that should be changed
def pagePaths = ["/content/we-retail/us/en/men"] as String[]

def liveRelationshipManager = getService(LiveRelationshipManager.class)

pagePaths?.each { pagePath ->
    def pageResource = resourceResolver.getResource(pagePath)
    def liveRelationship = liveRelationshipManager.getLiveRelationship(pageResource, true)
    
    liveRelationshipManager.cancelPropertyRelationship(resourceResolver, liveRelationship, propertyNames, true)
    
    def jcrContentResource = resourceResolver.getResource(pageResource?.path + "/jcr:content")
    def valueMap = jcrContentResource?.adaptTo(ModifiableValueMap.class)
    
    println "Page has been updated ${pageResource?.path}"
    propertyNames?.each { property ->
        valueMap.put(property, propertyValues[property])
        println "Property has been changed ${property}"
    }
    resourceResolver.commit()
    
    // activating jcr:content because this is place where page properties are stored and this node has been changed
    activate(jcrContentResource?.path)
}