I'm new to the ArcGIS Python API and Jupyter Notebooks so I may just be doing something wrong, but I'd like to update users in AGOL with their last names (since auto-register is not adding last names) but even trying to query for first and last names gives the error:
AttributeError: 'User' object has no attribute 'firstName'
This is the code in my notebook:
import arcgis from arcgis.gis import GIS from IPython.display import display import getpass password = getpass.getpass("Enter Password: ") gis = GIS("http://agol.maps.arcgis.com", "Admin User", password) user_accounts = gis.users.search('',max_users=700) for a in accounts: print (a.firstName)
I've tried a bunch of variations on 'firstName', e.g. 'firstname', 'first_name', 'FirstName', 'firstname', etc. but always get the same result. According to sample code on the Python API Samples page, 'firstName' should work. Also according to the API reference, the User object should have a 'userdict' object...
classarcgis.gis.
User
(gis, username, userdict=None)
...but I can't seem to get that either. But maybe I'm reading that wrong?
I can resolve and print the username fine, i.e. "print (a.username)", so I know I have valid User objects.
I've found online that it isn't until v1.4.1 that we are able to update users with first and last name. I'm not sure what version I'm running locally and also not sure how to find it -- is there a command I can run in Notebook to give me the Python API version number?
I tried running this same code in the online Jupyter Notebook on the Esri Python API 'try it live' site,which I was hoping is the latest version, but I'm getting the same error there.
Any thoughts?
Solved! Go to Solution.
Hi Chris Beaudette -
You can find the ArcGIS API for Python version from the Jupyter notebook with the following code:
import arcgis
arcgis.__version__
If a user doesn't have values for the firstName or lastName attributes, the error you observe is returned. At version 1.4.1, you can use the user.update(first_name='first', last_name='last') function to add that information and then your code would return values. You can get around this by checking for the existence of that attribute:
for user in user_accounts:
try:
print(user.firstName)
except AttributeError:
print("{} has no firstName attribute.".format(user.username))
Hi Chris Beaudette -
You can find the ArcGIS API for Python version from the Jupyter notebook with the following code:
import arcgis
arcgis.__version__
If a user doesn't have values for the firstName or lastName attributes, the error you observe is returned. At version 1.4.1, you can use the user.update(first_name='first', last_name='last') function to add that information and then your code would return values. You can get around this by checking for the existence of that attribute:
for user in user_accounts:
try:
print(user.firstName)
except AttributeError:
print("{} has no firstName attribute.".format(user.username))
This is exactly what I needed. Thanks John Yaist!
John,
Why do some user objects in AGOL have firstName and lastName and some do not? I can't find a common thread between these users. In both case their display profile shows both first and last name.
Is adding the attributes like you suggest the accepted way of fixing this problem?
Thanks