Page tree
Skip to end of metadata
Go to start of metadata

Page scripts

List pages

var spaceKey = 'ds';
result.promise = httpClient.get({
  url: "/rest/api/content?spaceKey=" + spaceKey
}); 

Get Page Content

// replace the pageId
var pageId = 1114116;
result.promise = httpClient.get({
  url: '/rest/api/content/' + pageId + '?expand=body.storage'
}).then(function (body) {
  return {
    "content-type": "text/html",
    body: JSON.parse(body).body.storage.value
  }
});

Create Page

var new_page = {
  "type": "page",
  "title": "Example Content title",
  //"ancestors": [{ "id": 456}],
  "space": {
    "key": "TST"
  },
  "body": {
    "storage": {
      "value": "<p>This is a new page</p>",
      "representation": "storage"
    }
  }
};
result.promise = httpClient.post({
  uri: '/rest/api/content',
  json: true,
  body: new_page
});

Create Child Page

var spaceKey = "TST";
var parentPageName = "Example+Content+title";
var newPageName = "Child Page";
var newPageContent = "<p>This is a new child page</p>";
// new a page JSON
var new_page = {
  "type": "page",
  "title": newPageName,
  "ancestors": [{
    "id": 0
  }],
  "space": {
    "key": spaceKey
  },
  "body": {
    "storage": {
      "value": newPageContent,
      "representation": "storage"
    }
  }
};
result.promise = httpClient.get('/rest/api/content?title=' + parentPageName + '&spaceKey=' + spaceKey)
  .then(function (body) {
    var parentId = JSON.parse(body).results[0].id;
    console.log(parentId);
    new_page.ancestors[0].id = parseInt(parentId);
    return httpClient.post({
      uri: '/rest/api/content',
      json: true,
      body: new_page
    });
  });

Copy a page

/*
Copy a page by space+title to another space+title
Known limitations:
- Will copy first match on the title in the space. 
- Does not rewire wiki links if copying to a new space.
- Does not copy attachments
- Copy to top level in destination space
- Does not copy permission
- Page belongs to Plugin user
*/
var sourceSpaceKey = "TST";
var sourcePageTitle = "Example Content title";
var destinationSpaceKey = "TST";
var destinationPageTitle = "Copy of " + sourcePageTitle;
result.promise = httpClient.get('/rest/api/content?title=' + sourcePageTitle + '&spaceKey=' + sourceSpaceKey + '&expand=body.storage')
	.then(function (body) {
		var content = JSON.parse(body).results[0].body.storage.value;
		var page_copy = {
			"type": "page",
			"title": destinationPageTitle,
			"space": {
				"key": destinationSpaceKey
			},
			"body": {
				"storage": {
					"value": content,
					"representation": "storage"
				}
			}
		};
		return httpClient.post({
			uri: '/rest/api/content',
			json: true,
			body: page_copy
		})
	});

 

Space Scripts

List spaces

see Space List Example

Create space

// Create a space
// WARNING, since the plugin user has sysadmin rights, the created will not be visible
// even to admin.
// You will need to go to the General Configuration->Space Permissions
// and click "Recover Permissions"
// The confluence REST api doesn't yet cover space or page permissions.
var new_space = {
	"key": "TST",
	"name": "Example space"
};
result.promise = httpClient.post({
	uri: '/rest/api/space',
	json: true,
	body: new_space
});

 

General Scripts

Create comment

// Create a comment
var pageId = 1114116;
var new_comment = {
  "type": "comment",
  "title": "Example Content title",
  "container": {
    "id": pageId,
    "type": "page"
  },
  "body": {
    "storage": {
      "value": "<p>This is a comment</p>",
      "representation": "storage"
    }
  }
};
result.promise = httpClient.post({
  uri: '/rest/api/content',
  json: true,
  body: new_comment
});

List blog

// from https://developer.atlassian.com/confdev/confluence-rest-api/confluence-rest-api-examples
result.promise = httpClient.get("/rest/api/content?type=blogpost&start=0&limit=10&expand=space,history,body.view,metadata.labels");

CQL

/* Get all content created by CloudSCript 
see https://developer.atlassian.com/confdev/confluence-rest-api/advanced-searching-using-cql
*/
result.promise = httpClient.get('/rest/api/content/search?cql=creator=currentUser()');
  • No labels
Write a comment…