English

How To: Copy metadata from an existing service to a new item through a REST endpoint using ArcGIS API for Python

Summary

An existing service metadata can be copied to a new item in ArcGIS Online or ArcGIS Enterprise using the ArcGIS API for Python. This can be particularly helpful to maintain uniformity throughout the whole organization in when multiple data is involved. 

Procedure

The following procedure describes how to copy the metadata from an existing service. There are two ways to copy the metadata, from a feature layer, and from a map service. 

Using Feature layer

  1. Import the necessary modules.
import arcgis
from arcgis.gis import GIS
from arcgis.features import FeatureLayer
  1. Specify the the FeatureLayer() class to get the items from the service endpoint.
gis = GIS("https://arcgis.com", "Username", "Password")
url = '<Feature Layer Service URL>'
layer = FeatureLayer(url)
  1. Set the metadata from the service to the respective variables. 
itemTags = layer.properties.documentInfo.Keywords
itemDescription = layer.properties.serviceDescription
itemSnippet = layer.properties.documentInfo.Subject
itemTitle = layer.properties.documentInfo.Title
  1. Create a new item in ArcGIS Online with the information from the existing service.
wmsUrl = '<URL to the service>'
props = {'title': itemTitle, 'type': 'Feature Service', 'url': '<URL to the service>', 'tags': itemTags, 'description': itemDescription, 'snippet': itemSnippet}
wmsItem = gis.content.add(item_properties=props, data=wmsUrl)
The following shows a sample of the full script:
import arcgis
from arcgis.gis import GIS
from arcgis.features import FeatureLayer

gis = GIS("https://arcgis.com", "Username", "Password")
url = 'https://services.arcgis.com/Wl7Y1m92PbjtJs5n/arcgis/rest/services/workers_dec6d6e11b564e338fb3254626cee2f0/FeatureServer'
layer = FeatureLayer(url)

itemTags = layer.properties.documentInfo.Keywords
itemDescription = layer.properties.serviceDescription
itemSnippet = layer.properties.documentInfo.Subject
itemTitle = layer.properties.documentInfo.Title

wmsUrl = 'https://domain.esri.com:6443/arcgis/rest/services'
props = {'title': itemTitle, 'type': 'Feature Service', 'url': 'https://domain.esri.com:6443/arcgis/rest/services', 'tags': itemTags, 'description': itemDescription, 'snippet': itemSnippet}
wmsItem = gis.content.add(item_properties=props, data=wmsUrl)
Using Map Service
  1. Import the necessary modules.
import arcgis
from arcgis.gis import GIS
from arcgis.mapping import MapImageLayer
  1. Specify the MapImageLayer() class to get the items from the service endpoint
gis = GIS("https://arcgis.com", "Username", "Password")
url = '<Map Service URL>'
layer = MapImageLayer(url)
  1. Set the metadata from the service to the respective variables.
itemTags = layer.item_info['tags']
itemDescription = layer.item_info['description']
itemSnippet = layer.item_info['summary']
itemTitle = layer.item_info['title']
itemTumbnail = layer.thumbnail()
  1. Create a new item with the information from the existing service.
wmsUrl = '<URL to the service>'
props = {'title': itemTitle, 'type': 'Map Service', 'url': '<Url to the service>', 'tags': itemTags, 'description': itemDescription, 'snippet': itemSnippet}
wmsItem = gis.content.add(item_properties=props, data=wmsUrl, thumbnail= itemTumbnail)
The following shows a sample of the full script:
import arcgis
from arcgis.gis import GIS
from arcgis.mapping import MapImageLayer

gis = GIS("https://arcgis.com", "Username", "Password")
url = 'https://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/MapServer'
layer = MapImageLayer(url)

itemTags = layer.item_info['tags']
itemDescription = layer.item_info['description']
itemSnippet = layer.item_info['summary']
itemTitle = layer.item_info['title']
itemTumbnail = layer.thumbnail()

wmsUrl = 'https://domain.esri.com:6443/arcgis/rest/services'
props = {'title': itemTitle, 'type': 'Map Service', 'url': 'https://domain.esri.com:6443/arcgis/rest/services', 'tags': itemTags, 'description': itemDescription, 'snippet': itemSnippet}
wmsItem = gis.content.add(item_properties=props, data=wmsUrl, thumbnail= itemTumbnail)

Related Information

Last Published: 4/23/2021

Article ID: 000019020

Software: ArcGIS Online 9.1 ArcGIS API for Python 1.8.4, 1.8.3, 1.8.2, 1.8.1, 1.8.0, 1.7.1, 1.7.0, 1.6.1, 1.6, 1.5.3, 1.5.2, 1.5.1, 1.5, 1.4.1, 1.4, 1.3, 1.2.4, 1.2.3, 1.2.2, 1.2.1, 1.2, 1.0.1, 1.0