Accessing and managing groups

Groups are collaborative spaces in your GIS where your users share related content. Groups support metadata allowing you to customize them as per their theme. You can add users to a group with different group related privileges. Refer to the topic on groups to learn more about them.

In the gis module, groups are represented using instances of Group class. Similar to Items, Users, Roles, an instance of a resource manager class called GroupManager is used to create, search and provide you access to Group objects. As with other resource manager objects, you would not create an instance of GroupManager using its constructor, but access it from the groups property of the GIS object.

In this guide, we will observe:

Searching for groups

You can search for groups by using the search() method of GroupManager object. The search() method accepts standard ArcGIS REST API queries. To illustrate this better, let us connect to ArcGIS Online anonymously and search for public groups that have the word 'water' in the title.

from arcgis.gis import GIS
ago_gis = GIS()
urban_groups = ago_gis.groups.search('title:water', max_groups=15)
urban_groups
[<Group title:" Mexican Water Chapter COVID19 Hub Content" owner:wy_mex>,
 <Group title:""High Water Mark" Initiative" owner:risson>,
 <Group title:"(S)ken je sloot Waternet" owner:sjoerd.hoekstra@tauw.com_tauw>,
 <Group title:"(T) Lewisboro - Lake Waccabuc Water Quality Survey" owner:gdesantis_Barton>,
 <Group title:"03383727 World Water Test NeSc-Content" owner:riwo4dechsupport>,
 <Group title:"100415487_001_Thames_Water_Charvil" owner:Edward.Wibberley@mottmac.com_mottmac_EMA>,
 <Group title:"11134352_SUSQTWP_STORM_WATER" owner:risson>,
 <Group title:"2.2: Inhoud Geodata portaal - Waterschap Aa en Maas" owner:aaenmaas>,
 <Group title:"2013 Audubon Center Water Quality Study" owner:williamtraddhymes>,
 <Group title:"2013 Audubon Water Quality" owner:andcardoza2013>,
 <Group title:"2013 Audubon Water Quality" owner:nicboucher12>,
 <Group title:"2013 Audubon Water Quality Study" owner:kylwalker>,
 <Group title:"2013 Audubon Water Quality Study" owner:bdhsaudubon>,
 <Group title:"2013 Audubon Water Quality Study" owner:Bradlynn>,
 <Group title:"2017 Esri Water Technical Workshops" owner:Deilson_EsriWater>]

The search() method returns a list of objects of type arcgis.gis.Group. When using the Jupyter notebook environment, Group objects can be represented in rich HTML with their thumbnail and metadata information.

urban_groups[1]
"High Water Mark" Initiative

Summary: Maps, Applications, and related GIS data
Description: None
Owner: risson
Created: August 27, 2018

You can expand the query string to include multiple fields on a group such as owner, description etc. Let us look for groups created and owned by account esri.

esri_owned_groups = ago_gis.groups.search(query='owner:esri and description:basemaps', max_groups=15)
esri_owned_groups
[<Group title:"Antarctic Maps" owner:esri>,
 <Group title:"ArcGIS Data and Maps" owner:esri>,
 <Group title:"ArcGIS Online Basemaps" owner:esri>,
 <Group title:"ArcGlobe Basemaps" owner:esri>,
 <Group title:"ArcMap Basemaps" owner:esri>,
 <Group title:"Arctic Maps" owner:esri>,
 <Group title:"China Basemaps for Developers" owner:esri>,
 <Group title:"Community Basemaps" owner:esri>,
 <Group title:"Esri Maps and Data" owner:esri>,
 <Group title:"India Basemaps for Developers" owner:esri>,
 <Group title:"Living Atlas: Community Maps" owner:esri>,
 <Group title:"OpenStreetMap Vector Basemap" owner:esri>,
 <Group title:"Places" owner:esri>,
 <Group title:"Tiled Basemaps" owner:esri>,
 <Group title:"Vector Basemaps" owner:esri>]

Properties of a group

You can query the metadata and related information about a group by querying the properties of its corresponding Group object:

esri_group1 = esri_owned_groups[0]
esri_group1.access
'public'

Let us print some more of the properties of this group

import time
print(esri_group1.groupid, esri_group1.isFav, esri_group1.isInvitationOnly)
print(esri_group1.owner)
time.localtime(esri_group1.created/1000)
6cd54e197601410e81a2cbcc9c3e1f22 False True
esri
time.struct_time(tm_year=2016, tm_mon=6, tm_mday=23, tm_hour=15, tm_min=49, tm_sec=45, tm_wday=3, tm_yday=175, tm_isdst=1)

Once you know the id of a group, you can access it using the get() method of the GroupManager object:

ago_gis.groups.get(esri_group1.groupid)
Antarctic Maps

Summary: This group contains a collection of ready-to-use polar web maps and layers for the Antarctic region that have been published by Esri.
Description:
This group contains a collection of web maps and map layers for the Antarctic region that have been published by Esri.  These maps and layers are also part of the Living Atlas of the World, the foremost collection of authoritative, ready-to-use global geographic information.


Using Web Maps

The ready-to-use maps in this collection are based on the polar projection WGS_1984_Antarctic_Polar_Stereographic and can be opened or added to applications that support web maps.  


Using Layers 

To view layers in this group you'll want to add layers to a map that is using the Polar projection of WGS_1984_Antarctic_Polar_Stereographic, for example the Antarctic Imagery basemap. Other polar projections may be used within their useful limits. 


Looking for content maps and layers on the other side of the globe?  The Arctic Maps group is also available.

Owner: esri
Created: June 23, 2016

Creating new groups

You can create new groups by calling the create() method of the GroupManager object. This method is similar to other create methods of ResourceManager objects and returns you an instance of the Group object it created.

Let us create a new group for geocaching enthusiasts in our GIS. Replace the credentials below with that of your org which has appropriate privileges to create a group. To learn more about user types, roles and privileges, click here. To learn more about profiles, see here

# connect to GIS with credentials
gis = GIS(profile="your_online_admin_profile")
# Searching if a group with title 'Recreational geocaching' already exists, if it exists then delete it
for gc_group in gis.groups.search(f"Recreational geocaching AND owner:{gis.users.me.username}"):
    try:
        gc_group.delete()
    except Exception as e:
        pass
geocaching_group = gis.groups.create(title='Recreational geocaching',
                                    tags = 'hobby, geocaching, gps, hide n seek',
                                    description = 'Group to share your landmarks and games',
                                    snippet = 'Share your GPX tracks as feature layers here',
                                    access = 'org',
                                    is_invitation_only = 'False',
                                    thumbnail = r'../../static/img/geocaching.jpeg')
geocaching_group
Recreational geocaching

Summary: Share your GPX tracks as feature layers here
Description: Group to share your landmarks and games
Owner: ArcGISPyAPIBot
Created: September 19, 2023

Sharing content to groups

In an ArcGIS Org, an Item can have 4 privacy levels - private, group, org, everyone. When an Item is shared to a group, all members of a group get to view it. Similarly, when shared to an organization, all authenticated members of the org can view it. When shared to everyone, and if the org permits anonymous access, the item is public and accessible to anyone and does not have to be a logged in user.

First, let us add a CSV containing some points to the GIS.

# fetch your username
username = gis.users.me.username
username
'ArcGISPyAPIBot'

We're going to create a CSV item called Hidden treasures, but before we do, let's ensure the user doesn't already own one in this portal. If they do, we'll delete it so the subsequent code will work.

for csv_item in gis.content.search(f"title:Hidden treasures AND owner:{username}"):
    try:
        csv_item.delete()
    except Exception as e:
        pass
        
# here we'll add the item titled 'Hidden treasures' with certain properties
item_properties = {'title':'Hidden treasures',
                  'tags':['geocaching', 'searching', 'hikes', 'POI'],
                  'snippet':'Points containing treasures for geocaching activity'}
fc_item = gis.content.add(item_properties, data='../../samples/05_content_publishers/data/hidden_treasures_geocaching.csv')
fc_item.access
'private'

Items with a private sharing level can only be shared to groups the item owner has sharing access to. To share an Item to a group, call the share() method of the Item object and pass the comma separated list of Group objects or names of the groups or their IDs.

# let us share it to the group so other enthusiasts can enjoy this map
fc_item.share(groups=[geocaching_group])
{'notSharedWith': [], 'itemId': '538a4c39061043828f5d7a4d2a09c30b'}

Managing your groups

Listing contents of the group

As the first step in managing your groups, let us view the items shared with the group by using the content() method

geocaching_group.content()
[<Item title:"Hidden treasures" type:CSV owner:ArcGISPyAPIBot>]

Adding users to the group

Now our group has sufficient details and content to make it useful for others. Let us add some users. You can either add users using the add_users() method and specify the list of usernames, or if you wish to leave joining to their choice, you can invite them to the group using the invite_users() method.

# let us add 2 users to this group
geocaching_group.add_users(['user_1', 'user_2'])
{'notAdded': []}

The method returns a dictionary with information on which users could not be added. As seen on the guide for managing users, users could have custom privileges preventing them from joining groups. In such occasions, you would get the names of those users in this return list.

Listing users belonging to a group

You can list the users in a group by calling the get_members() method. The method returns a dictionary which contains the owner of the group, and the list of admin and users.

members_of_group = geocaching_group.get_members()
members_of_group
{'owner': 'ArcGISPyAPIBot',
 'admins': ['ArcGISPyAPIBot'],
 'users': ['user_1']}

Removing users from a group

You can remove users from a group by calling the remove_users() method and passing the list of usernames to be removed. The method returns you a dictionary containing those users who cannot be removed.

# get the username of the owner of the group
owner_of_group_username = members_of_group["owner"]

Let's remove a member from the group. We'll return the first user from the users key component of the get_members() output

Note: If there are no users in the group except the owner and the admin then the following cell will give an output 'No users in this group to remove.'

try:
    first_username_of_group = members_of_group["users"][0]
    print(geocaching_group.remove_users([first_username_of_group]))
except IndexError as ie:
    print("No users in this group to remove.")
{'notRemoved': []}
geocaching_group.get_members()
{'owner': 'ArcGISPyAPIBot', 'admins': ['ArcGISPyAPIBot'], 'users': []}

Let's see if we can remove the owner of the group

geocaching_group.remove_users([owner_of_group_username])
{'notRemoved': ['ArcGISPyAPIBot']}

Thus, you cannot remove the owner or admin of the group until you reassign the ownership to another user.

Updating a group

You can update any or all the fields of a group that you specified when creating it. Thus, the update() accepts the same parameters as create(). Let us close the group to members who can be added through invitation only. This prevents users from sending a joining request.

Note: Please note that the group ID cannot be updated.
geocaching_group.isInvitationOnly
False
geocaching_group.update(is_invitation_only=True)
True
geocaching_group.isInvitationOnly
True

Terminating a group

If a group no longer serves the purpose or if its requirements have changed, the owner and members have a few options. They can leave the group by calling the leave() method. When users leave a group, the content they shared with the group will automatically be unshared. The owner of the group or the admin of the organization can delete the group by calling the delete() method.

However if the contents and the group have to be preserved, the group's ownership can be transferred by calling the reassign_to() and specifying the new owner's username.

Note: Only the administrator of the organization can reassign a group to another user.

let us reassign ownership to another user

members_of_group
{'owner': 'ArcGISPyAPIBot',
 'admins': ['ArcGISPyAPIBot'],
 'users': ['user_1']}
first_username = ''
try:
    first_username = members_of_group['users'][0]
except:
    print('No users exist in the group except the owner itself')
    
try:
    print(geocaching_group.reassign_to(target_owner = first_username))
except:
    print('No other user to reassign the group to')
True

if the group got reassigned then we can see that the owner of the group has changed by running the following cell

group_memebers = geocaching_group.get_members()
group_memebers['owner']
'user_1'

if the group got reassigned then we see that the previous owner now becomes a user in the group

geocaching_group.get_members()
{'owner': 'user_1',
 'admins': ['user_1'],
 'users': ['ArcGISPyAPIBot']}

Finally, let us delete this group

geocaching_group.delete()
True

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