Export gis.user.groups from portal

1553
5
Jump to solution
06-12-2019 06:31 AM
ShukwaunCheung
New Contributor

Hi everbody,

I want to export from gis.user.search() the json array "groups".

Here some Information about the "groups": arcgis.gis module — arcgis 1.6.1 documentation 

How can I see the groups from user?

Thanks .

from arcgis.gis import GIS
import arcgis
import json

gis = GIS("url_to_portal", "admin_user", "password")

user_accounts = gis.users.search()
group = gis.groups.search()

for user in (str(user_accounts.groups)):
    liste = {}
    user = json.loads(user_accounts.groups)
    print (user)
   
else:
    print("isn't working so far")
	
	
	
#Error Msg
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-18-57b3d16d2703> in <module>
     19 
---> 20 for user in (str(user_accounts.groups)):
     21     liste = {}

AttributeError: 'list' object has no attribute 'groups'‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
1 Solution

Accepted Solutions
Clubdebambos
Occasional Contributor III

Hi @chill_gis_dude 

Here's an implementation, code is commented. I have an issue with accessing some users' groups.

from arcgis import GIS
import csv

## connect to agol/portal
agol = GIS("home")

## csv filepath for file creation/overwrite 
csv_filepath = r"path\to\user_groups.csv"

## get a list of user objects
users = agol.users.search(max_users=1000)

## create/overwite csv
with open(csv_filepath, 'w', newline="") as csv_file:
    writer = csv.writer(csv_file)
    ## write the header
    writer.writerow(["username", "email", "groups"])
    ## iterate through users
    for user in users:
        print(user)
        ## write a new row with info per user
        try:
            groups = ",".join([group.title for group in user.groups])
            new_row = [user.username, user.email, groups]
            writer.writerow(new_row)
        ## i have instances where I cant seem to access a users groups
        ## you may or may not encounter this issue
        except:
            new_row = [user.username, "", ""]
            writer.writerow(new_row)
            print("\t FAILED")

## save the file
csv_file.close()

 

~ learn.finaldraftmapping.com

View solution in original post

0 Kudos
5 Replies
Egge-Jan_Pollé
MVP Regular Contributor

Hi Shukwaun Cheung‌,

You might have a look at a Python script which I have published here:

https://community.esri.com/people/EPolle_TensingInternational/blog/2019/03/21/a-quick-overview-of-gr... 

Does this serve your purposes?

Cheers,

Egge-Jan

ShukwaunCheung
New Contributor

Hi Egge-Jan Pollé,

thank you for your help.
I was able to create a CSV with help of your script, in which I could see the users with all available groups. Each user had a zero as a result for each group, which means that this user is not in this group. Later I invited a user to a group, I got a one in the whole table as result. I know that there should be serveral ones. 😕
My aim was to list each user's groups in a column with a comma as seperater.

What I got:

What I got

What I want:

Whats I want

some Ideas?

0 Kudos
chill_gis_dude
New Contributor III

Were you ever able to figure this one out?

0 Kudos
Clubdebambos
Occasional Contributor III

Hi @chill_gis_dude 

Here's an implementation, code is commented. I have an issue with accessing some users' groups.

from arcgis import GIS
import csv

## connect to agol/portal
agol = GIS("home")

## csv filepath for file creation/overwrite 
csv_filepath = r"path\to\user_groups.csv"

## get a list of user objects
users = agol.users.search(max_users=1000)

## create/overwite csv
with open(csv_filepath, 'w', newline="") as csv_file:
    writer = csv.writer(csv_file)
    ## write the header
    writer.writerow(["username", "email", "groups"])
    ## iterate through users
    for user in users:
        print(user)
        ## write a new row with info per user
        try:
            groups = ",".join([group.title for group in user.groups])
            new_row = [user.username, user.email, groups]
            writer.writerow(new_row)
        ## i have instances where I cant seem to access a users groups
        ## you may or may not encounter this issue
        except:
            new_row = [user.username, "", ""]
            writer.writerow(new_row)
            print("\t FAILED")

## save the file
csv_file.close()

 

~ learn.finaldraftmapping.com
0 Kudos
chill_gis_dude
New Contributor III

Thank you, this was incredibly helpful!

0 Kudos