ArcPy sharing draft - how to add spaces to tag names?

650
5
Jump to solution
09-09-2020 11:13 AM
dcafdg
by
Occasional Contributor II

I am trying to get a tag containing spaces, such as "Endangered Species Act", into an ArcGIS Online item. I have no problem with this when sharing from ArcGIS Pro or using the AGOL web interface in a browser. But when using arcpy to share the layer to AGOL, it splits them on the spaces as well as the commas, resulting in a tag for each word. 

aprx = arcpy.mp.ArcGISProject(in_project)
map_out = aprx.listMaps(map_name)[0]
# TODO: Error handling if map not found
layers = map_out.listLayers()
for layer in layers:
    if layer.name == 'ESA':
    sharing_draft = map_out.getWebLayerSharingDraft("HOSTING_SERVER", "FEATURE", 
        agol_layer_name, layer)
    sharing_draft.overwriteExistingService = True
    sharing_draft.portalFolder = agol_folder_name
    sharing_draft.tags = 'salmon, trout, Endangered Species Act, fish'

    # in summary the rest looks like this: 
    # sharing_draft.exportToSDDraft()
    # arcpy.StageService_server()
    # arcpy.UploadServiceDefinition_server()    ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Here are the various things I tried: 

InputOutput

'salmon, trout, Endangered Species Act, fish'

(in the snippet above)

salmon, trout, Endangered, Species, Act, fish

(splits on spaces or commas)

"salmon, trout, 'Endangered Species Act', fish"

(embed single quotes in string)

salmon, trout, 'Endangered, Species, Act', fish

(still splitting, plus quote marks included)

'salmon, trout, "Endangered Species Act", fish'

(same as previous, reversing single/double quotes)

salmon, trout, "Endangered, Species, Act", fish

(still splitting, plus quote marks included)

'salmon, trout, Endangered%20Species%20Act, fish'

(use HTML encoding)

salmon, trout, Endangered%20Species%20Act, fish

['salmon', 'trout', 'Endangered Species Act', 'fish']

(as array)

['salmon', 'trout', 'Endangered, Species, Act', 'fish']

(literally includes brackets and quotes)

I know how to edit tags with the arcgis API, but since I'm already here using arcpy, I'd really like to solve these issues with just one toolkit. 

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

Sounds like

BUG-000116752: Spaces are considered as separators for multiple tag.. 

You will need to contact tech support to add your name to the list, OR hit SUBSCRIBE on the top right of the link above


... sort of retired...

View solution in original post

5 Replies
DanPatterson
MVP Esteemed Contributor

Sounds like

BUG-000116752: Spaces are considered as separators for multiple tag.. 

You will need to contact tech support to add your name to the list, OR hit SUBSCRIBE on the top right of the link above


... sort of retired...
dcafdg
by
Occasional Contributor II

Wow, identified 2 years ago in version 2.2 and still not fixed... Seems like a trivial string parsing problem. That's disappointing. 

Thanks for the heads-up, at least now I can stop banging my head against the wall. 

0 Kudos
DanPatterson
MVP Esteemed Contributor

As a thought... try

"salmon;trout;Endangered Species Act;fish"

esri uses semi-colon delimited strings in some functions, and I remembered, there are no spaces after the separator.

Worth a shot and you can say you covered all bases


... sort of retired...
0 Kudos
dcafdg
by
Occasional Contributor II

Good thought, but it still splits on space, and does not split on semicolon, so I get the following: 

Input: salmon;trout;Endangered Species Act;fish

Output (3 tags out, split on commas): salmon;trout;Endangered, Species, Act;fish

0 Kudos
dcafdg
by
Occasional Contributor II

It's a bummer that I need to use another API to solve this, but in case anyone reading this wants to know how, follow these steps to use the ArcGIS API for Python. My code has a bunch of variables and loops that I simplified below, but I think this is more or less ready to run (though not tested ). I create my tags as a list, but you could just create them as a comma-delimited string and remove the join() function below. 

portal_url = "https://<org>.maps.arcgis.com/"
username = '<you>'
password = '<password>'
tag_list = ['salmon', 'trout', 'Endangered Species Act', 'fish']

# Open a connection 
from arcgis.gis import GIS
gis = GIS(portal_url, username, password)
item = gis.get_item_by_id(item_id)
item_properties = {'tags': ','.join(tag_list)}
item.update(item_properties=item_properties)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍