AnsweredAssumed Answered

Updating Feature Service Coded Value Domain with ArcGIS API for Python

Question asked by jvw122 on Mar 24, 2018
Latest reply on Mar 27, 2018 by rvburton

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: Change Domain in ArcGIS Online via ArcGIS Pro.

 

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.

Outcomes