Create a new group and add members

Learn how to use the portal service to create a new group in your portal, and add members to the group.

Prerequisites

You need the following for this tutorial:

  1. Account: An ArcGIS Location Platform, ArcGIS Online, or ArcGIS Enterprise account.
  2. Authentication: A basic understanding of API key authentication or User authentication.

Steps

Get the portal URL

To access a portal, you need an ArcGIS account which is associated with an organization that allows you to store and manage your content.

  1. In a web browser, sign in to your portal with your ArcGIS account.
  2. Identify the portal URL from the navigation bar. The base URL should be one of the following:
    • ArcGIS Location Platform: https://www.arcgis.com/sharing/rest
    • ArcGIS Online: https://www.arcgis.com/sharing/rest
    • ArcGIS Enterprise: https://{machine.domain.com}/{webadaptor}/rest

Implement authentication

You need an access token with the correct privileges for in this tutorial.

  1. Go to the Create an API key tutorial and create an API key with the following privilege(s):

    • Privileges

      • Admin privileges > Content > View all
      • Admin privileges > Groups

Create a new group and add members

  1. Add the following import statement to your code.

    main.py
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
    from arcgis.gis import GIS
    
    
  2. Establish a connection to the portal using the GIS() module.

    main.py
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
    from arcgis.gis import GIS
    
    # Authenticate with your ArcGIS Online account
    gis = GIS("https://www.arcgis.com", "your_username", "your_password")
    
    
  3. Create a dictionary of the new group and add it using the create_from_dict method.

    main.py
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
    from arcgis.gis import GIS
    
    # Authenticate with your ArcGIS Online account
    gis = GIS("https://www.arcgis.com", "your_username", "your_password")
    
    # Create a group
    group_properties = {
        "title": "Example Group",
        "description": "This is an example group",
        "tags": "example, group, arcgis python",
        "snippet": "Example group",
        "access": "public"  # Can be 'private', 'org', or 'public'
    }
    
    group = gis.groups.create_from_dict(group_properties)
    print(f"Group created: {group.title}")
    
    
  4. Add existing members to the new group using the add_users method.

    main.py
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
    from arcgis.gis import GIS
    
    # Authenticate with your ArcGIS Online account
    gis = GIS("https://www.arcgis.com", "your_username", "your_password")
    
    # Create a group
    group_properties = {
        "title": "Example Group",
        "description": "This is an example group",
        "tags": "example, group, arcgis python",
        "snippet": "Example group",
        "access": "public"  # Can be 'private', 'org', or 'public'
    }
    
    group = gis.groups.create_from_dict(group_properties)
    print(f"Group created: {group.title}")
    
    # Add an existing member to the group
    usernames_to_add = ["member_username1", "member_username2"]
    result = group.add_users(usernames_to_add)
    
    
  5. Verify members were added to the group.

    main.py
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
    from arcgis.gis import GIS
    
    # Authenticate with your ArcGIS Online account
    gis = GIS("https://www.arcgis.com", "your_username", "your_password")
    
    # Create a group
    group_properties = {
        "title": "Example Group",
        "description": "This is an example group",
        "tags": "example, group, arcgis python",
        "snippet": "Example group",
        "access": "public"  # Can be 'private', 'org', or 'public'
    }
    
    group = gis.groups.create_from_dict(group_properties)
    print(f"Group created: {group.title}")
    
    # Add an existing member to the group
    usernames_to_add = ["member_username1", "member_username2"]
    result = group.add_users(usernames_to_add)
    
    if 'notAdded' in result and result['notAdded']:
        print(f"Failed to add user(s): {result['notAdded']}")
    else:
        print(f"Users added successfully to the group {group.title}")

View the results

If successful, your code will print an output like this to verify that the operation was successful:

Use dark colors for code blocksCopy
1
2
Group created: Example Group
Users added successfully to the group Example Group

What's next

Sign in and access your portal

List items in your portal

Set sharing level for an item

Use the portal service to set the sharing level for an item in your portal.


Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.