Copy a hosted layer item

Learn how to use the portal service to create a new group in your portal, and add members to the group.

Prerequisites

You need an ArcGIS Location Platform, ArcGIS Online, or ArcGIS Enterprise account to use your portal.

Steps

Get the portal URL

To access a portal, you need an ArcGIS account which is associated with an organization that allows you to store and manage your content.

  1. In a web browser, sign in to your portal with your ArcGIS account.
  2. Identify the portal URL from the navigation bar. The base URL should be one of the following:
    • ArcGIS Location Platform: https://www.arcgis.com/sharing/rest
    • ArcGIS Online: https://www.arcgis.com/sharing/rest
    • ArcGIS Enterprise: https://{machine.domain.com}/{webadaptor}/rest

Get an access token

To perform the portal service operation in this tutorial, you need an access token from OAuth 2.0 credentials. It is the only supported form of authentication. The use of API keys is not supported.

  1. Go to the Create OAuth credentials for user authentication tutorial to get a set of OAuth 2.0 credentials.

  2. Get an access token from the OAuth 2.0 credentials.

  3. Copy the access token to your clipboard when prompted.

Set up environment

  1. In a different window, launch Postman and create a blank request.

Copy a hosted layer item

  1. Add the following import statement to your code.
    main.py
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    from arcgis.gis import GIS
    from arcgis.features import FeatureLayerCollection
    
    
  2. Establish a connection to the portal using the GIS() module.
    main.py
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    from arcgis.gis import GIS
    from arcgis.features import FeatureLayerCollection
    
    # Replace with your actual username, password, and item ID
    username = 'your_username'
    password = 'your_password'
    feature_layer_item_id = 'your_feature_layer_item_id'  # The item ID of the item you want to copy
    portal_url = 'https://www.arcgis.com'
    
    # Authenticate with ArcGIS Online
    gis = GIS(portal_url, username, password)
    
    
  3. Get the item using the itemID.
    main.py
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    from arcgis.gis import GIS
    from arcgis.features import FeatureLayerCollection
    
    # Replace with your actual username, password, and item ID
    username = 'your_username'
    password = 'your_password'
    feature_layer_item_id = 'your_feature_layer_item_id'  # The item ID of the item you want to copy
    portal_url = 'https://www.arcgis.com'
    
    # Authenticate with ArcGIS Online
    gis = GIS(portal_url, username, password)
    
    # Access the hosted feature layer item
    feature_layer_item = gis.content.get(feature_layer_item_id)
    
    
  4. Use the clone_items method to create a deep copy of the feature layer item.
    main.py
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    from arcgis.gis import GIS
    from arcgis.features import FeatureLayerCollection
    
    # Replace with your actual username, password, and item ID
    username = 'your_username'
    password = 'your_password'
    feature_layer_item_id = 'your_feature_layer_item_id'  # The item ID of the item you want to copy
    portal_url = 'https://www.arcgis.com'
    
    # Authenticate with ArcGIS Online
    gis = GIS(portal_url, username, password)
    
    # Access the hosted feature layer item
    feature_layer_item = gis.content.get(feature_layer_item_id)
    
    # Use the `clone_items` method to create an entirely new copy of the feature layer item
    cloned_items = gis.content.clone_items([feature_layer_item])
    
    # Check the result of the clone operation
    if cloned_items:
        print(f"Successfully cloned the item. New item ID: {cloned_items[0].id}")
    else:
        print("Failed to clone the item.")

View the results

If successful, your code will print an output like this to verify that the operation was successful:

Use dark colors for code blocksCopy
1
Successfully cloned the item. New item ID: <new_item_id>

What's next

Sign in and access your portal

List items in your portal

Set sharing level for an item

Use the portal service to set the sharing level for an item in your portal.


Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.