Run Scripts as Other Users

You can use HAPI to run scripts as other users or an anonymous user!

CQL and HAPI

Where there are CQL statements in example scripts, you can use any CQL statements to modify the scripts to work for you. Visit the CQL Guide for help with CQL.

Run script as another user

The script that follows is used to add a label to every page in a space as another user. To run a script as another user, use a script like this in the Script Console:

Users.runAs("user3") {
    Pages.search("space = DS").each { page ->
        page.addLabels("2023")
    }
}

After running the script, the pages in the space will have 2023 added by user3.

The result of the HAPI script, which is the pages in the space have 2023 added by user3

Customize the above script

You can customize this script by updating the username and label.

Users.runAs("USERNAME") {
    Pages.search("space = DS").each { page ->
        page.addLabels("LABEL")
    }
}

For tips on adding labels, visit Work with Labels.

Run script as anonymous user

To run this script, turn on anonymous access. To anonymously run a script to return all pages that have anonymous access, use a script like this in the Script Console

Users.runAnonymously {
    Pages.search("type = page")
}

After running this script, you will have a list of pages that have anonymous access:

The result of the HAPI script, which is a list of pages with anonymous access

Customize the above script

To customize this script, you can change the type of content (blog, page, space, etc.)  you are searching for in the script.

Users.runAnonymously {
    Pages.search("type = CONTENT")
}

A note about permissions

If you use a script that searches pages by ID, you can run the Pages.getById method even when anonymous access is disabled globally.


Related pages


On this page