Update AGOL Webmap Symbology using ArcGIS API for Python

08-27-2020 04:15 PM
New Contributor II

I have all my JSON updating figured out and now I am trying to write it to ArcGIS Online to swap out it's JSON file that controls the properties of the webmap.

Found this script below from an Esri staff member that should do the trick:

from arcgis import GISimport json, sys def search_item(conn,layer_name):    search_results = conn.content.search(layer_name, item_type='Web Map')    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 = conn.content.get(found_item.id)    return get_item def update_wm_layerdef(item):    item_data = item.get_data()     print("*******************ORIGINAL DEFINITION*********************")    print(json.dumps(item_data, indent=4, sort_keys=True))    # Open JSON file containing symbology update    with open('MY_JSONFILE') as json_data:        data = json.load(json_data)     # 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)     # Print item_data to see that changes are reflected    new_item_data = item.get_data()    print("***********************NEW DEFINITION**********************")    print(json.dumps(new_item_data, indent=4, sort_keys=True))def main():    conn = GIS("https://arcgis.com",                 "MY_USERNAME", "MY_PASSWORD")         # Search for item, get item data)    item = search_item(conn, 'wm_lyrsym')    update_wm_layerdef(item)if __name__ == '__main__':    sys.exit(main())

where you see "MY_..." denotes where I inserted my own values in place of what the original author provided (except for https://arcgis.com for the last function).

I get this error when running the script:

Traceback (most recent call last):  File "<string>", line 117, in <module>  File "<string>", line 113, in main   File "<string>", line 85, in search_itemIndexError: list index out of range

I suspect this is because I need to replace 'wm_lyrsym' but I did replace it with the layer name that the sybology is applied to and got the same error.

Anything I need to further alter the original script to get it to work? Here's the website I found the code on:


Tags (2)
0 Kudos
0 Replies