Add new tag without removing existing ones

885
3
Jump to solution
09-30-2022 01:29 PM
Rayn
by
New Contributor III

Im trying to do what i thought would be a simple task but its turning out not to be so simple (or maybe it is but im making it not simple).  I just want to add a new tag to all my items in my portal without removing their existing tags.  I have attempted to use item.update but that removes existing tags.  I have moved on to getting the existing list of tags from item.tags and then appending to that list but i cannot get the code to work (spoiler alert, i am not a python expert).  I've tried inserting as a list as well as converting to a string to insert into the update function.  nothing seems to work.  Any help is appreciated.

this is latest iteration:

item = source.content.get("item_id")
tags = item.tags
tags.append('newtag')
liststring = ','.join(tags)
inputstring = "item_properties={'tags': '" + liststring + "'}"
item.update(inputstring)

 thanks

0 Kudos
1 Solution

Accepted Solutions
Clubdebambos
Occasional Contributor III

Although item.tags returns a list, using the append method directly on the property returns None.

from arcgis import GIS

agol = GIS("home")

item = agol.content.get("item_id")

tags_1 = item.tags

print(tags_1)
>>> ['tag1', 'tag2', 'tag3']

tags_2 = item.tags.append("newtag")

print(tags_2)
>>> None

## USE THIS
tags = item.tags

tags.append("newtag")

item.update(item_properties={'tags':tags})

 

 

~ learn.finaldraftmapping.com

View solution in original post

0 Kudos
3 Replies
jcarlson
MVP Esteemed Contributor

You may be making it more complicated than you need to. According to the docs, the tags object is:

Optional string. Tags listed as comma-separated values, or a list of strings. Used for searches on items.

Also, I don't think it will work, creating a string that looks like a dict. It actually needs to be one. Try something like this, maybe:

item = source.content.get('itemid')

item.update(item_properties={'tags':item.tags.append('newtag')})
- Josh Carlson
Kendall County GIS
0 Kudos
Clubdebambos
Occasional Contributor III

Although item.tags returns a list, using the append method directly on the property returns None.

from arcgis import GIS

agol = GIS("home")

item = agol.content.get("item_id")

tags_1 = item.tags

print(tags_1)
>>> ['tag1', 'tag2', 'tag3']

tags_2 = item.tags.append("newtag")

print(tags_2)
>>> None

## USE THIS
tags = item.tags

tags.append("newtag")

item.update(item_properties={'tags':tags})

 

 

~ learn.finaldraftmapping.com
0 Kudos
Rayn
by
New Contributor III

That did it!  thanks!

0 Kudos