Updating Portal user profile with ArcGIS API for python

1616
4
03-18-2021 05:19 PM
Michael_Kraus
Occasional Contributor

Hi there,

I'd like to update something in the user profile - simple eg: change units or language - for all users.  I have some scripts for reading info, but haven't done any updates.  I was thinking of using the "user bio" in the profile to store info such as city and department which would be useful for usage monitoring.

Is writing / updating possible with the API and does anyone have an example they can share for a user profile update.

Thanks,

0 Kudos
4 Replies
FrédéricPRALLY
Esri Contributor

Hi Michael,

Retrieve the following sample to update language.

from arcgis.gis import GIS
gis=GIS("https://arcgis.com", "login", "password")

users= gis.users.search(query='')
##For testing filter user by startswith
users=[user for user in users if user.username.startswith('user')]

##Read original 2 users profil:
for u in range(len(users)):
    print('Original profil: lastName: {0}, units: {1}, culture: {2}, region: {3}'.format(users[u].lastName, users[u].units, users[u].culture, users[u].region))

#As result, we have:
#Original profil: lastName: demo2, units: metric, culture: en-US, region: EN
#Original profil: lastName: demo3, units: metric, culture: en-US, region: EN

##Write user profil:
for u in range(len(users)):
    users[u].update(culture='fr-FR', region='FR')


##Read modified 2 users profil:
for u in range(len(users)):
    print('Modified profil: lastName: {0}, units: {1}, culture: {2}, region: {3}'.format(users[u].lastName, users[u].units, users[u].culture, users[u].region))

#As result, we have:
#Modified profil: lastName: demo2, units: metric, culture: fr-FR, region: FR
#Modified profil: lastName: demo3, units: metric, culture: fr-FR, region: FR


According the doc it seems you cannot update units from user profile. See link below https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#user

Extract :
update(access=None, preferred_view=None, description=None, tags=None, thumbnail=None, fullname=None, email=None, culture=None, region=None, first_name=None, last_name=None, security_question=None, security_answer=None)

Hope this help you,

Fred

0 Kudos
JohnMcGlynn
Occasional Contributor

I have the same problem - updating the security question and answer.

 

    # Process users
    upd_users = ["DaveG","PeterF"]
    try:
        count = 0
        errorcount = 0
        for upuser in upd_users:
            print (upuser)
            exusr = source.users.search(query=upuser)
            if len(exusr) > 0:
                try:
                    # update the user
                    for u in range (len(exusr)):
                        print(exusr[u].email) # Test
                        successor = exusr[u].update(description='Here')

                        print (f"{u.username} updated: {successor}")
                        if successor: count +=1
                        break
                except Exception as ex:
                    print ("ERROR: {},{}".format(u.username,ex))
                    errorcount += 1

                
    finally:
       print ("Completed {} updated. {} don't exist.".format(str(count), str(errorcount)))

 

As a test, updating the description I get the error:

ERROR: DaveG,Expecting value: line 1 column 1 (char 0)

Can someone point to the error I'm causing?

Thanks

0 Kudos
JohnMcGlynn
Occasional Contributor

A bit more detail on the error: 

Traceback (most recent call last):
File "C:\Work\Python\User_Management\update_security_question.py", line 70, in <module>
main()
File "C:\Work\Python\User_Management\update_security_question.py", line 60, in main
exusr[u].update(description='Here')
File "C:\Users\mcglynnj\AppData\Local\Programs\Python\Python39\lib\site-packages\arcgis\gis\__init__.py", line 7723, in update
ret = self._gis._con.post(path=url,
File "C:\Users\mcglynnj\AppData\Local\Programs\Python\Python39\lib\site-packages\arcgis\gis\_impl\_con\_connection.py", line 716, in post
return self._handle_response(resp=resp,
File "C:\Users\mcglynnj\AppData\Local\Programs\Python\Python39\lib\site-packages\arcgis\gis\_impl\_con\_connection.py", line 504, in _handle_response
data = resp.json()
File "C:\Users\mcglynnj\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\models.py", line 900, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\mcglynnj\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\mcglynnj\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\mcglynnj\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Completed 0 updated. 0 don't exist.

John

0 Kudos
MiriEshel1
New Contributor II

Hi everyone,

I've tried to update culture and region:
for user in target_users:
        user.update(culture='he', region='IL')
It looks like it runs OK but it didn't update anything and it stayed English as before.
Why?


Thanks a lot,

Miri

0 Kudos