Create Hosted Feature Layer coded value attribute list using ArcGIS API for Python

930
4
Jump to solution
12-13-2023 12:37 PM
Labels (1)
EmmaSchultz1
Occasional Contributor

Hi Community, 

I am having issues with trying to create a coded attribute list for a field in a hosted feature layer using python. Here is some background information and some solutions in related posts that I have tried to no avail. 

The layer was published directly to AGOL from an .xlsx spreadsheet. I have a script that overwrites the hosted feature layer. When overwriting the hosted feature layer, some of the properties are reset which need to be addressed in the code: Need to make the layer editable again, and need to re-create the attribute list for a field. I need the attribute list to use radio buttons in Field Maps. 

Here is the code, everything works great (layer overwrites, capabilities are updated), but when I try to update the feature layer collection definition to create the coded value domain list, it returns: 

{'success': True}

 But nothing is changed. No error message or anything. 

 

Full code

#Import Modules
from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
print('Modules Imported')

#AGOL variables assigned
user: str = '<username'
pssw = '<password>'
test_itemid = '<itemid>'
test_upload = '<filepath.xlsx>'


#Coded value variables
low_code = 'Low'
low_name = 'Low'
med_code = 'Med'
med_name = 'Med'

#Get the hosted feature layer collection
gis = GIS('<portal_name>', user, pssw)
test_item= gis.content.get(test_itemid)
test_flc = FeatureLayerCollection.fromitem(test_item)

#Overwrite layer and update capabilities with a dictionary
test_flc.manager.overwrite(test_upload)
update_dict1 = {"hasStaticData":False, "capabilities": "Create,Delete,Query,Update,Editing"}
test_flc.manager.update_definition(update_dict1)
print('Updated definition successfully')

#Create a dictionary to use to update the domain list
update_dict2 = {'fields': [{'name': 'Ranking', 'domain': {'type': 'codedValue',
'name': 'Ranking_Domain',
'codedValues': [{'name': med_name,'code': med_code},
{'name': low_name,'code': low_code}]
}}]}

test_flc.manager.update_definition(update_dict2)

 

Things I have tried

1) Tried to create a list without Python in AGOL, then tried updating the coded value list to include an additional value. When I create a list in AGOL, then return the domain list it returns the following: 

 [{
"type": "codedValue",
"name": "Sheet1_Ranking_itemid",
"codedValues": [
{
"name": "Low",
"code": " Low"
},
{
"name": "Med",
"code": " Med "
}
]
}]

I then tried to use this default domain name in a script to add another coded value to the list, where it says it was successful but nothing updates. This test was to see if when I use an existing domain name if it would work, as described in the following: Solved: Re: Updating Feature Service Coded Value Domain wi... - Esri Community

The process for updating an already existing domain list in AGOL is also described in here, but they use ArcGIS Pro to update: How To: Edit Read-Only Domains of a Hosted Feature Layer Using Python in ArcGIS Pro (esri.com)

2) Views are locked: I then found the following piece: Solved: Re: Can't edit Hosted Feature Layer Domains in Arc... - Esri Community

It describes an issue where the feature layer views parameters need to be changed, but I could not access the viewLayerDefinition as my layer does not have views. 

Solutions?

I am wondering if the issue resides in

1) limitations from publishing the hosted feature layer right to AGOL instead of using Pro. There are a number of questions/ solutions in other posts, but most of them are publishing from Pro.

2) The layer did not have coded value domains when first published - does the layer need a domain list from the beginning in order to update? 

3) Is there another parameter/ capability that I need to update in order to enable a domain list on the layer? 

 

Any help would be greatly appreciated! I have been trying this for a few weeks now without a solution. 

 

 

0 Kudos
1 Solution

Accepted Solutions
Clubdebambos
Frequent Contributor

Hello @EmmaSchultz1 

Domains are not a property of a FeatureLayerCollection, they are the property of a Field in a FeatureLayer so you need to access the feature layer and use the FeatureLayerManager uppdate_definition(). Try the below...

fl = test_item.layers[0] ## ASSUMING ONLY ONE LAYER & ACCESSING IT

field_dict = {
    'name': 'Ranking', ## THE NAME OF THE FIELD TO UPDATE
    'domain': {
        'type': 'codedValue',
        'name': 'Ranking_Domain',
        'codedValues': [
            {
                'name': med_name,
                'code': med_code
            },
            {
                'name': low_name,
                'code': low_code
            }
        ]
    }
}

print(fl.manager.update_definition({"fields":[field_dict]}))

 

~ learn.finaldraftmapping.com

View solution in original post

4 Replies
Clubdebambos
Frequent Contributor

Hello @EmmaSchultz1 

Domains are not a property of a FeatureLayerCollection, they are the property of a Field in a FeatureLayer so you need to access the feature layer and use the FeatureLayerManager uppdate_definition(). Try the below...

fl = test_item.layers[0] ## ASSUMING ONLY ONE LAYER & ACCESSING IT

field_dict = {
    'name': 'Ranking', ## THE NAME OF THE FIELD TO UPDATE
    'domain': {
        'type': 'codedValue',
        'name': 'Ranking_Domain',
        'codedValues': [
            {
                'name': med_name,
                'code': med_code
            },
            {
                'name': low_name,
                'code': low_code
            }
        ]
    }
}

print(fl.manager.update_definition({"fields":[field_dict]}))

 

~ learn.finaldraftmapping.com
EmmaSchultz1
Occasional Contributor

Hi @Clubdebambos 

First of all, if I could give you 10 Kudos I would. 

Again, the difference between FeatureLayer and FeatureLayerCollection has got me! I have read and read documentation on the differences and when to use each but am continually confused. I think my mind was stuck on the fact that to update layer capabilities - "edit, add, delete" etc. this uses the FeatureLayerCollection and creating a domain list in my head is similar to creating a domain field list. I See now that accessing attributes is based on the FeatureLayer and not FeatureLayerCollection. 

This has prompt me to do more reading on the differences to wrap my head around this. 

I cannot thank you enough for the help. 

Emma 

MichaelKohler
Frequent Contributor

Hi @Clubdebambos 

I'm trying to use this solution to add values to a domain and the success shows true but the domain doesn't get updated. There is a single value in the domain and I'm testing to add more values. I got the domain name from the feature definition

 

DAZ = gis.content.search("************", "Feature Layer")
lyr = DAZ[0]
fl = FeatureLayerCollection.fromitem(lyr)
field_dict = {
    'name': 'Team', ## THE NAME OF THE FIELD TO UPDATE
    'domain': {
        'type': 'codedValue',
        'name': 'Damage Assessment Zones_Team_2ba4d3f3-73df-4405-87b9-713fd622dac4',
        'codedValues': [
            {
            "name" : "Team 1", 
            "code" : "Team 1"
          }, 
          {
            "name" : "Team 2", 
            "code" : "Team 2"
          }
        ]
    }
}
print(fl.manager.update_definition({"fields":[field_dict]}))

no worries because I'll just update the definition on the admin page but if there are any ideas, I'd appreciate any help.

 

 

0 Kudos
Clubdebambos
Frequent Contributor

Hi @MichaelKohler 

Similar issue as the original post, you are trying to update a FLC when the domain information is in the FeatureLayer.

Your workflow would look similar to the below....

 

## search always returns a list of Item objects
DAZ = gis.content.search("************", "Feature Layer")

## accessing the first item of the search list
## assuming this is a Feature Service
fs = DAZ[0]

## accessing the feature layer of interest from the item
## the item must be a Feature Service
## accessing the first layer in the service with [0]
fl = fs.layers[0]

field_dict = {
    'name': 'Team', ## THE NAME OF THE FIELD TO UPDATE
    'domain': {
        'type': 'codedValue',
        'name': 'Damage Assessment Zones_Team_2ba4d3f3-73df-4405-87b9-713fd622dac4',
        'codedValues': [
            {
            "name" : "Team 1", 
            "code" : "Team 1"
          }, 
          {
            "name" : "Team 2", 
            "code" : "Team 2"
          }
        ]
    }
}
print(fl.manager.update_definition({"fields":[field_dict]}))

 

 

~ learn.finaldraftmapping.com
0 Kudos