R-ArcGIS / arcgislayers

ArcGIS Location Services
http://r.esri.com/arcgislayers/
Apache License 2.0
38 stars 9 forks source link

Support AddToDefinition (add new field to feature service) #178

Open JosiahParry opened 5 months ago

JosiahParry commented 5 months ago

Discussed in https://github.com/R-ArcGIS/arcgislayers/discussions/177

Originally posted by **sarahhurley** March 27, 2024 Hi there - I am wondering if there is the capability of adding a field to a feature layer through the arcgislayers package? I'm working on setting up a workflow that may require us to add a field to a dataset over time (trying to avoid needing to republish it every time). I've tried to look through the documentation, but I've only found in the "[Overwrite Hosted Feature Layer](https://r.esri.com/r-bridge-site/location-services/workflows/overwrite-feature-service.html)" section of the R-ArcGIS Bridge documentation a line that says "Truncating a service does not change the field definitions or permit us to add or remove fields to it. If you wish to do so, publish a new layer instead." Thanks for your help!
JosiahParry commented 5 months ago

For reference: https://github.com/R-ArcGIS/arcgislayers/discussions/177#discussioncomment-8929402

@mmachir helped provide a sample POST request which should be easy to mimic in R

request url - 
https://services1.arcgis.com/unique-id-here/arcgis/rest/admin/services/heart_binary/FeatureServer/0/addToDefinition

method-
POST

payload - 
f: 
json
addToDefinition: 
{"fields":[{"name":"testfieldname","type":"esriFieldTypeString","alias":"Test Display Name","nullable":true,"editable":true,"length":256,"defaultValue":"~de fault-value!"}]}
token:  redacted

This should be fairly simple to mimic:

b_req <- arc_base_req(x[["url"]], path = "addToDefinition")
httr2::req_form_body(
    b_req,
    fields = fields_json
)
JosiahParry commented 1 month ago

Note that this would allow us to also add related tables https://community.esri.com/t5/arcgis-online-questions/adding-related-table-to-arcgis-online-feature/td-p/252845

this could be a useful workflow when there are tables that have many matches to single polygons. Repeating the polygons is not ideal.