I wrote a script that will automate the overwrite feature layer process using a map object in ArcGIS Pro. When changes are made to the underlying data they show up on AGOL after running the script. The only problem is that one of the feature layers has to have full edit capabilities for field application users and that setting seems to get wiped out after overwriting the feature layer. I've tried loading the JSON from AGOL and then changing the 'capabilities' values and uploading them to AGOL and although a get_item_data reflects these changes are in the JSON if I print the object on my python interpreter it doesn't carry through to the JSON on AGOL. Here is what I am running after the overwrite script finishes:
import arcpy
import os, sys, json, urllib.request
from arcgis.gis import GIS
# define ArcGIS Pro project
prj = r"C:\\ArcGIS\Projects\TestPublishing\TestPublishing.aprx"
# make a list of maps representing the feature services to be overwritten
prj = arcpy.mp.ArcGISProject(prj)
mp = prj.listMaps()
portal = "http://www.arcgis.com" # Can also reference a local portal
user = "myUserName"
password = "myPassWord"
# get token for url in case there are secured services to overwrite
print("Connecting to {}".format(portal))
gis = GIS(portal, user, password)
token = gis._con.token
# AGOL json url for feature layer item data
sd_fs_name = mp[0].name
url1a = r"https://myServices.arcgis.com/gooblyGuck/ArcGIS/rest/services/"
url1b = r"/FeatureServer/0?f=pjson&token=" + "{}".format(token)
url1 = '{}{}{}'.format(url1a, sd_fs_name, url1b)
# create json memory dict to alter and write to json file so it can replace the json file for feature layer item.get_data()
with urllib.request.urlopen(url1) as http_response:
editInfo = json.loads(http_response.read().decode())
# change editit capabilities
editInfo['capabilities'] = 'Create,Delete,Query,Update,Editing'
# write changes to JSON file to be uploaded to AGOL
jFile = '{}\\{}{}'.format('C:\\myPath', mp[0].name, ".json")
with open(jFile, "w") as f:
json.dump(editInfo, f)
f.close()
editInfo.clear()
http_response.close()
print ("Json ready")
# update feature layer edit capabilities with updated JSON file
def search_item(gis,layer_name):
search_results = gis.content.search(layer_name, item_type='Feature Layer')
proper_index = [i for i, s in enumerate(search_results) if
'"'+layer_name+'"' in str(s)]
found_item = search_results[proper_index[0]]
get_item = gis.content.get(found_item.id)
return get_item
def update_layerdef(item):
item_data = item.get_data()
# Open JSON file that now contains the original edit capabilities before overwrite
with open(jFile) as json_data:
data = json.load(json_data)
print (data['capabilities'])
# Set the item_properties to include the desired update
item_properties = {"text": json.dumps(data)}
# 'Commit' the updates to the Item
item.update(item_properties=item_properties)
new_item_data = item.get_data()
def main():
# Search for item, get item data
item = search_item(gis, mp[0].name)
update_layerdef(item)
print("item: " + item.name)
print ("successful")
main()
After running this the feature layer rest service json for item_data['capabilities'] continues to revert back to 'Query'
What am I missing that will upload the JSON file (jFile) up to AGOL so the editing capabilities will go back to what they were before running the overwrite script ('capabilities': 'Create,Delete,Query,Update,Editing')?