<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: AGOL Notebook - item.export and the 'overwrite' argument in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/agol-notebook-item-export-and-the-overwrite/m-p/1112467#M6774</link>
    <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/254600"&gt;@AaronKoelker&lt;/a&gt;thanks for reporting. This could be a bug on the Python API. The server does not actually support overwriting in this operation. I have logged a bug and we will investigate.&lt;/P&gt;</description>
    <pubDate>Fri, 29 Oct 2021 16:52:17 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2021-10-29T16:52:17Z</dc:date>
    <item>
      <title>AGOL Notebook - item.export and the 'overwrite' argument</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/agol-notebook-item-export-and-the-overwrite/m-p/1112440#M6772</link>
      <description>&lt;P&gt;I'm writing a notebook to export a hosted feature layer that I own as a File Geodatabase item into my content, using &lt;A href="https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#arcgis.gis.Item.export" target="_self"&gt;item.export&lt;/A&gt; . This would be used as a backup copy of my data, if needed. I then intend to schedule a task for the notebook to have it run on its own periodically -- and ideally would like it to overwrite the previously-created backup each time so that I don't have a bunch of items creating clutter and taking up storage space.&lt;/P&gt;&lt;P&gt;The item.export method has an argument for 'overwrite' to replace an existing item, which sounds like what I need, however I'm either misunderstanding it or it doesn't seem to be working correctly. When I set overwrite = true, it just creates a new item in my content with the same exact name.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;dataitem = gis.content.get(#itemid)
backupname = f'{dataitem.title} Backup'
dataitem.export(backupname, 'File Geodatabase', parameters=None, wait=True, overwrite=True)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any idea if this is a bug or intended behavior? Is there a better way to do this? I tried using a search to locate the previous backup so I could just delete it -- but even when feeding in the exact item name, it would occasionally pull up some other item in the org.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Oct 2021 16:34:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/agol-notebook-item-export-and-the-overwrite/m-p/1112440#M6772</guid>
      <dc:creator>AaronKoelker</dc:creator>
      <dc:date>2021-10-29T16:34:00Z</dc:date>
    </item>
    <item>
      <title>Re: AGOL Notebook - item.export and the 'overwrite' argument</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/agol-notebook-item-export-and-the-overwrite/m-p/1112467#M6774</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/254600"&gt;@AaronKoelker&lt;/a&gt;thanks for reporting. This could be a bug on the Python API. The server does not actually support overwriting in this operation. I have logged a bug and we will investigate.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Oct 2021 16:52:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/agol-notebook-item-export-and-the-overwrite/m-p/1112467#M6774</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-10-29T16:52:17Z</dc:date>
    </item>
    <item>
      <title>Re: AGOL Notebook - item.export and the 'overwrite' argument</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/agol-notebook-item-export-and-the-overwrite/m-p/1112528#M6775</link>
      <description>&lt;P&gt;Bummer, but thanks for the info.&lt;/P&gt;&lt;P&gt;If anyone has any ideas for how to achieve this through another method, I'd love to hear it. Looking into using item.update now.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Oct 2021 19:30:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/agol-notebook-item-export-and-the-overwrite/m-p/1112528#M6775</guid>
      <dc:creator>AaronKoelker</dc:creator>
      <dc:date>2021-10-29T19:30:24Z</dc:date>
    </item>
    <item>
      <title>Re: AGOL Notebook - item.export and the 'overwrite' argument</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/agol-notebook-item-export-and-the-overwrite/m-p/1113292#M6779</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/254600"&gt;@AaronKoelker&lt;/a&gt;we decided to remove this parameter in v2.0 of the API (future release). The reason being, server does not establish a comprehensive item2item relationship for this use case, without which the API does not know which item to overwrite. Besides, we have a cardinality issue where a source item gets exported multiple times and the API would not know which of those items to overwrite.&lt;/P&gt;&lt;P&gt;The alternate is for you (as the user) to determine which item needs to be updated and call the &lt;A href="https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#arcgis.gis.Item.update" target="_blank"&gt;https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#arcgis.gis.Item.update&lt;/A&gt; (DestinationItem.update()) method on it and pass the file that was returned from the SourceItem.export() call.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Nov 2021 16:56:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/agol-notebook-item-export-and-the-overwrite/m-p/1113292#M6779</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-11-02T16:56:10Z</dc:date>
    </item>
    <item>
      <title>Re: AGOL Notebook - item.export and the 'overwrite' argument</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/agol-notebook-item-export-and-the-overwrite/m-p/1113327#M6780</link>
      <description>&lt;P&gt;Thanks for the update! I was able to get the item.update method to work for my purposes. Appreciate you taking a look.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Nov 2021 17:47:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/agol-notebook-item-export-and-the-overwrite/m-p/1113327#M6780</guid>
      <dc:creator>AaronKoelker</dc:creator>
      <dc:date>2021-11-02T17:47:20Z</dc:date>
    </item>
    <item>
      <title>Re: AGOL Notebook - item.export and the 'overwrite' argument</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/agol-notebook-item-export-and-the-overwrite/m-p/1173127#M7385</link>
      <description>&lt;P&gt;Hi Aaron. I'm trying to do the same thing, but I haven't figured it out. Would you mind sharing your script? Much appreciated if you would.&lt;/P&gt;</description>
      <pubDate>Wed, 11 May 2022 23:54:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/agol-notebook-item-export-and-the-overwrite/m-p/1173127#M7385</guid>
      <dc:creator>heatherdaw</dc:creator>
      <dc:date>2022-05-11T23:54:25Z</dc:date>
    </item>
    <item>
      <title>Re: AGOL Notebook - item.export and the 'overwrite' argument</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/agol-notebook-item-export-and-the-overwrite/m-p/1173344#M7390</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/74723"&gt;@heatherdaw&lt;/a&gt;&amp;nbsp; this is the full script I'm using to create a backup fgdb of a hosted feature layer in my content. It overwrites the existing backup each time. Hope that helps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS
from time import strftime
gis = GIS("home")
username = gis.properties.user.username
home_dir = os.path.abspath(os.path.join(os.sep, 'arcgis', 'home'))

# Variables
itemid = "000abc000abc000abc000abc000abc00" # AGO layer to backup
itemtype = "File Geodatabase" # export format
itemtags = "backup, fgdb" # item tags
itemdesc = strftime("This backup was generated on %m/%d/%Y.") # item description
folderlocation = "/" # export folder, / is root folder

# Check to see if a backup file geodatabase already exists
dataitem = gis.content.get(itemid) #fetch the item to be backed-up
backupname = f"{dataitem.title} Backup" #acquire backup name using the source hosted feature layer
searchquery = f"title:{backupname} AND owner:{username}" # search query using item title and owner
searchresult = gis.content.search(query=searchquery, item_type=itemtype, sort_field='uploaded', sort_order='desc') #search for existing backups

print(f'Found {len(searchresult)} existing backups')
# Update existing backup item, if it exists
if len(searchresult) &amp;gt;= 1:
    print('Updating existing backup file...')
    
    dataitem = gis.content.get(itemid) #fetch the item to be backed-up
    backupname = f'{dataitem.title} Backup' #create a name for the backup using the existing item name
    dataitem.export(backupname, itemtype, parameters=None, wait=True, tags=itemtags, snippet = itemdesc, overwrite=True) #export the data to a file geodatabase within the user's content
    
    searchquery = f"title:{backupname} AND owner:{username}" # search query using item title and owner
    searchresult = gis.content.search(query=searchquery, item_type=itemtype, sort_field='uploaded', sort_order='desc') #find the new temporary backup item that was just created
    backupitem = gis.content.get(searchresult[0].itemid)
    dwnldname = backupname.replace(" ","_")
    backupitem.download(save_path=home_dir, file_name=dwnldname + '.zip')
    
    searchquery = f"title:{backupname} AND owner:{username}" # search query using item title and owner
    searchresult = gis.content.search(query=searchquery, item_type=itemtype, sort_field='uploaded', sort_order='asc') #search for the previous backup
    oldbackup = gis.content.get(searchresult[0].itemid)
    itemprops = {"snippet":f"{itemdesc}","description":f"{itemdesc}"}
    oldbackup.update(item_properties=itemprops, data = os.path.join(home_dir, dwnldname, '.zip'))

    backupitem.delete(dry_run=False) #Can change dry_run to True for testing, won't remove duplicate backups
    
#Create a new backup if no previous backup exists   
else:
    print('Creating new backup...')
    
    dataitem = gis.content.get(itemid) #fetch the item to be backed-up
    backupname = f'{dataitem.title} Backup' #create a name for the backup using the existing item name
    dataitem.export(backupname, itemtype, parameters=None, wait=True, tags=itemtags, snippet = itemdesc) #export the data to a file geodatabase within the user's content
    
    ## Move the backup to designated folder, if not root
    searchquery = f"title:{backupname} AND owner:{username}" # search query using item title and owner
    searchresult = gis.content.search(backupname, item_type=itemtype) #find the backup that was just created
    backupitem = gis.content.get(searchresult[0].itemid) #get the itemid of that new item
    if folderlocation != "/":
        print('Moving backup to designated folder...')
        backupitem.move(folder=folderlocation) #move the item to the correct user folder, if user chose somewhere other than the default root&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jan 2024 16:50:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/agol-notebook-item-export-and-the-overwrite/m-p/1173344#M7390</guid>
      <dc:creator>AaronKoelker</dc:creator>
      <dc:date>2024-01-10T16:50:25Z</dc:date>
    </item>
    <item>
      <title>Re: AGOL Notebook - item.export and the 'overwrite' argument</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/agol-notebook-item-export-and-the-overwrite/m-p/1173347#M7391</link>
      <description>&lt;P&gt;Thanks so much for the swift reply! I'll take a look at this.&lt;/P&gt;</description>
      <pubDate>Thu, 12 May 2022 14:57:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/agol-notebook-item-export-and-the-overwrite/m-p/1173347#M7391</guid>
      <dc:creator>heatherdaw</dc:creator>
      <dc:date>2022-05-12T14:57:18Z</dc:date>
    </item>
    <item>
      <title>Re: AGOL Notebook - item.export and the 'overwrite' argument</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/agol-notebook-item-export-and-the-overwrite/m-p/1173370#M7392</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/74723"&gt;@heatherdaw&lt;/a&gt;sorry I forgot the top bit of the script which imports strftime and defines the username and home directory. Edited the original post above to include those at the top.&lt;/P&gt;</description>
      <pubDate>Thu, 12 May 2022 15:32:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/agol-notebook-item-export-and-the-overwrite/m-p/1173370#M7392</guid>
      <dc:creator>AaronKoelker</dc:creator>
      <dc:date>2022-05-12T15:32:50Z</dc:date>
    </item>
  </channel>
</rss>

