Scripting administrative tasks with PortalPy

PortalPy is a Python 2.7 module that you can use to script common administrative tasks against your own portal. Python scripts that leverage PortalPy can be executed from any machine with access to your portal as long as your machine is configured to run Python 2.7 (required).

This topic provides a basic introduction to the PortalPy module, includes several samples, and instructions to get you started. Using PortalPy requires Python scripting skills, but provides the most flexible way to truly automate the administration of your portal. With PortalPy, you can automate all the workflows offered by the command line utilities, sample Python scripts, plus much more.

The PortalPy module is available through a public GitHub repository. This resource contains the most up-to-date PortalPy module as well as additional content contributed from the user community. To access this repository, you'll need to create a GitHub account or log in with your existing GitHub account.

Configuring the PortalPy module on your machine

To script administrative tasks with PortalPy, you'll need to set up the PortalPy module on your machine. This can be any machine that has access to your portal. To get started, see the steps below.

Steps:
  1. Download the PortalPy module and extract the contents of the file to a directory on your machine. For example, C:\portalpy.
  2. On the machine hosting the PortalPy module, set an environment variable called PYTHONPATH. Specify the path to the directory where portalpy.py exists. For example, C:\portalpy\portalpy-master. If you need help with this step, consult the Windows product documentation.
  3. Create a file called test.py and place it in the same directory where portalpy.py exists. For example, C:\portalpy\portalpy-master.
  4. Copy the following code into the file and update the URL to match the URL of your portal.
    #!/usr/bin/python
    import portalpy
    url = "https://portal.domain.com/arcgis"
    portal = portalpy.Portal(url)
    print portal.get_version()
    
  5. Save and close the file.
  6. Run test.py from the command line or the Python IDLE environment. The script will print a version number such as 2.3.

Your machine is now configured to use the PortalPy module.

PortalPy module documentation

To review the classes and methods included with PortalPy, see PortalPy module. The topic contains usage samples that can help you learn how to program against the ArcGIS REST API using the PortalPy module.

Example scripts

Common administrative tasks you can script using PortalPy include the following:

Example: List users in a group

portal = PortalPy.Portal(portalUrl, user, password)
resp = portal.get_group_members('67e1761068b7453693a0c68c92a62e2e')
for user in resp['users']:
   print user

Example: Create a group

portal= PortalPy.Portal(portalUrl, user, password)
group_id = portalAdmin.create_group('my group', 'test tag', 'a group to share travel maps')

Example: Delete a user and reassign the user's content to another user

portal= PortalPy.Portal(portalUrl, user, password)
portal.delete_user('amy.user', True, 'bob.user')
5/5/2015