How to create pages from template pages

Description

Situations arise where it would be nice to be able to create a new space pre-populated with pages or to create a new section of an existing space with a standard set of pages. This could be for new projects as they get defined, new design pages, new customers, or new employees' user spaces. Processes or company policies may demand specific permissions and standards. 

The CLI can be used to automate the creation of sets of pages that follow a standard format. First, template (or skeleton) pages are created that represent the page content for the new pages. The new pages will be created from the template pages. Permissions can be added where needed. Replacement text can automatically replace text on the copy. 

Template

We use template as a generic term (not related to Confluence templates). Here a template page is just a normal page that can be edited in the standard way, but is used as the base for a copy.

Server or Cloud

This works with Confluence Server or Cloud.

Self service

For Confluence Server users, a self service setup is possible using Run CLI Actions in Confluence - see How to create pages from template pages - self service

Example - creating a standard personal space

The following creates a personal space for a user (userId) who has a manager (managerId). We assume the template pages are already defined in a space with key: templateSpace. We use replacement variables of the form @xxxx@ so the script can be generalized. See the findReplace paramater used below.

Action script

personal-space.txt
# add personal space
--action addSpace --userId "@userId@"
 
# add admin permission for the user to their own space
--action addpermissions --space "~@userId@" --permissions "SETSPACEPERMISSIONS" --userId @userId@

# add space permissions to allow general user access to personal space
--action addPermissions --space "~@userId@" --group confluence-users --permissions "VIEWSPACE, COMMENT"

# add space permissions to all manager more access
--action addPermissions --space "~@userId@" --userId @managerId@ --permissions "VIEWSPACE, EDITSPACE, EXPORTPAGE, SETPAGEPERMISSIONS, REMOVEPAGE, COMMENT, REMOVECOMMENT, CREATEATTACHMENT, REMOVEATTACHMENT, EXPORTSPACE"

# add pages by copying template pages
--action copyPage --space templateSpace --title @home           --newSpace "~@userId@" --descendents --replace --noConvert
--action copyPage --space templateSpace --title "Public stuff"  --newSpace "~@userId@" --descendents --parent @home --replace --noConvert
--action copyPage --space templateSpace --title "Private stuff" --newSpace "~@userId@" --descendents --parent @home --replace --noConvert
--action copyPage --space templateSpace --title "Manager stuff" --newSpace "~@userId@" --descendents --parent @home --replace --noConvert

# add page permission to protect private section
--action addPermissions --space "~@userId@" --title "Private stuff" --permissions "view, edit" --userId @userId@

# add page permission to protect manager section
--action addPermissions --space "~@userId@" --title "Manager stuff" --permissions "view" --userId @userId@
--action addPermissions --space "~@userId@" --title "Manager stuff" --permissions "view, edit" --userId @managerId@ 

Run the action script

Run script
confluence --action run --file personal-space.txt --continue --findReplace "@userId@:bob,@managerId@tom"

Continue

We use --continue so that all actions will be run even if there are errors on one or more actions. This allows for the script to be corrected and re-run easily.

Example output

Output
Run: --action addSpace --userId bob
Space added with key: ~bob name: bob and url: http://imac2.local:8118/display/~bob
# add admin permission for the user to their own space

Run: --action addpermissions --space "~bob" --permissions "SETSPACEPERMISSIONS" --userId bob
[SETSPACEPERMISSIONS] permissions where added to space: '~bob' for: 'bob'.
# add space permissions to allow general user access to personal space

Run: --action addPermissions --space "~bob" --group confluence-users --permissions "VIEWSPACE, COMMENT"
[VIEWSPACE, COMMENT] permissions where added to space: '~bob' for: 'confluence-users'.
# add space permissions to all manager more access

Run: --action addPermissions --space "~bob" --userId tom --permissions "VIEWSPACE, EDITSPACE, EXPORTPAGE, SETPAGEPERMISSIONS, REMOVEPAGE, COMMENT, REMOVECOMMENT, CREATEATTACHMENT, REMOVEATTACHMENT, EXPORTSPACE"
[VIEWSPACE, EDITSPACE, EXPORTPAGE, SETPAGEPERMISSIONS, REMOVEPAGE, COMMENT, REMOVECOMMENT, CREATEATTACHMENT, REMOVEATTACHMENT, EXPORTSPACE] permissions where added to space: '~bob' for: 'tom'.
# add pages by copying template pages

Run: --action copyPage --space templateSpace --title @home           --newSpace "~bob" --replace --noConvert
Page 'Home' copied to: 'Home' in space ~bob.  Page has id: 94833027

Run: --action copyPage --space templateSpace --title "Public stuff"  --newSpace "~bob" --parent @home --replace --noConvert
Page 'Public stuff' copied to: 'Public stuff' in space ~bob as child of '@home'.  Page has id: 94833032

Run: --action copyPage --space templateSpace --title "Private stuff" --newSpace "~bob" --parent @home --replace --noConvert
Page 'Private stuff' copied to: 'Private stuff' in space ~bob as child of '@home'.  Page has id: 94833033

Run: --action copyPage --space templateSpace --title "Manager stuff" --newSpace "~bob" --parent @home --replace --noConvert
Page 'Manager stuff' copied to: 'Manager stuff' in space ~bob as child of '@home'.  Page has id: 94833034
# add page permission to protect private section

Run: --action addPermissions --space "~bob" --title "Private stuff" --permissions "view, edit" --userId bob
[View, Edit] permissions where added to page: 'Private stuff' in space: '~bob' for: 'bob'.
# add page permission to protect manager section

Run: --action addPermissions --space "~bob" --title "Manager stuff" --permissions "view" --userId bob
[View] permissions where added to page: 'Manager stuff' in space: '~bob' for: 'bob'.

Run: --action addPermissions --space "~bob" --title "Manager stuff" --permissions "view, edit" --userId tom
[View, Edit] permissions where added to page: 'Manager stuff' in space: '~bob' for: 'tom'.

Run completed successfully. 11 actions were successful