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
Solved! Go to Solution.
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})
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')})
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})
That did it! thanks!