Cannot update service definition of feature layer view to hide fields

1370
3
Jump to solution
09-16-2021 03:53 PM
EvanThoms1
New Contributor III

I am following the instructions here (https://support.esri.com/en/technical-article/000020083) to create a hosted feature layer view and set the definition. I am able to change items such as name, viewDefinitionQuery, and drawingInfo, but I have been unsuccessful in setting the list of fields to be available (which I think you do by updating the "fields" array in the definition? not entirely sure). After supplying a dictionary to the update_definition method I get a JSON response of 'success' although the fields have not been changed.  

 

# after getting the original hosted feature layer, I make a feature layer collection
source_flc = FeatureLayerCollection.fromitem(source_item)

# make a view from the feature layer collection
new_view = source_flc.manager.create_view(name="testview")

# search for the new view (per instructions at url above but it also looks like you can
# just pick up new_view? Is there a difference?
view_search = gis.content.search("testview")[0]
# and why do the directions have you make another feature layer collection?
# I have tried it this way as well as setting service_layer to view_search.layers[0] 
# and new_view.layers[0]. Both work.
view_flc = FeatureLayerCollection.fromitem(view_search)
service_layer = view_flc.layers[0]

# in this case, I copied the fields I wanted from the JSON view of the 
# hosted feature layer view properties into one big string, but I 
# have also tried a python list and a JSON object
fields = '''[
{
"name" : "OBJECTID",
"type" : "esriFieldTypeOID",
"alias" : "OBJECTID",
"sqlType" : "sqlTypeOther",
"nullable" : false,
"editable" : false,
"domain" : null,
"defaultValue" : null
},
{
"name" : "AGDB_ID",
"type" : "esriFieldTypeInteger",
"alias" : "AGDB_ID",
"sqlType" : "sqlTypeOther",
"nullable" : true,
"editable" : true,
"domain" : null,
"defaultValue" : null
},
{
"name" : "LAB_ID",
"type" : "esriFieldTypeString",
"alias" : "LAB_ID",
"sqlType" : "sqlTypeOther",
"length" : 255,
"nullable" : true,
"editable" : true,
"domain" : null,
"defaultValue" : null
},
{
"name" : "PREV_LAB_ID_1",
"type" : "esriFieldTypeString",
"alias" : "PREV_LAB_ID_1",
"sqlType" : "sqlTypeOther",
"length" : 255,
"nullable" : true,
"editable" : true,
"domain" : null,
"defaultValue" : null
}]'''

# create the update dictionary, adding the keys "name" and "fields"
update_dict = {"name" : "foobar", "fields": fields}
service_layer.manager.update_definition(update_dict)

# have to search for the layer properties again after setting them
# to see what has changed
gis.content.search("testview")[0].layers[0].properties

 

At this point I can see that name has been changed, but all of the original fields are included in the definition

0 Kudos
1 Solution

Accepted Solutions
MehdiPira1
Esri Contributor

@EvanThoms1 ,

You must use the right format. By default, all fields are visible and you need to hide the ones you don't want. 

Use the following dictionary, here the ISO_SUB, Creator and NAME fields will be invisible:

update_dict = {"fields" : [

  {

     "name" : "ISO_SUB",

    "visible" : False

  },
    {

     "name" : "Creator",

    "visible" : False

  },
    {

     "name" : "NAME",

    "visible" : False

  },
    

]}

 I hope that helps.

cheers

Mehdi

View solution in original post

0 Kudos
3 Replies
MehdiPira1
Esri Contributor

@EvanThoms1 ,

You must use the right format. By default, all fields are visible and you need to hide the ones you don't want. 

Use the following dictionary, here the ISO_SUB, Creator and NAME fields will be invisible:

update_dict = {"fields" : [

  {

     "name" : "ISO_SUB",

    "visible" : False

  },
    {

     "name" : "Creator",

    "visible" : False

  },
    {

     "name" : "NAME",

    "visible" : False

  },
    

]}

 I hope that helps.

cheers

Mehdi

0 Kudos
EvanThoms1
New Contributor III

Oh my gosh! So simple.

This was hard to suss out because, while I read about the "visible" key in example 6 at https://developers.arcgis.com/rest/services-reference/enterprise/update-definition-feature-layer-.ht... and saw it in the data posted to the updateDefinition endpoint when I set the visibility manually in AGOL, it doesn't appear in the JSON feature layer service definition when viewed in AGOL. It's not like updating the values of other keys or even adding keys like viewDefinitionQuery. It's evaluated by the update method which then writes out which fields are available.

Thank you!

0 Kudos
MehdiPira1
Esri Contributor

@EvanThoms1 ,

Glad that helped!

0 Kudos