Updating Feature Service Coded Value Domain with ArcGIS API for Python

3558
3
Jump to solution
03-24-2018 03:32 PM
James_Whitacre
Occasional Contributor

I am trying to update an attribute coded value domain using the ArcGIS API for Python using the Update Servcie Definition method. I am an admin for my AGO org.

Here is my code so far (if you are testing it, you will need to used your own parameters):

import arcgis

username = 'username'

gis = arcgis.gis.GIS('https://www.arcgis.com', username)


url = 'https://domain.com/arcgis/rest/services/Service_name/FeatureServer/0'

field_name = 'Field_Name'

cv_code = 1

cv_name = 'Code Name'


fs = arcgis.features.FeatureLayer(url, gis)

update_dict = {"fields": [{"name": field_name, "domain": {"codedValues": [{"name": cv_name, "code": cv_code}]}}]}

fs.manager.update_definition(update_dict)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

When I run in my Jupyter Notebook, I get the error below. What am I missing? Is my 'update_dict' not correct (I ma inclined to think it isn't)? Is there a better way to script this out? Thanks.

Error:

Unable to update feature service layer definition.
Invalid definition for ''.
Invalid definition for System.Collections.Generic.List`1[ESRI.ArcGIS.SDS.FieldInfo]
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-7-4fbf02d770c9> in <module>()
----> 1 fs.manager.update_definition(update_dict)

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\managers.py in update_definition(self, json_dict)
    981         u_url = self._url + "/updateDefinition"
    982 
--> 983         res = self._con.post(u_url, params)
    984         self.refresh()
    985         return res

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\_impl\connection.py in post(self, path, postdata, files, ssl, compress, is_retry, use_ordered_dict, add_token, verify_cert, token, try_json, out_folder, file_name, force_bytes, add_headers)
   1151                                          verify_cert=verify_cert, is_retry=True)
   1152 
-> 1153                 self._handle_json_error(resp_json['error'], errorcode)
   1154                 return None
   1155         except AttributeError:

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\_impl\connection.py in _handle_json_error(self, error, errorcode)
   1171 
   1172         errormessage = errormessage + "\n(Error Code: " + str(errorcode) +")"
-> 1173         raise RuntimeError(errormessage)
   1174 
   1175 class _StrictURLopener(request.FancyURLopener):

RuntimeError: Unable to update feature service layer definition.
Invalid definition for ''.
Invalid definition for System.Collections.Generic.List`1[ESRI.ArcGIS.SDS.FieldInfo]
(Error Code: 400)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Follow up:

Ok, in investigating further, I found this idea: https://community.esri.com/ideas/12962.

Great idea, and a good solution for my immediate issue. However, I am foreseeing a need in my greater use case to automate other tasks in conjunction with this task or to have a set of tools (either Python or eventually shipped with ArcGIS Pro) that allows for editing/updating other service definition properties. I know this can be done with the ArcGIS API for Python, but it seems cumbersome when going back and forth between JSON and Python. I also don't have a lot of experience with working with JSON in Python, so that could be one of my hindrances here.

Any help is still greatly appreciated.

1 Solution

Accepted Solutions
RandyBurton
MVP Regular Contributor

You may need to supply the domain name in your update_dict.

{
  "fields": [{
    "name": "Field_Name",
    "domain": {
      "type": "codedValue",
      "name": "Domain_Name",
      "codedValues": [{
        "name": "Code Name",
        "code": 1
      }]
    }
  }]
}

View solution in original post

3 Replies
RandyBurton
MVP Regular Contributor

You may need to supply the domain name in your update_dict.

{
  "fields": [{
    "name": "Field_Name",
    "domain": {
      "type": "codedValue",
      "name": "Domain_Name",
      "codedValues": [{
        "name": "Code Name",
        "code": 1
      }]
    }
  }]
}
FPCWAGIS_Admin
New Contributor III

I know this thread is a little old now, but it's the closest I've found in my searching. Did you (or anyone) have any luck with getting Domain values in an ArcOnline feature service to update based on an external list via python (or another automated method)?

My requirement is to have a pick list (Domain) of Staff Names or Contractor Names updated on a regular basis (i.e. Weekly) from an external source so that the list is representative of our staff/contractors at all times. 

0 Kudos
RandyBurton
MVP Regular Contributor

I have used the  Update Definition (Feature Layer) of the REST API to modify domains (see example 6).  I think adding to the domain list works best, as deleting a domain value may leave some invalid values in the field.  Instead of deleting a domain, I would recommend changing the name/description to indicate it is out of date.  If the field needing the domain update is used for symbology, you will also need to edit and update the "types" section of the JSON file.

I have not used the ArcGIS API for Python for this purpose.  For this, I would explore the  update_definition section of the API documentation.