Using the Scaffolding REST API with ScriptRunner for Confluence

Overview

Examples of using the Scaffolding REST API to manipulate Scaffolding data by using Groovy script with a third-party app ScriptRunner for Confluence.

Step 1: Create a sample page

Use this wiki markup to create a sample page.

||Text Data||{text-data:atlassian-macro-output-type=INLINE|content=wiki|name=Sample Text}{text-data}| ||List Data||{list-data:atlassian-macro-output-type=INLINE|name=Sample List|type=check} {list-option:value=Option 1}{list-option} {list-option:value=Option 2}{list-option} {list-data}| ||Number Data||{number-data:atlassian-macro-output-type=INLINE|name=Sample Number}{number-data}| ||Date Data||{date-data:atlassian-macro-output-type=INLINE|name=Sample Date}{date-data}|

Step 2: Use the ScriptRunner for Confluence app and run the script

Use ScriptRunner for Confluence to run the following Groovy script. Remember to modify the parameters according to your needs.

// Sample script to manipulate Scaffolding data // Atlassian Imports import com.atlassian.sal.api.component.ComponentLocator import com.atlassian.confluence.pages.templates.PageTemplateManager import com.atlassian.confluence.pages.PageManager import com.atlassian.confluence.spaces.SpaceManager import com.atlassian.confluence.setup.settings.SettingsManager //JSON Manipulations import org.json.JSONArray // Misc manipulations import java.nio.charset.StandardCharsets import java.text.SimpleDateFormat // Page and Space managers def pageTemplateManager = ComponentLocator.getComponent(PageTemplateManager) def spaceManager = ComponentLocator.getComponent(SpaceManager) def pageManager = ComponentLocator.getComponent(PageManager) def settingsManager = ComponentLocator.getComponent(SettingsManager) // Sample Space details def sampleSpaceKey = "SAMPLE" def sampleSpace = spaceManager.getSpace(sampleSpaceKey) def sampleHomePage = sampleSpace.getHomePage() // REST API Access, could be implemented different ways def username = "admin" def password = "Charlie!" def authStr = (username+":"+password).bytes.encodeBase64().toString() log.warn("UPWD: " + authStr) // Confluence base URL def baseURL = settingsManager.getGlobalSettings().getBaseUrl() log.warn(baseURL) // Scaffolding REST URL def formURL = "/rest/scaffolding/1.0/api/form/" def metaURL = "/rest/scaffolding/1.0/api/form/meta" // Page to manipulate Scaffolding data def samplePageTitle = "Sample Page" def samplePage = pageManager.getPage(sampleSpaceKey, samplePageTitle) // Sample Page ID def scaffPageID = samplePage.getId() log.warn(scaffPageID) // Building REST URL URL restApiURL = new URL(baseURL + formURL + scaffPageID) log.warn(restApiURL) // Creating HTTP Connection HttpURLConnection httpConnection = (HttpURLConnection) restApiURL.openConnection() // Building data to send def fields = new JSONArray() def value = new LinkedHashMap() def options = [] // For Text Data value.clear() value.put("name", "Sample Text") value.put("macro", "text-data") value.put("value", "Some Interesting Text\nEven with the New Lines") fields.put(value) // For List Data value.clear() value.put("name", "Sample List") value.put("macro", "list-data") options.clear() options.add("Option 1") value.put("value", options.clone()) fields.put(value) // For Number Data value.clear() value.put("name", "Sample Number") value.put("macro", "number-data") value.put("value", "123") fields.put(value) // For Date Data value.clear() value.put("name", "Sample Date") value.put("macro", "date-data") value.put("value", new SimpleDateFormat("yyyy-mm-dd HH:mm:ss").format(new SimpleDateFormat("dd/MM/yyyy").parse("24/02/2019"))) fields.put(value) log.warn(fields.toString()) // Connecting and sending data httpConnection.setRequestMethod("PUT") httpConnection.setRequestProperty("Accept", "application/json") httpConnection.setRequestProperty("Authorization", "Basic " + authStr) httpConnection.setDoOutput(true) httpConnection.setDoInput(true) httpConnection.connect() // Output Stream def outputStream = httpConnection.getOutputStream() // Writing outputStream.write(fields.toString().getBytes(StandardCharsets.UTF_8)) log.warn(httpConnection.getResponseCode()) log.warn(httpConnection.getResponseMessage()) // Close and Finish outputStream.flush() outputStream.close() httpConnection.disconnect() // Script credit: Areg Vrtanesyan

See also