How to update the item properties from metadata using the Python API?

6532
9
03-22-2019 01:40 PM
JonathanBailey
Occasional Contributor III

Through the ArcGIS Portal UI, when I edit the metadata for a portal item, the item's properties are updated when I save the metadata record:

However, when I update the item's metadata from the Python API, the item's properties are not updated. I've used the following 2 methods to update the item's metadata:

item.metadata = esri_metadata_file_name

item.update (metadata=esri_metadata_file_name)

Is there a way to, using the Python API, to update the item's properties when the metadata is updated?

0 Kudos
9 Replies
Egge-Jan_Pollé
MVP Regular Contributor

Good morning Jonathan,

Please have a look at the example below. This script allows you to get your item and to update metadata items like the snippet, the description, the Terms of Use, the credits and one or more tags.

HTH,

Egge-Jan

from arcgis.gis import GIS

# Log in to ArcGIS Online
my_agol = GIS("https://www.arcgis.com", username, password)

# Get your item
my_item = my_agol.content.get('<itemID>')

# Define metadata
service_snippet = 'Here you can put a short snippet describing the Feature Layer'
service_description = 'Here you can put a description of the Feature Layer'
service_terms_of_use = 'FOR INTERNAL USE ONLY'
service_credits = '© Me, myself and I'
service_tags = ['Railway','Canada']

# Create update dict
item_properties = {'snippet' : service_snippet,
                   'description' : service_description,
                   'licenseInfo' : service_terms_of_use,
                   'accessInformation' : service_credits,
                   'tags' : service_tags}

# Apply update
my_item.update(item_properties)

Modified Metadata

Via Child Resources > Info > Metadata you will get to an XML with your modified values (see screen dumps below).

JonathanBailey
Occasional Contributor III

Hi Egge-Jan,

Thank you for your response.

However, I'm trying to go in the other direction. I have an XML metadata document that I'm applying to the item. I'm wishing to have the item properties updated without having to write additional code to parse the XML and explicitly set the properties on the item.

Jon.

Egge-Jan_Pollé
MVP Regular Contributor

OK - would not know how to accomplish that. Let's see what others have to say about it...

0 Kudos
by Anonymous User
Not applicable

Hi Egge-Jan,
Thank you for posting your script file... very useful.

Question: what do you use as a resource to identify the name of the metadata item eg. 'snippet' or 'licenseInfo'. 

For example, if I wanted to update: metadata > Resource > Citation > Titles & Dates > Revision Date - see screencap - what would be the keyword to use in the 'dictitem_properties' in the script.

cheers,
Greg

Tags (3)
0 Kudos
MunachisoOgbuchiekwe
New Contributor III

What version of the Python API are you using? Can you try the item.update with version 1.5.1 of the Python API? The newest version 1.6.0 did not work for me but 1.5.1 did. 

0 Kudos
JonathanBailey
Occasional Contributor III

Hi Munacghiso,

Partial success -- I was using v1.6.0, so I went back to v1.5.1, and I'm now able to get the Description of the item to update. Other attributes, such as Credits, are still not updating, however.

SanghongYoo1
New Contributor II

I also use XML metadata file to maintain ArcGIS online metadata. Take a look at my GitHub repo. It works with version 1.5.2  (haven't tested with 1.6.0).

0 Kudos
GurminderBharani1
New Contributor III

Can you please confirm if you are able to update (overwrite) the metadata successfully by providing the local metadata file in the user interface of ArcGIS Online?

If yes, then you can track the network request to extract the URL and the parameters and send an https request using requests python module. 

You would also need token, so use REST API again to generate token. 

sweston_doc
New Contributor

It seems to be working for most metadata XML files I try, but there are a few where only uploading through ArcGIS Online manually works. This is at version 1.7.1 of the python API.

0 Kudos