Select to view content in your preferred language

Is it possible to get a list of outside organization groups shared with my organization members.

793
3
08-14-2025 09:32 AM
Labels (1)
ClaytonBurns2
Occasional Contributor

When using the gis.group.search() function it only returns groups that are owned by members of my organization. As an organization admin I'd like to see all the groups that are owned by members of other organizations, but that my members have shared access to. Similarly to how the filter in ArcGIS Online Groups tab currently works. Is this possible within the ArcGIS Python API? I can't find anything in particular in the modules documentation that shows how it can be done.

https://developers.arcgis.com/python/latest/api-reference/arcgis.gis.toc.html#group

ClaytonBurns2_0-1755189119768.png

 

0 Kudos
3 Replies
Clubdebambos
MVP Regular Contributor

Hi @ClaytonBurns2.,

I was never able to figure that out myself and the API documentation is outdated (print the python help as shown in the snippet below to see). You can however invoke the REST API as per below. Code commented. Let me know if anything needs clarification. 

from arcgis.gis import GIS

## access ArcGIS Online
agol = GIS("home")

## your orgnanisation's ID
orgId = agol.properties.user.orgId

## the username you want check has access to what outside groups
username = "USERNAME"

## use the REST API URL and get the return as JSON
groups = agol._con.get("{0}/sharing/rest/community/groups?searchUserAccess=groupMember&searchUserName={1}&q=-orgid:{2}&f=json".format(agol.url, username, orgId))

## print the groups variable to see what is returned
##print(groups)

# print out some infor for each group
for group in groups["results"]:
    print(group["title"], group["id"], group["owner"])

## if you want to see the groups search syntax (differs from docs)
## there is an outside_org param but never got it to work properly
##print(help(agol.groups.search))

All the best,

Glen

~ learn.finaldraftmapping.com
0 Kudos
AdamMesser1
Regular Contributor

@ClaytonBurns2 The python api used to work for this. Based on my errors I saw in our code that started around the Feb 2025 update, I believe there must be a bug that caused this to start failing with only organization groups, not non-organization groups, to be returned. Honestly, I got different results using the python api UserObject.groups function and the code above from @Clubdebambos . If you use the Rest API code below it seems to get what you are looking for. However, you will have to compare the results to your own organization groups.

import requests

def generate_token(username, password, portal_url):
    """Generate a token for the given user."""
    url = f"{portal_url}/sharing/rest/generateToken"
    params = {
        'username': username,
        'password': password,
        'client': 'referer',
        'referer': portal_url,
        'expiration': 60,
        'f': 'json'
    }
    response = requests.post(url, data=params)
    response_json = response.json()
    if 'token' in response_json:
        return response_json['token']
    else:
        raise Exception(f"Unable to get token: {response_json}")

def get_user_groups(username, portal_url, token):
    """Get a list of groups the user belongs to."""
    url = f"{portal_url}/sharing/rest/community/users/{username}"
    params = {
        'f': 'json',
        'token': token
    }
    response = requests.get(url, params=params)
    response_json = response.json()
    # Returns a 'groups' property with a list of group info
    return response_json.get('groups', [])

# Usage
portal_url = BASE_URL  # or your Portal instance base URL
username = AUTHENTICATING_USERNAME
password = PASSWORD
username_tosearch = SEARCH_USERNAME

token = generate_token(username, password, portal_url)
user_groups = get_user_groups(username_tosearch , portal_url, token)

print(f"User '{username_tosearch }' belongs to the following '{str(len(user_groups))}' groups:")
for group in user_groups:
    print(f"- {group.get('title')} (ID: {group.get('id')})")

 

ClaytonBurns2
Occasional Contributor

Thank you Adam, I'll give this a shot in the next week and let you know what I find. Appreciate the advice!

0 Kudos