How to publish many mxd files in ArcGIS Server 10.1 automatically?

I have about 60 mxd files and I want to publish them automatically. Is there any way to do this?

  • Btelliot, your code also works 10.2! There is obviously a need to define some details concerning the service, but this is an amazing starting point!!! 10X
    – user27019
    Feb 17 '14 at 9:51

you can use arcpy for publishing your mxd files.

for listing your mxd files use os.walk :

import os
import arcpy

path= r"c:\path"
for (path, dirs, files) in os.walk(path):
  for fl in files:
     if fl.lower().endswith(".mxd"):
       mxd = arcpy.mapping.MapDocument(os.path.join(path, fl))
       print mxd.filePath

and then go this way:

1. AnalyzeForMSD() function (info): Analyzes map documents (.mxd) to determine sources for potential suitability and performance issues when converting a map to a map service definition (MSD) file.


import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\ReadyForMSD.mxd")
analysis = arcpy.mapping.AnalyzeForMSD(mxd)

2. ConvertToMSD() function (info) : Converts a map to a map service definition (MSD) file.


import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\ReadyForMSD.mxd")
msd = r"C:\Project\Output\Project.msd"
df = arcpy.mapping.ListDataFrames(mxd, "County Maps")[0]
arcpy.mapping.ConvertToMSD(mxd, msd, df, "NORMAL", "NORMAL")
del mxd, msd

3. PublishMSDToServer() function (info) : Publishes an existing map service definition (MSD) file to a designated ArcGIS Server.


import arcpy
msd = r"C:\Project\Project.msd"
arcpy.mapping.PublishMSDToServer (msd, "http://<MyServer>/arcgis/services", 
              "<MyServer>", "MyMapService", "MyMapServiceFolder", ["WMS", "KML"])

the last thing is you should do that merge all funcitons for your needs. actually there is a tutorial which can help u here about Use ArcPy Mapping to Publish Map Documents to ArcGIS Server. it is similar to my documentation...

To summarize(image is from link which i give above):


i hope it helps you...

  • Its will by work with ags 10.1? In 10.1 is *.sd instead of *.msd.
    – user7172
    Aug 14 '12 at 9:39
  • yes this is for 10 but you can update the new arcpy for 10.1...
    – Aragon
    Aug 14 '12 at 9:48
  • 1
    Thanks Aragon, this was very helpful! Just finished auto-publishing over 130 MXDs. :) Just for posterity, the 10.1 workflow is: arcpy.mapping.CreateMapSDDraft -> arcpy.StageService_server -> arcpy.UploadServiceDefinition_server
    – Mintx
    May 3 '13 at 19:50

if you want do it in c# you can see in my blog: http://nicogis.blogspot.it/2012/10/ags-101-restful-administrative-api.html

for conversion mxd -> msd you can use python called from c#


Aragon has a great answer, but unfortunately it doesn't work for ArcGIS / ArcServer 10.1.

The new method I just tried is based off of the ArcGIS 10.1 help. You can find the link to the help file here: http://resources.arcgis.com/en/help/main/10.1/index.html#//00s30000006q000000

Here is how I used the code to publish MXDs based on a specified folder:

#import modules
import arcpy, sys, os, string

#specify folder containing MXDs
inFolder = raw_input("Please enter folder containing 10.1 MXDs to Publish to ArcServer: ")

#specify connection File Path
connectionFilePath = r'C:\Users\<your user name>\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\<your connection file location.ags>'

#look in folder for mxds
MapPath= []
MapFolder = os.listdir(inFolder)
for file in MapFolder:
    fileExt = os.path.splitext(file)[1]
    if fileExt == ".mxd":
        MapPath = os.path.join(inFolder, file)
        file = MapPath.strip('\'')
        mxd = arcpy.mapping.MapDocument(file)
        base = os.path.basename(file)
        serviceName = base[:-4]
        SDDraft = file[:-4] + ".sddraft"
        sd = file[:-4] + ".sd"

        #Create Map SD Draft
        print "\n" + "Publishing: " + base
        analysis = arcpy.mapping.CreateMapSDDraft(mxd, SDDraft, serviceName, "FROM_CONNECTION_FILE", connectionFilePath, "False", <Service Folder Name>,  "None", "None")

        # stage and upload the service if the sddraft analysis did not contain errors
        if analysis['errors'] == {}:
            # Execute StageService
            print "Staging Service"
            arcpy.StageService_server(SDDraft, sd)
            # Execute UploadServiceDefinition
            print "Uploading Service Definition"
            arcpy.UploadServiceDefinition_server(sd, connectionFilePath)
            print "Publishing " + base +" succeeded" + "\n"
            # if the sddraft analysis contained errors, display them
            print analysis['errors']

There might be a simpler way to use this code, but it works for me. Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.