Select to view content in your preferred language

Making a Custom Metadata Import tool

230
1
04-08-2025 05:17 AM
CTalbot
Occasional Contributor

Hello all, 

I'm troubleshooting this attempted custom metadata import tool 

the latest is a curious error of deleting the output location upon completion...

If anyone can give a clue as to why or how to fix would be much appreciated

here's the code:

import arcpy
from arcpy import metadata as md

class ImportMetadataTool:
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Import Metadata Tool"
self.description = "Imports metadata from a source to a target item in ArcGIS Pro."

def getParameterInfo(self):
"""Define the tool parameters."""
params = [
arcpy.Parameter(
displayName="Source Metadata",
name="source_metadata",
datatype="GPString",
parameterType="Required",
direction="Input"
),
arcpy.Parameter(
displayName="Target Metadata",
name="target_metadata",
datatype="GPString",
parameterType="Required",
direction="Input"
),
arcpy.Parameter(
displayName="Enable Automatic Updates",
name="enable_automatic_updates",
datatype="GPBoolean",
parameterType="Optional",
direction="Input"
),
arcpy.Parameter(
displayName="Output Location",
name="output_location",
datatype="GPString",
parameterType="Required",
direction="Input"
),
]
return params

def isLicensed(self):
"""Set whether the tool is licensed to execute."""
return True

def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return

def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return

def execute(self, parameters, messages):
"""The source code of the tool."""
source_metadata = parameters[0].valueAsText
target_metadata = parameters[1].valueAsText
enable_automatic_updates = parameters[2].value
output_location = parameters[3].valueAsText

try:
# Create a Metadata object for the target item
target_item_md = md.Metadata(target_metadata)

# Import metadata from the source
target_item_md.importMetadata(source_metadata, output_location)

# Set automatic updates if enabled
if enable_automatic_updates:
target_item_md.metadata_sync_option = 'ALWAYS'
target_item_md.reload()

# Save the changes to the target metadata
target_item_md.save()
messages.addMessage("Metadata imported successfully.")

except Exception as e:
messages.addErrorMessage(f"An error occurred: {e}")

#def postExecute(self, parameters):
"""This method takes place after outputs are processed and
added to the display."""
return

Sincerely,
Christopher
0 Kudos
1 Reply
TonyAlmeida
MVP Regular Contributor

Try removing the output_location from the importMetaData() and replace it with import_type.

0 Kudos