Upload Service Definition (Server)
Summary
Uploads and publishes a GIS service to a specified GIS server based on a staged service definition (.sd) file.
Usage
-
This tool uploads and publishes a GIS service based on the input service definition. Whenever you share a service using ArcGIS for Desktop, this tool is run, and you will see a result in the geoprocessing Results window.
This tool does not upload and publish service definition draft (.sddraft) files. If you have a service definition draft, you can convert it to a staged service definition by using the Stage Service tool.
You can create an ArcGIS for Server connection using ArcGIS for Desktop by making a publisher connection.
You can use the Sign In To Portal tool to connect to an ArcGIS Online portal.
Syntax
Parameter | Explanation | Data Type |
in_sd_file |
The service definition (.sd) contains all the information needed to publish a GIS service. | File |
in_server |
You can use ArcGIS for Server connections listed under the GIS Servers node in the Catalog window, or you can navigate to a different folder where you might have server connection files stored. If you are connecting to ArcGIS Online, make sure you type My Hosted Services for the server connection with each word capitalized and a space between each word. | ServerConnection |
in_service_name (Optional) |
Use this to override the service name currently specified in the service definition with a new name. | String |
in_cluster (Optional) |
Use this if you want to change the cluster to which the service has been assigned. You must choose from clusters that are available on the specified server. | String |
in_folder_type [in_folder_type,...] (Optional) |
Folder type is used to determine the source for the folder. The default is to get a folder from the service definition. You can also choose to get a list of folders already existing on the specified server, or you can specify a new folder to be created once you publish this service.
| String |
in_folder (Optional) |
Use this to specify the folder for the service. The default is to use the folder specified in the service definition. If you chose the NEW folder type, you use this parameter to enter a new folder name. If you chose the EXISTING folder type, you can choose from the existing folders on the server. | String |
in_startupType (Optional) | Use this to determine the start/stop state of the service immediately after publishing.
| Boolean |
in_override (Optional) | Use this parameter if you want to override the sharing properties set in the service definition. These properties define if, and how, you are sharing your service with ArcGIS Online. Sharing your service with ArcGIS Online exposes it for others to use.
You must be logged in to ArcGIS Online in order to override sharing properties. | Boolean |
in_my_contents (Optional) | All shared services are available through My Contents. Even if you only want to share with a specific group in your organization, the service will also be shared through My Contents.
You must be logged in to ArcGIS Online in order to override sharing properties. | Boolean |
in_public (Optional) | Choose whether or not your service will be available to the public.
You must be logged in to ArcGIS Online in order to override sharing properties. | Boolean |
in_organization (Optional) | You can share your service with your organization.
You must be logged in to ArcGIS Online in order to override sharing properties. | Boolean |
in_groups [group_name,...] (Optional) | A list of group names with which to share the service. You must be logged in to ArcGIS Online in order to override sharing properties. | String |
Code Sample
Uploads and publishes a service definition to a specified server.
import arcpy
from arcpy import env
env.workspace = "C:/data"
arcpy.UploadServiceDefinition_server("myMapService.sd", "GIS Servers/myServerConnection")
The following script demonstrates a publishing workflow using Stage Service and Upload Service Definition.
# Name: StageService_UploadServiceDefinition_example2.py
# Description: Use a service definition draft to create a service definition
# and then upload and publish that service definition.
# Requirements: Connection to an ArcGIS Server, Spatial Data Server, or My Hosted Services
# Import system modules
import arcpy
from arcpy import env
# Set environment settings
env.workspace = "C:/data"
# Set local variables
inServiceDefinitionDraft = "myMapService.sddraft"
outServiceDefinition = "myMapService.sd"
# Execute StageService
arcpy.StageService_server(inServiceDefinitionDraft, outServiceDefinition)
# Set local variables
inSdFile = outServiceDefinition
inServer = "GIS Servers/myServerConnection"
# Execute UploadServiceDefinition
arcpy.UploadServiceDefinition_server(inSdFile, inServer)
The following script loops through all service definitions in a folder and publishes each one to an ArcGIS Server.
# Name: UploadServiceDefinition_example3.py
# Description: Upload and publish all service definitions contained in a folder
# Requirements: Connection to an ArcGIS Server, Spatial Data Server,
# or My Hosted Services
# Import system modules
import arcpy
from arcpy import env
# Set environment settings
env.workspace = "C:/data"
# Set local variable
inServer = "myServerConnection.ags"
print "Publishing to " + inServer
# Find all the service definitions (.sd or .sds) in a workspace and
# upload\publish each one to an ArcGIS Server, Spaital Data Server, or My Hosted Services
sdList = arcpy.ListFiles("*.sd")
for inSdFile in sdList:
print "Publishing " + sdName
try:
arcpy.UploadServiceDefinition_server(inSdFile, inServer)
except Exception, e:
print e.message
The following script uploads an existing service definition and uses optional parameters to modify some properties of the service.
# Name: UploadServiceDefinition_example5.py
# Description: Uploads an existing service definition and uses optional
# parameters to modify some details of the service
# Requirements: Connection to an ArcGIS Server, Spatial Data Server,
# or My Hosted Services
# Import system modules
import arcpy
from arcpy import env
# Set environment settings
env.workspace = "C:/data"
# Set local variables
inSdFile = "myMapService.sd"
inServer = "myServerConnection.ags"
inServiceName = "newServiceName"
inCluster = "myCluster"
inFolderType = "NEW"
inFolder = "newFolder"
inStartup = "STOPPED"
inOverride = "OVERRIDE_DEFINITION"
inMyContents = "SHARE_ONLINE"
inPublic = "PRIVATE"
inOrganization = "NO_SHARE_ORGANIZATION"
inGroups = "My Group"
# Execute UploadServiceDefinition
arcpy.UploadServiceDefinition_server(inSdFile, inServer, inServiceName,
inCluster, inFolderType, inFolder,
inStartup, inOverride, inMyContents,
inPublic, inOrganization, inGroups)
The following script creates and uploads a service definition that can be used to overwrite an existing service.
# Name: StageService_example3_UploadServiceDefinition_example4.py
# Description: Creates a service definition that can be used to overwrite an
# existing service. When this service definition is published it
# will overwrite the existing service.
# Requirements: Connection to an ArcGIS Server, Spatial Data Server,
# or My Hosted Services
# Import system modules
import arcpy
from arcpy import env
import xml.dom.minidom as DOM
# Set environment settings
env.workspace = "C:/data"
# Set local variables
inServiceDefinitionDraft = "myMapService.sddraft"
outServiceDefinition = "myMapService.sd"
newType = 'esriServiceDefinitionType_Replacement'
xml = draftPath + in_sd_draft
doc = DOM.parse(xml)
descriptions = doc.getElementsByTagName('Type')
for desc in descriptions:
if desc.parentNode.tagName == 'SVCManifest':
if desc.hasChildNodes():
desc.firstChild.data = newType
outXml = xml
f = open(outXml, 'w')
doc.writexml( f )
f.close()
# Execute StageService
arcpy.StageService_server(inServiceDefinitionDraft, outServiceDefinition)
# Set local variables
inSdFile = outServiceDefinition
inServer = "GIS Servers/myServerConnection"
# Execute UploadServiceDefinition
arcpy.UploadServiceDefinition_server(inSdFile, inServer)