Back

Importing Content from Confluence

Description

dotCMS 5 series introduced the Scripting as a Service API endpoint.  This endpoint allows you to execute powerful Velocity scripts from a command line interface.  As an example, using the scripting as a service endpoint, you can force dotCMS to retrieve rendered content from an Atlassian's Confluence instance and import it into dotCMS as normal web page content.

Please note that the user executing the script must have the "System > Scripting User"  role.

Code

## Importing Confluence content

export TOK="{GET_A_DOTCMS_API_TOKEN}"


curl -XPUT https://{YOUR_DOTCMS_SITE}/api/vtl/dynamic/  \
    -H "Authorization:Bearer $TOK" \
    -H "Content-Type:text/plain" \
    -d '

        ## Set the authorization headers
        #set($headers = {})
        #set($x = $headers.put("Accept", "application/json"))
        #set($x = $headers.put("Authorization", "Basic {GET_A_CONFLUENCE_API_TOKEN}"))
        
        
        ## Get the first 10 pages of raw (not rendered) content from Confluence 
        #set($rawData = $json.fetch("https://{YOUR_SITE_NAME}.atlassian.net/wiki/rest/api/content?spaceKey={YOUR_SPACE_NAME}&start=0&limit=10&expand=body.view,body.storage,metadata.labels", $headers))
        
        
        ## Loop over the results
        #foreach($result in $rawData.results)
        
            #set($postJson = $result.body.storage)
            
            #set($postJson = $postJson.put("representation", "storage"))
            
            
            
            ## Send the results back to Confluence to be rendered     
            #set($newData = $json.post("https://{YOUR_SITE_NAME}.atlassian.net/wiki/rest/api/contentbody/convert/view?expand=webresource.superbatch.uris.css", $headers, "$postJson"))
            #set($htmlString="")
            
            ## add any external CSS calls to the rendered content
            #foreach($conCss in $newData.webresource.superbatch.uris.css)
                #set($htmlString="$htmlString <link type=${esc.q}text/css$esc.q rel=${esc.q}stylesheet${esc.q} href=${esc.q}$conCss${esc.q} media=${esc.q}all${esc.q}>")
            #end
            #set($htmlString="$htmlString $newData.value")
            
            ## create Map to use as our content
            #set($cContent = {})
            
            ## add content values to map
            $cContent.put("title", "$result.title")
            $cContent.put("contentType", "webPageContent")
            $cContent.put("body", $htmlString)
            
            ## Host and Folder to place content in
            $cContent.put("contentHost", "demo.dotcms.com:/intranet")
            
            
            ## Check the content in and call the "Save / Publish" workflow action on it.
            #set($savedContent = $workflowtool.fire($cContent, "b9d89c80-3d88-4311-8365-187323c96436")); 
            $savedContent
        #end
'

Have you created code you'd like to share with the dotCMS community?

Share your code Become a Contributor