Create a Folder in ArcGIS Server Manager (Portal) with the ArcGIS API for Python

Table of Contents

Introduction

Today’s blog post is inspired by a question posed on Esri Communities. The question asks how to “Create an arcgis server folder with the python API?”. We often see comments that the documentation for the ArcGIS API for Python can be difficult to follow, so let’s take a look at approaching the answer to this question from using the documentation. The user who posted the question was quite close to achieving their goal with one step missing. 

Unlock the full potential of ArcGIS Online by mastering the art of efficient Content Management with the ArcGIS API for Python. In this comprehensive course, you will embark on a journey to streamline your geospatial workflows, enhance data organization, and maximize the impact of your ArcGIS Online platform.

Geospatial Professionals, GIS Analysts, Data Managers, and enthusiasts will discover the power of automation and script-based operations to efficiently manage content in ArcGIS Online. Throughout this course, you will gain practical, hands-on experience in leveraging the ArcGIS API for Python to perform a wide range of content management tasks with ease.

The Code

Here we have the code required to complete the task. In the following sections of this blog post we will assess using the ArcGIS API Reference for writing the code on lines 7 and 10 below.

				
					from arcgis.gis import GIS

## access Portal
portal = GIS("home") # can also use url, username, password

## get the server of interest
server = portal.admin.servers.list()[0]

## access the server ServiceManager and create folder
status = server.services.create_folder("MY_NEW_FOLDER")

print(status) # True if successful, False if a failure.
				
			

The Outcome

ArcGIS Server New Folder by ArcGIS API for Python

The ArcGIS API for Python Reference

Below, we have the home page to the API Reference (2.4.0 at time of writing). The Search topics option to the left of the screen is a good way to find the functionality you desire (although it can lack if you are not as specific as it wants you to be).

The ArcGIS API for Python Reference Homepage

Search

Let’s use the Search topics input and type “create folder” and see what it comes up with. Just type the search words and do not hit enter. We can see there are multiple entries for create_folder. Keeping in mind that we want to create a folder for ArcGIS Server, the bottom one looks like the most obvious choice. This create_folder function is available from the ServiceManager class from the arcgis.gis.server module, all the items to the right of the > are classes available in the ArcGIS API for Python.

Click the create_folder option for the ServiceManager.

ArcGIS API for Python Reference Search Topics

The create_folder in ServiceManager

We are brought to location of the create_folder method for the ServiceManager.

The create_folder method has two parameters, one required; the folder name as a string, and one optional; the description of the folder also a string.

If the creation of the folder is successful, True is returned, otherwise, False. 

ArcGIS API for Python Reference create_folder method for the ServiceManager from the server module

Let’s begin building our Python script below…

				
					from arcgis.gis import GIS

## access Portal
portal = GIS("home") # can also use url, username, password

## get the server of interest
server = # insert code here

## Work-in-progress: call the create_folder() method
status = create_folder(folder_name="MY_NEW_FOLDER")

print(status) # True if successful, False if a failure.

				
			

The ServiceManager Class

While in the documentation, scroll up a little from the create_folder entry (just above it) and we find the ServiceManager class. We are tracing back to see how we access the create_folder from the ServiceManager. We see from the documentation that “An instance of this class, called ‘services’, is available as a property of the Server object”. So we access the ServerManager via the services property of the Server object. Well, let’s find the Server class.

ArcGIS API for Python Reference ServiceManager class

The Server Class

Type “Server” into the Search topics on the left-hand-side and select the arcgis.gis.server module. Scroll down through the entries under the Server class until you find the services property. (you could also have just searched for services)

ArcGIS API for Python Reference Server Class

services Property of the Server Class

The services property returns a ServiceManager object that we have already seen, and this ServiceManager gives access to the create_folder method.

services (ServiceManager) > create_folder

Now we know that we need to create a Server object, access the services property, and utilise the create_folder method. But how do we get our Server object?

ArcGIS API for Python Reference Server Class services property

Let’s first update our script. We know we access the create_folder method via the ServiceManager, which is the services property of a Server object. So let’s add the services property in front of the create_folder method.

				
					from arcgis.gis import GIS

## access Portal
portal = GIS("home") # can also use url, username, password

## get the server of interest
server = # insert code here

## Work-in-progress: call the create_folder() method from the services property
status = services.create_folder(folder_name="MY_NEW_FOLDER")

print(status) # True if successful, False if a failure.

				
			

The ServerManager Class

Scroll back up to the description for the Server Class. We can see that we can get an instance of a Server object using the ServerManager list() or get() methods.

ArcGIS API for Python Reference Server Class Description

So off we go to the ServerManager! The documentation states that we access the ServerManager via the servers property from the gis.admin object. In our Python code, the portal variable is our gis.

ArcGIS API for Python Reference ServerManager Class

According to the Server class description, the ServerManager has a list() and get() methods that we are interested in. 

ArcGIS API for Python Reference SeverManager class list and get methods

Let’s update our script. We are working with line 7 below where we access our GIS object (portal), the docs tell us that our GIS has an admin module and this admin module has a property called servers that is a ServerManager class. We know from above that the ServerManager class has a method called list to return all Server objects available in a list. At this point you would print the list to screen and note the index of the server of interest. For our example, we are interested in the first server returned, hence the [0] after the list() call. 

				
					from arcgis.gis import GIS

## access Portal
portal = GIS("home") # can also use url, username, password

## Work-in-progress: from the Portal (GIS) access the admin module and the nthe servers property - which is a ServerManager object
server = portal.admin.servers.list()[0]

## Work-in-progress: call the create_folder() method from the services property
status = services.create_folder(folder_name="MY_NEW_FOLDER")

print(status) # True if successful, False if a failure.
				
			

The Final Leg!

We are almost there!! Now that we have our Server object in line 7, we need to finalise line 10. Casting or eyes back to the Server object documentation, we can see that it has a services property (ServiceManager), which in turn has our create_folder() method. So we place our server object variable at the front of our code for line 10. And that’s it. 

				
					from arcgis.gis import GIS

## access Portal
portal = GIS("home") # can also use url, username, password

## GIS > admin > ServerManager > list() - return a list of Server objects
server = portal.admin.servers.list()[0]

## Server object > ServiceManager > create_folder
status = server.services.create_folder(folder_name="MY_NEW_FOLDER")

print(status) # True if successful, False if a failure.
				
			

While the documentation can be a little cumbersome to navigate, you can dig in there and find how it all pieces together using a combination of the Search topics function along with the Module and Class descriptions.

Geospatial Professionals, GIS Analysts, and enthusiasts will discover the power of automation and script-based operations to efficiently interact and update WebMaps in ArcGIS Online. Throughout this course, you will gain practical, hands-on experience in leveraging the ArcGIS API for Python to perform a wide range of WebMap tasks with ease.

We will dissect WebMaps for all that they are and by the end of this course you will be comfortable with manipulating the JSON, which is the behind the scenes configuration of a WebMap, to produce scripts for workflows where there is currently no API Python method, such as grouping layers.

Leave a Comment

Your email address will not be published. Required fields are marked *