Managing your content

As an organization matures and expands its GIS, users add items of various types and properties with varying relationships to one another. Any administrator must regularly manage items contained in various groups and owned by various users. In this section we demonstrate how to work with individual items in a GIS. This guide shows how to retrieve item properties, delete an existing item, and how to examine relationships between items.

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 [ ]:
#connect to GIS
from arcgis.gis import GIS
gis = GIS("https://pythonapi.playground.esri.com/portal", "arcgis_python", "amazing_arcgis_123")
In [ ]:
#access an Item
volcanoes_item = gis.content.get('452afa4ce761441995cb6d8c69d854d2')
volcanoes_item
Out[ ]:
Volcanic Eruptions Around the World
Feature Layer Collection by arcgis_python
Last Modified: April 13, 2018
0 comments, 0 views
In [ ]:
# item id
volcanoes_item.id
Out[ ]:
'452afa4ce761441995cb6d8c69d854d2'
In [ ]:
# title
volcanoes_item.title
Out[ ]:
'Volcanic Eruptions Around the World'
In [ ]:
# tags
volcanoes_item.tags
Out[ ]:
['natural disasters', 'volcano']

Updating item properties

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

In [ ]:
# update the tags
volcanoes_item.update(item_properties={'tags':'python, vei, empirical, in-situ'})
Out[ ]:
True
In [ ]:
volcanoes_item.tags
Out[ ]:
['python', 'vei', 'empirical', 'in-situ']
In [ ]:
# updating thumbnail
volcanoes_item.update(thumbnail=r'pathway\to\your\directory\IMAGE_NAME.jpg')
Out[ ]:
True
In [ ]:
volcanoes_item
Out[ ]:
Volcanic Eruptions Around the World
Feature Layer Collection by arcgis_python
Last Modified: April 13, 2018
0 comments, 2 views

Downloading your items

You can download various components of your items, such as the thumbnail, data, or metadata. Downloading assists you in the process of archiving content for a GIS your organization is retiring, or for publishing and migrating content from one GIS to another.

Download item data

The get_data() function reads the binary or text data associated with an item and returns it as a byte array.

  • If the text data is JSON, it converts the data to a Python dictionary. If it is another text format, it returns the file as a string.
  • If data is not text, binary files are returned along with the path to where the data is downloaded.
In [ ]:
ports_csv_item = gis.content.get('a0b157b9ce2440a39551967c7c789835')
ports_csv_item
Out[ ]:
US Locations of LNG and Oil Deep Water Ports
liquefied natural gas and oil deep water ports in the United StatesCSV by arcgis_python
Last Modified: April 13, 2018
0 comments, 2 views

The data for a csv item is the csv file itself. It's downloaded to your default temporary directory

In [ ]:
ports_csv_item.get_data()
Out[ ]:
'C:\\pathway\\to\\your\\temp_directory\\FILENAME.csv'

Download metadata

You can download metadata into an XML file using the download_metadata() method. Once you have the XML file you can edit it and then modify existing portal items by entering the file name as the metadata parameter of the Item.update() method. You can also add it with the metadata parameter on the ContentManager.add() method when creating new content. See the specific documentation for enabling metadata with ArcGIS Online or ArcGIS Enterprise.

In [ ]:
ports_csv_item.download_metadata(save_folder=r'\pathway\to\download\the\metadata\')

Download thumbnail

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

In [ ]:
ports_csv_item.download_thumbnail(save_folder= r'pathway\to\your\directory\')
Out[ ]:
'pathway\to\your\directory\IMAGE_NAME.png'

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 [ ]:
item_for_deletion = gis.content.get('a1752743422b45f791b7eb4dbc5a8010')
item_for_deletion
Out[ ]:
Liberia Airports
Feature Layer Collection by arcgis_python
Last Modified: April 13, 2018
0 comments, 2 views
In [ ]:
item_for_deletion.delete()
Out[ ]:
True

Delete protection

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

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

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcpy-dev\lib\site-packages\arcgis\gis\__init__.py in delete(self)
   5795         except:
   5796             folder = None
-> 5797         return self._portal.delete_item(self.itemid, self.owner, folder)
   5798 
   5799     def update(self, item_properties=None, data=None, thumbnail=None, metadata=None):

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcpy-dev\lib\site-packages\arcgis\_impl\portalpy.py in delete_item(self, item_id, owner, folder)
    589         path += '/items/' + item_id + '/delete'
    590         #print(path)
--> 591         resp = self.con.post(path, self._postdata())
    592 
    593         if resp:

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcpy-dev\lib\site-packages\arcgis\_impl\connection.py in post(self, path, postdata, files, ssl, compress, is_retry, use_ordered_dict, add_token, verify_cert, token, try_json, out_folder, file_name, force_bytes, add_headers)
   1154                                          verify_cert=verify_cert, is_retry=True)
   1155 
-> 1156                 self._handle_json_error(resp_json['error'], errorcode)
   1157                 return None
   1158         except AttributeError:

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcpy-dev\lib\site-packages\arcgis\_impl\connection.py in _handle_json_error(self, error, errorcode)
   1174 
   1175         errormessage = errormessage + "\n(Error Code: " + str(errorcode) +")"
-> 1176         raise RuntimeError(errormessage)
   1177 
   1178 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.

<a id ="finding-relationships-between-items"></a>

Finding relationships between items

You can add many types of items to your web GIS. Depending upon the type of item, you can then perform different operations on that item. Once an item is added to the GIS, it seldom exists in isolation but often relates to other items. You might publish new items from other items to create a new service, or create a new service as a result of a geoprocessing operation on a source item. You may also add one or more layer items to compose a web map or web scene item. Whenever you perform such operations, you establish a relationship between the items. A GIS supports different relationship types depending upon the items involved. These relationships represent how items in your GIS are connected to one another. They also convey the impact removing an item from your GIS may have on other items. See the Relationships between web services and portal items article for more information.

The Item class has 3 methods that allow you to determine the relationships to and from an item:

Let us observe how the ports csv item and the feature layer published from this item share a relationship

NOTE: The Relationship Type functionality is currently evolving within Enterprise and ArcGIS Online implementations. Some items do not currently return all relationships they have between other items.

In [ ]:
ports_feature_layer = gis.content.get('238d3e97434f40fda38bc846a97b5cfe')
ports_feature_layer
Out[ ]:
US Locations of LNG and Oil Deep Water Ports
liquefied natural gas and oil deep water ports in the United StatesFeature Layer Collection by arcgis_python
Last Modified: April 13, 2018
0 comments, 1 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 [ ]:
ports_feature_layer.related_items('Service2Data', 'forward')
Out[ ]:
[<Item title:"US Locations of LNG and Oil Deep Water Ports" type:CSV owner:arcgis_python>]

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

In [53]:
ports_csv_item.related_items('Service2Data', 'reverse')
Out[53]:
[<Item title:"US Locations of LNG and Oil Deep Water Ports" type:Feature Layer Collection owner:arcgis_python>]

Relationships of web map items

As seen above, source data and the services published from the data share a relationship. Web Map items share a Map2Service relationship with the items used as layers in the map. Let's examine what results we get with a web map using the methods described above.

In [ ]:
webmap_item = gis.content.get('e92ec599e2a64c69945fe5cd833c8cb8')
webmap_item
Out[ ]:
Liberia Map
A web map demonstrating the relationships between items.Web Map by arcgis_python
Last Modified: April 13, 2018
0 comments, 0 views

The dependent_upon() method lists all forward relationships for an item. It also lists the items it depends on, and the type of dependency between the two items. In the case of this web map, it has three operational layers dependent upon hosted feature layers (with corresponsding item id values) and a base map layer that depends upon a map service (with corresponding url).

In [ ]:
webmap_item.dependent_upon()
Out[ ]:
{'list': [{'dependencyType': 'id', 'id': '77561ef541054730af5597ff4d1a3d98'},
  {'dependencyType': 'id', 'id': 'a1752743422b45f791b7eb4dbc5a8010'},
  {'dependencyType': 'id', 'id': '656e5b4e46ba474c8b146fd02803809a'},
  {'dependencyType': 'url',
   'url': 'https://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer'}],
 'nextStart': -1,
 'num': 4,
 'start': 1,
 'total': 4}

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

In [ ]:
webmap_item.dependent_to()
Out[ ]:
{'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 4 other items. However, for the hosted feature layers (powered by feature services) the dependency type is listed as id instead of Map2Service. We can change that by adding a new relationship.

You can add a relationship by using the add_relationship() method to pass the item to which the current item is related and the type of relationship you want to create.

In [ ]:
#from the example above, use the item id of first relationship to get the related item
webmap_related_item = gis.content.get('77561ef541054730af5597ff4d1a3d98')
webmap_related_item
Out[ ]:
Access_To_Services
Feature Layer Collection by arcgis_python
Last Modified: April 13, 2018
0 comments, 3 views
In [ ]:
# add a relationship
webmap_item.add_relationship(rel_item= webmap_related_item, rel_type= 'Map2Service')
Out[ ]:
True

Now that a relationship is added, the property is reflected on the web map item. After the update, querying for related items on the web map item promptly returns the feaure layer collection item.

In [ ]:
webmap_item.related_items('Map2Service', 'forward')
Out[ ]:
[<Item title:"Access_To_Services" type:Feature Layer Collection owner:arcgis_python>]

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?