How To: Organize items in the Portal for ArcGIS root folder into specific folders based on the last modified date using ArcGIS API for Python
Summary
There are incidences when items pile up in root folders users in Portal for ArcGIS. When dealing with multiple items, it is often necessary to have the items organized so they can be identified according to their usage. This article describes how to order items in the root folder by their last modified date and moving them to a folder ordered by months using ArcGIS API for Python.
Procedure
- Import the necessary modules.
from arcgis.gis import GIS,ContentManager import datetime
- Define the credentials for Portal for ArcGIS.
ago_gis=GIS('https://domain.esri.com/portal','Username','Password', verify_cert=False)
Note: The verify_cert parameter is an optional boolean value. The default value is True if the parameter is not specified. If a site has an invalid SSL certificate or is accessed via the IP or hostname instead of the name on the certificate, set this value to False.
- Define the variables for the user, the root folder, and the name of the folders for the respective months.
me = ago_gis.users.me rootitems=me.items(folder=None,max_items=1000) year=['01-January', '02-February', '03-March', '04-April', '05-May', '06-June', '07-July', '08-August', '09-September', '10-October', '11-November', '12-December']
- Create the folders according to the name specified in Step 3.
year1=['01-January', '02-February', '03-March', '04-April', '05-May', '06-June', '07-July', '08-August', '09-September', '10-October', '11-November', '12-December'] for month in year1: ago_gis.content.create_folder(month)
- Create a list of items in the root folder and date modified.
DatemodifiedList=[] for i in rootitems: d=(i.modified/1000) dt = datetime.datetime.fromtimestamp(d) tempstring=str(dt.date())+'-'+i.title DatemodifiedList.append(tempstring)
- Move all items to folders for corresponding months of the last date modified.
for item in rootitems: epochtime=(item.modified/1000) date_time = datetime.datetime.fromtimestamp(epochtime) itemYear=date_time.year Month=date_time.month item.move(year[Month-1]) DatemodifiedList print ("Items are organized")
The following is a sample of the full script:
from arcgis.gis import GIS,ContentManager import datetime ago_gis=GIS('https://domain.esri.com/portal','User1','user123', verify_cert=False) me = ago_gis.users.me rootitems=me.items(folder=None,max_items=1000) year=['01-January', '02-February', '03-March', '04-April', '05-May', '06-June', '07-July', '08-August', '09-September', '10-October', '11-November', '12-December'] year1=['01-January', '02-February', '03-March', '04-April', '05-May', '06-June', '07-July', '08-August', '09-September', '10-October', '11-November', '12-December'] for month in year1: ago_gis.content.create_folder(month) DatemodifiedList=[] for i in rootitems: d=(i.modified/1000) dt = datetime.datetime.fromtimestamp(d) tempstring=str(dt.date())+'-'+i.title DatemodifiedList.append(tempstring) for item in rootitems: epochtime=(item.modified/1000) date_time = datetime.datetime.fromtimestamp(epochtime) itemYear=date_time.year Month=date_time.month item.move(year[Month-1]) DatemodifiedList print ("Items are organized")
Related Information
- ArcGIS API for Python: ContentManager
- ArcGIS API for Python: Accessing and managing users
- How To: Organize items in the ArcGIS Online root folder into specific folders based on the last modified date using ArcGIS API for Python
Last Published: 2/2/2023
Article ID: 000029281
Software: Portal for ArcGIS 10.9.1, 10.9