April 9, 2020
Estimated Post Reading Time ~

Content Migration in AEM using SlingPostServlet

A very basic migration flow looks as follows: 


In this scenario, you have a CMS(that could be Sitecore, Drupal, WordPress or any other CMS) which has source content that needs to be migrated to AEM. To achieve this, we typically need to do the following things:

Get content from source CMS in any format(XML, CSV, etc)
Process this content and extract content that needs to be exported to AEM. This would include parsing XML/CSV exported by source CMS and massaging it(if needed).

The processed content is then imported to AEM. There can be various strategies for this, like Talend, Package Manager, and SlingPostServlet.
I like SlingPostServlet as I feel it is closer to coding than other strategies. This blog is focused on that only. Considering that source CMS gives you XML, I created a Groovy script for migration. Here are the steps:
Parsing XML using Groovy XML Parser

def records = new XmlParser().parseText(file.text)?.blog
Creating a map out of parsed content

records.eachWithIndex { blog, idx ->
def name = blog.name.text()
def content = blog.outline.text()
def status = blog.status.text()
def parentSubject = blog.subject_parent.text()
Map contentMap = [
"./jcr:primaryType": "cq:Page",
"./jcr:content/jcr:primaryType": "cq:PageContent",
"./jcr:content/jcr:title": "${name}",
"./jcr:content/blog/sling:resourceType": resourceType,
"./jcr:content/blog/status": status,
"./jcr:content/blog/parentSubject": parentSubject,
"./jcr:content/blog/text/sling:resourceType": "foundation/components/text",
"./jcr:content/blog/text/text": "${content}"
]
callPost("${baseContentPath}blog${idx}", contentMap)
}

Any key in the map corresponds to a property in JCR. If you split the property by “/”, the last element would give you the property name and elements from first to second last gives you the hierarchy. Taking example of

"./jcr:content/blog/status": "Published"

entry in the map, this key would create hierarchy jcr:content -> blog and blog node would have a property status and value Published.

Posting content to AEM  

void callPost(String baseURL, Map contentMap) {
/*Setting auth basic in request doesnt work... Had to set it in headers*/
// http.auth.basic("admin", "admin")
MigrationConfiguration.client.request(Method.POST) {
uri.path = baseURL
requestContentType = ContentType.URLENC
headers.'Authorization' = "Basic ${"admin:admin".bytes.encodeBase64().toString()}"
body = contentMap

response.failure = { resp -> println "\nERROR: ${resp.statusLine} for ${uri.path}" }
}
}

And that is all you need to do.
You can now check the content hierarchy in CRX. You can modify this Groovy script as per your use case.
In addition, if you would like to know more about Content Migration to AEM, here’s a simple step-by-step guide on how to do it?
Source: https://www.tothenew.com/blog/content-migration-in-aem-using-slingpostservlet/


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.