Clone Portal users, groups and content 'User' object has no attribute 'users'

829
4
04-26-2021 06:11 PM
cloneportal
New Contributor II

hi all, I'm trying to clone ArcGIS online users to another online, but it gives me an error at the last step when calling the Clone function :
note that these accounts are trial only 21 days.

here is the full code from ESRI and I run it on Jupyter notebook:

ــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ

from arcgis.gis import GIS
from IPython.display import display
import arcgis
import os
print (arcgis.__version__)

source = GIS("https://cloneportaltest.maps.arcgis.com/home""clone_portal", Password )
target = GIS("https://cloneportal2.maps.arcgis.com/home""clone_portal_2", Password )
target_admin_username = 'clone_portal_2'

#!esri_ & !admin
source_users = source.users.search('!esri_ & !admin')
for user in source_users:
    print(user.username + "\t:\t" + str(user.role))
len(source_users)

target_users = target.users.search('!esri_ & !admin & !system_publisher')
for target in target_users:
       print(target.username + "\t:\t" + str(target.role))
print('LenUsers:',len(target_users))

# filter out system and initial administrator accounts
target_users = target.users.search('!esri_ & !admin & !system_publisher')
target_users


for source_user in source_users:
    try:
        target_user = target.users.get(source_user.username)
        if target_user is not None:
            print('Deleting user: ' + target_user.fullName)
            target_user.reassign_to(target_admin_username)
            target_user.delete()
    except:
        print('User {} does not exist in Target Portal'.format(source_user.username))


def copy_user(target_portalsource_user, 'TestPassword@123')
    # See if the user has firstName and lastName properties
    try:
        first_name = source_user.firstName
        last_name = source_user.lastName
    except:
        # if not, split the fullName
        full_name = source_user.fullName
        first_name = full_name.split()[0]
        try:
            last_name = full_name.split()[1]
        except:
            last_name = 'NoLastName'

    try:
        # create user
        target_user = target_portal.users.create(source_user.username, password, first_name, 
                                                 last_name, source_user.email, 
                                                 source_user.description, source_user.role)

        # update user properties
        target_user.update(source_user.access, source_user.preferredView,
                           source_user.description, source_user.tags, 
                           source_user.get_thumbnail_link(),
                           culture=source_user.culture, region=source_user.region)
        return target_user
    
    except Exception as Ex:
        print(str(Ex))
        print("Unable to create user "+ source_user.username)
        return None


for user in source_users:
    print("Creating user: " + user.username)
    copy_user(target, user, 'TestPassword@123')
ــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ
Error
Creating user: clone_portal
Unable to create clone_portal
'NoneType' object has no attribute 'update'
Unable to create user clone_portal
0 Kudos
4 Replies
jcarlson
MVP Esteemed Contributor

Can you elaborate on the code that precedes this? What are the variables "target" and "source_users"?

Is "copy_user" a custom function? What does its definition look like?

- Josh Carlson
Kendall County GIS
0 Kudos
cloneportal
New Contributor II

the post now is edited, kindly review.

0 Kudos
jcarlson
MVP Esteemed Contributor

Great! I'll look it over. But you also might want to check out the code formatting options in the extended post toolbar. Pasting code as regular text is hard to follow, and then there's this little thing:

jcarlson_0-1619520943868.png

jcarlson_1-1619520982572.pngjcarlson_2-1619520996693.png

 

- Josh Carlson
Kendall County GIS
0 Kudos
jcarlson
MVP Esteemed Contributor

Hard to say for sure, but I would guess it's happening when you create the new user. In the docs for that function, you can see:

Returns The user if successfully created, None if unsuccessful.

Your error indicates that target_user = None.

I would add a couple lines to check the value of target_user before proceeding to the update, but also look a little closer at the create function to see what's causing it to fail.

- Josh Carlson
Kendall County GIS
0 Kudos