Swapping of template in AEM

Avatar

Avatar
Level 1
Prashanth96
Level 1

Likes

0 likes

Total Posts

2 posts

Correct reply

0 solutions
View profile

Avatar
Level 1
Prashanth96
Level 1

Likes

0 likes

Total Posts

2 posts

Correct reply

0 solutions
View profile
Prashanth96
Level 1

19-01-2021

Hi, 

 

Scenario: we have 1000's of pages created based on a certain template, now we are trying to replace the template with a new one for some of the pages. I wanted to know is it possible to swap template for those existing pages?

 

If yes, could you please provide me with the solution how we can approach for the same.

 

@jbrar @Arun_Patidar @kautuk_sahni @BrianKasingli

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Springboard
Level 7
KiranVedantam1992
Level 7

Likes

195 likes

Total Posts

213 posts

Correct reply

60 solutions
Top badges earned
Springboard
Give Back 5
Ignite 1
Affirm 50
Validate 1
View profile

Avatar
Springboard
Level 7
KiranVedantam1992
Level 7

Likes

195 likes

Total Posts

213 posts

Correct reply

60 solutions
Top badges earned
Springboard
Give Back 5
Ignite 1
Affirm 50
Validate 1
View profile
KiranVedantam1992
Level 7

19-01-2021

Hi @Prashanth96,

 

You did not mention which template you are using. Is it a static template or editable?

 

1. For static template, you cannot do the change once the page is created

2. For an editable template, you can modify the template and they will reflect automatically as they maintain a dynamic reference to the page.

 

In your use-case, replacing the old template with a new one, will not work in static templates. For dynamic templates, you can change the below properties in page's jcr:content to make it work

1. cq:template

2. sling:resourceType

 

Note: You might run into errors if the code on the new page component is expecting a different content than what is currently under your jcr:content.

 

I would recommend creating a new editable template and upgrade your changes if your template is static. If it is editable, create different versions of components using a style system, play around with page policies, initial content, and page structure to achieve your desired result.

 

Hope this helps. All the best.

 

Thanks,

Kiran Vedantam.

Answers (2)

Answers (2)

Avatar

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

645 likes

Total Posts

607 posts

Correct reply

239 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

645 likes

Total Posts

607 posts

Correct reply

239 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
BrianKasingli
MVP

20-01-2021

@Prashanth96,

One way is by using the AEM groovy console to find all those pages, and update the sling:resourceType, cq:template, you can create a migration script. https://github.com/icfnext/aem-groovy-console

 

The migration script would look something like this:

 

 

// example of finding all nodes with the sling:resourceType == 'my-site/components/structure/page';
def doMigration() {
    def queryManager = session.workspace.queryManager
    def statement = "/jcr:root/content/my-site//*[@sling:resourceType='my-site/components/structure/page']"
    def query = queryManager.createQuery(statement, "xpath");
    def result = query.execute();
    def foundNodesInRows = result.rows;
    // applying write method to each node.
    foundNodesInRows.each { row ->
        def pageNode = getNode(row.path)
        pageNode.set("sling:resourceType", "my-site/components/structure/page2")
        pageNode.set("cq:template", "/conf/my-site/settings/wcm/templates/page2")
        // some kind of move operation...
        println "Updated sling:resourceType * cq:template on ${row.path}"
    }
}
 
doMigration()
session.save()

 

 

While creating the migration script, you should pull down content that is similar to production into your development environment, so you can test your script as you are building the script.

 

To take extra precautions, you should pull down the content into a lower environment, run the migration script, and then using a package manager to upload the code back to the production author... then replicate.

 

Avatar

Avatar
Validate 10
MVP
kunal23
MVP

Likes

166 likes

Total Posts

565 posts

Correct reply

172 solutions
Top badges earned
Validate 10
Validate 1
Ignite 3
Ignite 1
Give Back 50
View profile

Avatar
Validate 10
MVP
kunal23
MVP

Likes

166 likes

Total Posts

565 posts

Correct reply

172 solutions
Top badges earned
Validate 10
Validate 1
Ignite 3
Ignite 1
Give Back 50
View profile
kunal23
MVP

20-01-2021

 

You can use AEM Modernization tools for updating the existing page properties. 

More details can be found here - https://opensource.adobe.com/aem-modernize-tools/pages/configuration/component.html