Managing your content

As a GIS matures, it gets filled with items of various types, properties and relationships with one another. Managing such items across various users of an org becomes an important task. In this section we observe how you can work with individual items in your contents, the properties of an item, how to delete them and how items are related to one another.

Topics covered in this page:

Properties of an item

An Item in your GIS is rich with multiple properties. You can access them as properties on the Item object

In [1]:
#connect to GIS
from arcgis.gis import GIS
gis = GIS("portal url", "username", "password")
In [2]:
#access an Item
earthquakes_item = gis.content.get('7ff6299367fa4a789bae81149b7ceae9')
Earthquakes around the world from 1800s to early 1900s
Feature Layer Collection by arcgis_python_api
Last Modified: December 14, 2016
0 comments, 5 views
In [5]:
# item id
In [6]:
# title
'Earthquakes around the world from 1800s to early 1900s'
In [7]:
# tags
['arcgis', 'python', 'earthquake', 'natural disaster', 'emergency']

Updating item properties

You can update any of the Items properties using the update() method. It accepts parameters similar to add() method.

In [8]:
# update the tags
earthquakes_item.update(item_properties={'tags':'python, seismometer, empirical, in-situ'})
In [9]:
['python', 'seismometer', 'empirical', 'in-situ']
In [10]:
# updating thumbnail
In [6]:
Earthquakes around the world from 1800s to early 1900s
Feature Layer Collection by arcgis_python_api
Last Modified: December 14, 2016
0 comments, 5 views

Downloading your items

You can download the various parts of your items such as thumbnail, data, metadata to file system. You can use this process to archive content if you are retiring your GIS or to re-publish / migrate content from one GIS to another.

Download item data

The get_data() reads the data associated with an item and returns it as a byte array. If the data is a JSON, it converts and returns as a Python dictionary, else stores it as a file in the user's temporary directory.

In [7]:
ports_csv_item = gis.content.get('a1623d78753a4213b1cc59790f54d15c')
Ports along west coast
CSV file with locations of major ports along West Coast of USACSV by arcgis_python_api
Last Modified: December 12, 2016
0 comments, 5 views
In [31]:

Download metadata

You can download metadata into an XML file using the download_metadata() method. Once you have this XML file, you can modify it as per need and pass it to the metadata parameter of update() or add() when updating or creating new content.

In [27]:

Download thumbnail

Finally, you can download the thumbnail of an item using the download_thumbnail() method.

In [32]:
ports_csv_item.download_thumbnail(save_folder= r'E:\temp')

Deleting content

You can delete any item using the delete() method of the Item object. When deleting a hosted layer, this operation also deletes the hosted services powering the item.

In [9]:
item_for_deletion = gis.content.get('a558ea98067c44049be3d2be18660774')
Big Bear mountains
mountainsFeature Collection by arcgis_python_api
Last Modified: December 09, 2016
0 comments, 1 views
In [34]:

Delete protection

You can protect items from getting deleted inadvertently. Use the protect() of the Item object for this.

In [35]:
# let us protect the ports item we accessed earlier
ports_csv_item.protect(enable = True)
{'success': True}
In [36]:
# attempting to delete will return an error
Unable to delete item. Delete protection is turned on.
RuntimeError                              Traceback (most recent call last)
<ipython-input-36-c89e80fc1020> in <module>()
      1 # attempting to delete will return an error
----> 2 ports_csv_item.delete()

C:\Anaconda3\envs\geosaurus_gold\lib\site-packages\arcgis\ in delete(self)
   2753         except:
   2754             folder = None
-> 2755         return self._portal.delete_item(self.itemid, self.owner, folder)
   2757     def update(self, item_properties=None, data=None, thumbnail=None, metadata=None):

C:\Anaconda3\envs\geosaurus_gold\lib\site-packages\arcgis\_impl\ in delete_item(self, item_id, owner, folder)
    580         path += '/items/' + item_id + '/delete'
    581         #print(path)
--> 582         resp =, self._postdata())
    584         if resp:

C:\Anaconda3\envs\geosaurus_gold\lib\site-packages\arcgis\_impl\ in post(self, path, postdata, files, ssl, compress, is_retry, use_ordered_dict, add_token, verify_cert, token)
    952                 elif errorcode == 498:
    953                     raise RuntimeError('Invalid token')
--> 954                 self._handle_json_error(resp_json['error'], errorcode)
    955                 return None
    956         except AttributeError:

C:\Anaconda3\envs\geosaurus_gold\lib\site-packages\arcgis\_impl\ in _handle_json_error(self, error, errorcode)
    973         errormessage = errormessage + "\n(Error Code: " + str(errorcode) +")"
--> 974         raise RuntimeError(errormessage)
    976 class _StrictURLopener(request.FancyURLopener):

RuntimeError: Unable to delete item. Delete protection is turned on.
(Error Code: 500)

As expected an exception is raised. You can disable delete protection by setting the enable parameter to False.

Finding relationships between items

In a GIS, seldom do items exist in isolation. You typically publish new items from existing items either into a new service or as a result of performing a geoprocessing operation on the source item. You may add one or more layer items to compose a web map or web scene item. Whenever you perform such an operation, you establish a relationship between the items. These relationships represent how items in your GIS are connected to one-another. They also convey the impact of removing an item from your GIS.

The Item class has 3 methods with which you can determine relationships to and from an item. They are dependent_upon(), dependent_to(), related_items(). Let us observe how the ports csv item and the feature layer published from this item share a relationship

In [10]:
ports_feature_layer = gis.content.get('b0cb0c9f63e74e8480af0286eb9ac01f')
Ports along west coast
Feature Layer Collection by arcgis_python_api
Last Modified: December 07, 2016
0 comments, 7 views

Since we know this feature layer item is published from the csv, we can specify the relationship_type parameter as Service2Data. The direction of relationship would be forward as the current item is a service and we are looking for the original data used to publish it.

In [56]:
ports_feature_layer.related_items('Service2Data', 'forward')
[<Item title:"Ports along west coast" type:CSV owner:arcgis_python_api>]

On the ports csv item, the relationship_type remains the same whereas the direction is reversed

In [57]:
ports_csv_item.related_items('Service2Data', 'reverse')
[<Item title:"Ports along west coast" type:Feature Service owner:arcgis_python_api>]

Relationships of web map items

Just like how data and services published out of the data share a relationship, web maps share a relationship of type Map2Service with the items used as web layers.

In [11]:
webmap_item = gis.content.get('cc1876f1708e494d81a93014c1f56c58')
Los Angeles tourism stops
demonstrating how maps and web layers are relatedWeb Map by arcgis_python_api
Last Modified: December 12, 2016
0 comments, 1 views

Calling dependent_upon() method lists all forward relationships an item has. It lists all the items the current item is dependent upon. In the case of this web map, it has one operational layer that is dependent upon a feature collection and has a base map layer that is dependent upon a map service.

In [59]:
{'fullCount': 2,
 'list': [{'dependencyType': 'id', 'id': '31ad8b9a8ed3461992607eb8309816e2'},
  {'dependencyType': 'url',
   'url': ''}],
 'nextStart': 1,
 'num': 2,
 'start': 1,
 'total': 2}

Calling dependent_to() lists the items which are dependent upon this web map item, which in this case is none.

In [63]:
{'fullCount': 0, 'list': [], 'nextStart': 1, 'num': 0, 'start': 1, 'total': 0}

The table in this documentation gives you the list of all supported relationship types that can exist between two items in your GIS.

Adding relationships

It is beneficial to add a relationship to establish how information flows from one item to another across a GIS. In the web map example above, the map is dependent upon 2 other items. However, the dependency type is listed as id instead of Map2FeatureCollection. We can change that by adding a new relationship.

You can add a relationship by using the add_relationship() method and passing it the item to which the current item is realted to and the type of relationship.

In [12]:
#from the example above, use the item id of first relationship to get the related item
webmap_related_item = gis.content.get('31ad8b9a8ed3461992607eb8309816e2')
Favorite tourist spots among travelersFeature Collection by arcgis_python_api
Last Modified: December 09, 2016
0 comments, 7 views
In [67]:
# add a relationship
webmap_item.add_relationship(rel_item= webmap_related_item, rel_type= 'Map2FeatureCollection')

Now that a relationship is added, this property is reflected not only on the web map item, but also on the feature collection item. Thus, if you query dependent_to() method on the feature collection item, you would get the item id of the web map item.

In [71]:
{'fullCount': 1,
 'list': [{'dependencyType': 'id', 'id': 'cc1876f1708e494d81a93014c1f56c58'}],
 'nextStart': 1,
 'num': 1,
 'start': 1,
 'total': 1}

After the update, querying for related items on the web map item promptly returns the feaure collection item.

In [72]:
webmap_item.related_items('Map2FeatureCollection', 'forward')
[<Item title:"LA_tourism_stops" type:Feature Collection owner:arcgis_python_api>]

Deleting relationships

You can remove defunct relationships by calling the delete_relationship() method and passing the relationship type and related item.

Feedback on this topic?