How to query and set user first and last names in Portal

1227
3
Jump to solution
05-30-2018 01:34 PM
deactivated-user-ChrisBeaudett
New Contributor III

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?  

0 Kudos
1 Solution

Accepted Solutions
JohnYaist1
Esri Contributor

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))‍‍‍‍‍‍‍‍‍‍

View solution in original post

3 Replies
JohnYaist1
Esri Contributor

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))‍‍‍‍‍‍‍‍‍‍
deactivated-user-ChrisBeaudett
New Contributor III

This is exactly what I needed.  Thanks John Yaist‌!

0 Kudos
SeanSweeney
New Contributor III

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

0 Kudos