Publish a Hosted Feature Service to ArcGIS Online from ArcGIS Pro using ArcPy

Table of Contents

Introduction

In this post we will provide the code to use ArcPy to access an APRX, select a Map, and publish the layer contents of that Map to ArcGIS Online as a Hosted Feature Service.

Leverage ArcPy for geospatial data management workflows within ArcGIS Pro. Learn the fundamentals of utilising ArcGIS Pro geoprocessing tools with ArcPy for data management, conversions, and analysis. This course is packed with amazing content that will help you discover the power of automating tasks within the ArcGIS Pro environment. Take your ArcPy skills from beginner to snake charmer. A little code goes a long way, a little ArcPy provides you with an in-demand skill. Sign up now for our highly rated course.

Import the ArcPy module

ArcPy is a Python site package that enables automation, analysis, and management of geographic data within the ArcGIS software environment. Import os from the standard Python library to help with some filepaths.

				
					import arcpy
import os
				
			

Define the inputs

We need a handful of user inputs to define the filepath to the APRX, the name of the Map inside the APRX that contains the information to publish, a staging folder for the definition files generated, and tags, a description, and summary for our newly created Hosted Feature Service.

				
					## the path to the APRX that contains the Map with the layers to publish
aprx_path = r"C:\path\to\project\your_project.aprx"

## the name of the Map in the APRX that contains the layers to publish
map_name = "NAME OF MAP"

## a folder to stage the definition files
staging_fldr = r"C:\path\to\staging_folder"

## some tags for our Hosted Feature Service
tags = "tag1,tag2,tag3"

## a description for our Hosted Feature Service
description = "This is our description"

## a summary for our Hosted Feature Service
summary = "This is our summary"
				
			

Access the ArcGIS Pro components

We’ll start by accessing the APRX and the Map of interest.

				
					## access the APRX
aprx = arcpy.mp.ArcGISProject(aprx_path)

## access the correct map
m = aprx.listMaps(map_name)[0]
				
			

Define the Staging definition filepaths

Two files will be generated during the process, the first is a .sddraft file which is then converted into a .sd file, which in-turn is used to help publish the Feature Service. The Snippet below is simply defining the filepaths for the two files and if they already exist, deleting them so they can be created fresh.

				
					## the service definition filenames and output paths
sddraft_output_filepath = os.path.join(staging_fldr, f"{map_name}.sddraft")
sd_output_filepath = os.path.join(staging_fldr, f"{map_name}.sd")

## if either of the output files already exists, then delete
if arcpy.Exists(sddraft_output_filepath):
    arcpy.Delete_management(sddraft_output_filepath)

if arcpy.Exists(sd_output_filepath):
    arcpy.Delete_management(sd_output_filepath)
				
			

Create the sddraft file and alter some properties

First up, we create the .sddraft file where we are defining that it is a Hosted Feature Service that we wish to publish. Here, we also set the service name withing the getWebLayerSharingDraft() tool.

This tool returns a FeatureSharingDraft object for us and the ability to set some properties for that object. Below, we set the description, summary, and tags from our input.

And our last step here is to export as the .sddraft file.

				
					## use the Map object getWebLayerSharingDraft method
## returns a FeatureSharingDraft object
sd_draft = m.getWebLayerSharingDraft(
    server_type = "HOSTING_SERVER",
    service_type = "FEATURE",
    service_name = map_name
)

## alter some properties of the sd_draft
sd_draft.description = description
sd_draft.summary = summary
sd_draft.tags = tags

## create the Service Definition Draft file
sd_draft.exportToSDDraft(sddraft_output_filepath)
				
			

Stage the Service

Next, we stage the service, we are converting the .sddraft into the full-blown .sd file.

				
					## stage the service
arcpy.server.StageService(
    in_service_definition_draft = sddraft_output_filepath,
    out_service_definition = sd_output_filepath
)
				
			

Publish to ArcGIS Online

Here we are at the main event already! It is time to publish the Hosted Feature Service to ArcGIS Online. We call upon the UploadServiceDefinition tool to do the job for us, supllying in the filepath to our .sd file and the type of server we are publishing to. 

				
					## publish to agol
arcpy.server.UploadServiceDefinition(
    in_sd_file = sd_output_filepath,
    in_server = "HOSTING_SERVER"
)

				
			

ArcGIS Pro Definition Query Masterclass with ArcPy & Python

Unlock the power of Definition Queries in ArcGIS Pro through this comprehensive course that dives deep into the intricacies of managing and utilizing definition queries with spatial data using ArcPy and Python scripting. Definition Queries provide a dynamic way to filter and display specific subsets of your data, enabling you to create more insightful maps and analyses. In this course, we will focus exclusively on Definition Queries, covering their creation, modification, and application through hands-on exercises and real-world scenarios. Accredited by the Association for Geographic Information (AGI) for 1 CPD point.

All the code in one place

You can find the entire code workflow below with links to important components in the documentation that were used.

				
					import arcpy
import os

################################################################################
## Esri Documentation:
##  https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/arcgisproject-class.htm
##  https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/map-class.htm
##  https://pro.arcgis.com/en/pro-app/latest/arcpy/sharing/featuresharingdraft-class.htm
##  https://pro.arcgis.com/en/pro-app/latest/tool-reference/server/stage-service.htm
##  https://pro.arcgis.com/en/pro-app/latest/tool-reference/server/upload-service-definition.htm
##
##
## ArcGIS Pro Version 3.2.0
################################################################################

################################################################################
## INPUT REQUIRED  #############################################################

## the path to the APRX that contains the Map with the layers to publish
aprx_path = r""

## the name of the Map in the APRX that contains the layers to publish
map_name = "MAP NAME"

## a folder to stage the definition files
staging_fldr = r""

## some tags for our Hosted Feature Service
tags = "tag1,tag2,tag3"

## a description for our Hosted Feature Service
description = "This is our description"

## a summary for our Hosted Feature Service
summary = "This is our summary"


################################################################################
## ACCESS ARCGIS PRO COMPONENTS  ###############################################

## access the APRX
aprx = arcpy.mp.ArcGISProject(aprx_path)

## access the correct map
m = aprx.listMaps(map_name)[0]


################################################################################
## STAGING #####################################################################

## the service definition filenames and output paths
sddraft_output_filepath = os.path.join(staging_fldr, f"{map_name}.sddraft")
sd_output_filepath = os.path.join(staging_fldr, f"{map_name}.sd")

## if either of the output files already exists, then delete
if arcpy.Exists(sddraft_output_filepath):
    arcpy.Delete_management(sddraft_output_filepath)

if arcpy.Exists(sd_output_filepath):
    arcpy.Delete_management(sd_output_filepath)


################################################################################
## MANIPULATE THE SD DRAFT ######################################################

## use the Map object getWebLayerSharingDraft method
## returns a FeatureSharingDraft object
sd_draft = m.getWebLayerSharingDraft(
    server_type = "HOSTING_SERVER",
    service_type = "FEATURE",
    service_name = map_name
)

## alter some properties of the sd_draft
sd_draft.description = description
sd_draft.summary = summary
sd_draft.tags = tags
sd_draft.allowExporting = True

## create the Service Definition Draft file
sd_draft.exportToSDDraft(sddraft_output_filepath)


################################################################################
## STAGE THE SERVICE ###########################################################

## stage the service
arcpy.server.StageService(
    in_service_definition_draft = sddraft_output_filepath,
    out_service_definition = sd_output_filepath
)


################################################################################
## PUBLISH TO ARGIS ONLINE #####################################################

## publish to agol
arcpy.server.UploadServiceDefinition(
    in_sd_file = sd_output_filepath,
    in_server = "HOSTING_SERVER"
)


################################################################################
print("\nSCRIPT COMPLETE")

				
			

Leave a Comment

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