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):
<SPAN class="keyword token">import</SPAN> arcgis
username <SPAN class="operator token">=</SPAN> <SPAN class="string token">'username'</SPAN>
gis <SPAN class="operator token">=</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>gis<SPAN class="punctuation token">.</SPAN>GIS<SPAN class="punctuation token">(</SPAN><SPAN class="string token"><SPAN>'</SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fwww.arcgis.com" target="_blank">https://www.arcgis.com</A><SPAN>'</SPAN></SPAN><SPAN class="punctuation token">,</SPAN> username<SPAN class="punctuation token">)</SPAN>
url <SPAN class="operator token">=</SPAN> <SPAN class="string token"><SPAN>'</SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fdomain.com%2Farcgis%2Frest%2Fservices%2FService_name%2FFeatureServer%2F0" target="_blank">https://domain.com/arcgis/rest/services/Service_name/FeatureServer/0</A><SPAN>'</SPAN></SPAN>
field_name <SPAN class="operator token">=</SPAN> <SPAN class="string token">'Field_Name'</SPAN>
cv_code <SPAN class="operator token">=</SPAN> <SPAN class="number token">1</SPAN>
cv_name <SPAN class="operator token">=</SPAN> <SPAN class="string token">'Code Name'</SPAN>
fs <SPAN class="operator token">=</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>features<SPAN class="punctuation token">.</SPAN>FeatureLayer<SPAN class="punctuation token">(</SPAN>url<SPAN class="punctuation token">,</SPAN> gis<SPAN class="punctuation token">)</SPAN>
update_dict <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">"fields"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">{</SPAN><SPAN class="string token">"name"</SPAN><SPAN class="punctuation token">:</SPAN> field_name<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"domain"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">"codedValues"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">{</SPAN><SPAN class="string token">"name"</SPAN><SPAN class="punctuation token">:</SPAN> cv_name<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"code"</SPAN><SPAN class="punctuation token">:</SPAN> cv_code<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">}</SPAN>
fs<SPAN class="punctuation token">.</SPAN>manager<SPAN class="punctuation token">.</SPAN>update_definition<SPAN class="punctuation token">(</SPAN>update_dict<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>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<SPAN class="punctuation token">.</SPAN>
Invalid definition <SPAN class="keyword token">for</SPAN> <SPAN class="string token">''</SPAN><SPAN class="punctuation token">.</SPAN>
Invalid definition <SPAN class="keyword token">for</SPAN> System<SPAN class="punctuation token">.</SPAN>Collections<SPAN class="punctuation token">.</SPAN>Generic<SPAN class="punctuation token">.</SPAN>List`<SPAN class="number token">1</SPAN><SPAN class="punctuation token">[</SPAN>ESRI<SPAN class="punctuation token">.</SPAN>ArcGIS<SPAN class="punctuation token">.</SPAN>SDS<SPAN class="punctuation token">.</SPAN>FieldInfo<SPAN class="punctuation token">]</SPAN>
<SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN>
RuntimeError Traceback <SPAN class="punctuation token">(</SPAN>most recent call last<SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token"><</SPAN>ipython<SPAN class="operator token">-</SPAN>input<SPAN class="number token">-7</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">4fbf02d770c9</SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">in</SPAN> <SPAN class="operator token"><</SPAN>module<SPAN class="operator token">></SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">></SPAN> <SPAN class="number token">1</SPAN> fs<SPAN class="punctuation token">.</SPAN>manager<SPAN class="punctuation token">.</SPAN>update_definition<SPAN class="punctuation token">(</SPAN>update_dict<SPAN class="punctuation token">)</SPAN>
C<SPAN class="punctuation token">:</SPAN>\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro<SPAN class="operator token">-</SPAN>py3\lib\site<SPAN class="operator token">-</SPAN>packages\arcgis\features\managers<SPAN class="punctuation token">.</SPAN>py <SPAN class="keyword token">in</SPAN> update_definition<SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">,</SPAN> json_dict<SPAN class="punctuation token">)</SPAN>
<SPAN class="number token">981</SPAN> u_url <SPAN class="operator token">=</SPAN> self<SPAN class="punctuation token">.</SPAN>_url <SPAN class="operator token">+</SPAN> <SPAN class="string token">"/updateDefinition"</SPAN>
<SPAN class="number token">982</SPAN>
<SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">></SPAN> <SPAN class="number token">983</SPAN> res <SPAN class="operator token">=</SPAN> self<SPAN class="punctuation token">.</SPAN>_con<SPAN class="punctuation token">.</SPAN>post<SPAN class="punctuation token">(</SPAN>u_url<SPAN class="punctuation token">,</SPAN> params<SPAN class="punctuation token">)</SPAN>
<SPAN class="number token">984</SPAN> self<SPAN class="punctuation token">.</SPAN>refresh<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="number token">985</SPAN> <SPAN class="keyword token">return</SPAN> res
C<SPAN class="punctuation token">:</SPAN>\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro<SPAN class="operator token">-</SPAN>py3\lib\site<SPAN class="operator token">-</SPAN>packages\arcgis\_impl\connection<SPAN class="punctuation token">.</SPAN>py <SPAN class="keyword token">in</SPAN> post<SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">,</SPAN> path<SPAN class="punctuation token">,</SPAN> postdata<SPAN class="punctuation token">,</SPAN> files<SPAN class="punctuation token">,</SPAN> ssl<SPAN class="punctuation token">,</SPAN> compress<SPAN class="punctuation token">,</SPAN> is_retry<SPAN class="punctuation token">,</SPAN> use_ordered_dict<SPAN class="punctuation token">,</SPAN> add_token<SPAN class="punctuation token">,</SPAN> verify_cert<SPAN class="punctuation token">,</SPAN> token<SPAN class="punctuation token">,</SPAN> try_json<SPAN class="punctuation token">,</SPAN> out_folder<SPAN class="punctuation token">,</SPAN> file_name<SPAN class="punctuation token">,</SPAN> force_bytes<SPAN class="punctuation token">,</SPAN> add_headers<SPAN class="punctuation token">)</SPAN>
<SPAN class="number token">1151</SPAN> verify_cert<SPAN class="operator token">=</SPAN>verify_cert<SPAN class="punctuation token">,</SPAN> is_retry<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="number token">1152</SPAN>
<SPAN class="operator token">-</SPAN><SPAN class="operator token">></SPAN> <SPAN class="number token">1153</SPAN> self<SPAN class="punctuation token">.</SPAN>_handle_json_error<SPAN class="punctuation token">(</SPAN>resp_json<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'error'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> errorcode<SPAN class="punctuation token">)</SPAN>
<SPAN class="number token">1154</SPAN> <SPAN class="keyword token">return</SPAN> None
<SPAN class="number token">1155</SPAN> <SPAN class="keyword token">except</SPAN> AttributeError<SPAN class="punctuation token">:</SPAN>
C<SPAN class="punctuation token">:</SPAN>\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro<SPAN class="operator token">-</SPAN>py3\lib\site<SPAN class="operator token">-</SPAN>packages\arcgis\_impl\connection<SPAN class="punctuation token">.</SPAN>py <SPAN class="keyword token">in</SPAN> _handle_json_error<SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">,</SPAN> error<SPAN class="punctuation token">,</SPAN> errorcode<SPAN class="punctuation token">)</SPAN>
<SPAN class="number token">1171</SPAN>
<SPAN class="number token">1172</SPAN> errormessage <SPAN class="operator token">=</SPAN> errormessage <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\n(Error Code: "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>errorcode<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN><SPAN class="string token">")"</SPAN>
<SPAN class="operator token">-</SPAN><SPAN class="operator token">></SPAN> <SPAN class="number token">1173</SPAN> <SPAN class="keyword token">raise</SPAN> RuntimeError<SPAN class="punctuation token">(</SPAN>errormessage<SPAN class="punctuation token">)</SPAN>
<SPAN class="number token">1174</SPAN>
<SPAN class="number token">1175</SPAN> <SPAN class="keyword token">class</SPAN> <SPAN class="token class-name">_StrictURLopener</SPAN><SPAN class="punctuation token">(</SPAN>request<SPAN class="punctuation token">.</SPAN>FancyURLopener<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
RuntimeError<SPAN class="punctuation token">:</SPAN> Unable to update feature service layer definition<SPAN class="punctuation token">.</SPAN>
Invalid definition <SPAN class="keyword token">for</SPAN> <SPAN class="string token">''</SPAN><SPAN class="punctuation token">.</SPAN>
Invalid definition <SPAN class="keyword token">for</SPAN> System<SPAN class="punctuation token">.</SPAN>Collections<SPAN class="punctuation token">.</SPAN>Generic<SPAN class="punctuation token">.</SPAN>List`<SPAN class="number token">1</SPAN><SPAN class="punctuation token">[</SPAN>ESRI<SPAN class="punctuation token">.</SPAN>ArcGIS<SPAN class="punctuation token">.</SPAN>SDS<SPAN class="punctuation token">.</SPAN>FieldInfo<SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">(</SPAN>Error Code<SPAN class="punctuation token">:</SPAN> <SPAN class="number token">400</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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.