Skip To Content

Scripting administrative tasks with PortalPy

In this topic

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 installed with the software and located in the tools directory. For example, <Portal for ArcGIS installation directory>/tools/portalpy. The module is also available through a public GitHub repository. This resource contains the most current PortalPy module as well as additional content contributed from the user community. To access this repository, 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.

  1. On the machine installed with Portal for ArcGIS, browse to <Portal for ArcGIS installation directory>/tools and copy the entire portalpy folder to a local directory on your machine. For example, /home/admin/portalpy. If the machine you want to run the PortalPy module on is the machine installed with Portal for ArcGIS, you can skip this step.
  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, export PYTHONPATH=$PYTHONPATH:/home/admin/portalpy.
  3. Create a file called test.py and place it in the same directory where portalpy.py exists. For example, /home/admin/portalpy.
  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. The script prints a version number such as 3.2.

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')