2

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')?

2 Answers 2

1

After checking with Esri they were able to point this out to me:

from arcgis.features import FeatureLayerCollection
gis = GIS('portal url', 'user name', 'password')

search_result= gis.content.search("Layer Name", "Feature Layer")
test_item= search_result[0]
test_flc = FeatureLayerCollection.fromitem(test_item)

update_dict = {"capabilities": "Create,Delete,Query,Update,Editing,Extract"}
test_flc.manager.update_definition(update_dict)

The updated code altogether would look like this:

import arcpy
import os, sys, json, urllib.request
from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection

# 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"

# sign in to AGOL (get token if needed)
print("Connecting to {}".format(portal))
gis = GIS(portal, user, password)
token = gis._con.token

search_result= gis.content.search(mp[0].name, "Feature Layer")
item= search_result[0]
flc = FeatureLayerCollection.fromitem(item)

update_dict = {"capabilities": "Create,Delete,Query,Update,Editing,Extract"}
flc.manager.update_definition(update_dict)
0

Thank you for answering your own question with the solution.

Here is a link to the respective guide from ESRI: https://developers.arcgis.com/python/guide/updating-feature-layer-properties/

Using the "manager" property on an instance of the FeatureLayerCollection [1] class will in fact access an instance of the FeatureLayerCollectionManager [2] class.

[1] https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#featurelayercollection

[2] https://developers.arcgis.com/python/api-reference/arcgis.features.managers.html#featurelayercollectionmanager

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.