I have a question...I am trying to update a specific field with the code below. I want to query all the data and change a field named "FLAGFIELD" to YES
I can get at the Portal ID for the Service and point to the correct table and set the proper JSON payload...
But I am missing where I am querying or selecting with a where clause of 1=1
Can anyone help with this part? How do I "Change ALL records to YES in the field FLAGFIELD"
Thanks
gis = GIS("https://xxxxxxx", "xxxxxx", "xxxxxxxx")
#currenttoken = gis._con.token
portal_item = gis.content.get('b373b127exxxxxxxxxx0dfb14a1')
ports_layer = portal_item.tables[0]
# We need to reconstruct json features to be updated
FLAGFIELDvalue = 'YES'
# construct json
add_data = {
"attributes" : {
"FLAGFIELD" : FLAGFIELDvalue,
}
}
# Push updates
add_updates = ports_layer.edit_features(updates = [add_data])
Solved! Go to Solution.
Hi @kapalczynski ,
An easier approach may be to use arcpy. Ex:
# Variables
portalURL = 'https://gis.esri.com/portal'
username = 'portaladmin'
password = 'p0rt@l@dmin1'
featureServiceURL = 'https://gis.esri.com/server/rest/services/pipes/FeatureServer/0'
# Sign into Portal
arcpy.SignInToPortal(portalURL, username, password)
# Calculate Field
arcpy.management.CalculateField(
in_table=featureServiceURL,
field="FLAGFIELD",
expression="'YES'",
expression_type="PYTHON3",
code_block="",
field_type="TEXT",
enforce_domains="NO_ENFORCE_DOMAINS"
)
Hi @kapalczynski ,
An easier approach may be to use arcpy. Ex:
# Variables
portalURL = 'https://gis.esri.com/portal'
username = 'portaladmin'
password = 'p0rt@l@dmin1'
featureServiceURL = 'https://gis.esri.com/server/rest/services/pipes/FeatureServer/0'
# Sign into Portal
arcpy.SignInToPortal(portalURL, username, password)
# Calculate Field
arcpy.management.CalculateField(
in_table=featureServiceURL,
field="FLAGFIELD",
expression="'YES'",
expression_type="PYTHON3",
code_block="",
field_type="TEXT",
enforce_domains="NO_ENFORCE_DOMAINS"
)
@JakeSkinner thanks... that is way easier... not sure when to use one or the other... still a bit confused there
@JakeSkinner How could I add a WHERE clause to that ... so update the field value only when XXX
I tend to stick to arcpy when the functionality is available in both arcpy and ArcGIS API for Python. It minimizes the code you will have to write.
For the WHERE clause, you can first create a Feature Layer using the Make Feature Layer tool.
So create a layer from the inputFeatureLayer where the field "Imported = No" and dump the results into an InMemory Layer
From there I need to query the InMemory layer (r"memory\importedRecords") and append that to the main Feature...
Something like this
inputFeatureLayer = r'https://gggg.gov/hosting/rest/services/Hosted/Public_DEV_View/FeatureServer/0'
outputFeatureLayer = r'https://gggg.gov/hosting/rest/services/Hosted/Public_/FeatureServer/0'
whereClause = "Imported = 'No'"
arcpy.MakeFeatureLayer_management(inputFeatureLayer, r"memory\importedRecords", whereClause )
arcpy.management.Append(r"memory\importedRecords"], outputFeatureLayer, "TEST_AND_SKIP")
@kapalczynski you can run the Calculate Field on the feature layer and the edits will be reflected in the service:
# Variables
portalURL = 'https://gis.esri.com/portal'
username = 'portaladmin'
password = 'p0rt@l@dmin1'
featureServiceURL = 'https://gis.esri.com/server/rest/services/pipes/FeatureServer/0'
whereClause = "Imported = 'No'"
# Sign into Portal
arcpy.SignInToPortal(portalURL, username, password)
# Make Feature Layer
arcpy.MakeFeatureLayer_management(featureServiceURL, "fLyr", whereClause)
# Calculate Field
arcpy.management.CalculateField(
in_table="fLyr",
field="FLAGFIELD",
expression="'YES'",
expression_type="PYTHON3",
code_block="",
field_type="TEXT",
enforce_domains="NO_ENFORCE_DOMAINS"
)
Does it matter if the Layer is Hosted? I am try that and getting errors..
The Hosted FS is Public and not token needed.
portalURL = 'https://xxxx.gov/portal'
username = 'xxxx'
password = 'xxxx'
arcpy.SignInToPortal(portalURL, username, password)
inputFeatureLayer = r"https://dfdfdf.gov/hosting/rest/services/Hosted/DEV_H/FeatureServer/3"
whereClause = "Imported = 'Yes'"
#arcpy.MakeFeatureLayer_management(inputFeatureLayer, r"memory\importedRecords", whereClause )
arcpy.MakeFeatureLayer_management(inputFeatureLayer, "fLyr", whereClause )
arcpy.management.CalculateField(
in_table= "fLyr",
field="Imported",
expression="'No'",
expression_type="PYTHON3",
code_block="",
field_type="TEXT",
enforce_domains="NO_ENFORCE_DOMAINS"
)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Input Features: Dataset https://xxxx.gov/hosting/rest/services/Hosted/DEV_H/FeatureServer/3 does not exist or is not supported
Failed to execute (MakeFeatureLayer).
EDIT
I forgot to mention that this was a table that I was trying to update... not a feature class ... Is there a make table?
Think I got it with this
arcpy.management.MakeTableView(inputFeatureTable, "fLyr", whereClause )
arcpy.management.CalculateField(
in_table= "fLyr",
field="IMPORTED",
expression="'Yes'",
expression_type="PYTHON3",
code_block="",
field_type="TEXT",
enforce_domains="NO_ENFORCE_DOMAINS"
)