Skip to content

Instantly share code, notes, and snippets.

@sofferm
Created December 13, 2013 10:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sofferm/7942506 to your computer and use it in GitHub Desktop.
Save sofferm/7942506 to your computer and use it in GitHub Desktop.
Publishes all mxd files in a folder to running arcgis services.
import arcpy
import os.path
# define local variables
wrkspc = r'C:\data\service_definitions'
arcpy.env.workspace = wrkspc
con = 'GIS Servers/arcgis on localhost_6080 (admin).ags'
for file in arcpy.ListFiles("*.mxd"):
file = os.path.join(wrkspc, file)
mapDoc = arcpy.mapping.MapDocument(file)
service = os.path.splitext(os.path.basename(file))[0]
sddraft = os.path.join(wrkspc, service + '.sddraft')
sd = os.path.join(wrkspc, service + '.sd')
summary = service
tags = 'my service'
# create service definition draft
analysis = arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, service, 'ARCGIS_SERVER',
None, False, 'property', summary, tags)
# stage and upload the service if the sddraft analysis did not contain errors
if analysis['errors'] == {}:
# Execute StageService
arcpy.StageService_server(sddraft, sd)
# Execute UploadServiceDefinition
arcpy.UploadServiceDefinition_server(sd, con)
else:
# if the sddraft analysis contained errors, display them
print analysis['errors']
print 'done'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment