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

Last Published: April 7, 2023

Summary

There are situations 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

  1. Import the necessary modules.
from arcgis.gis import GIS,ContentManager
import datetime
  1. 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.
  1. 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']
  1. 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)
  1. 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)
  1. 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")

Article ID: 000029281

Software:
  • Portal for ArcGIS
  • ArcGIS Web Adaptor

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options