0

I have a folder of MXDs and I want to publish all of them in batch to a single ArcGIS Server. I've had success using Python on a single MXD, but when I add a loop into the code so that I can do multiple services at once, I run into errors that I'm unable to troubleshoot. I found the code below on ArcGIS Enterprise site and tried to modify it for batch processing.

import arcpy
import os

wrkspc = 'C:/Project/BULK/'
#mxd = arcpy.mapping.MapDocument('BULK_Test.mxd')

con = 'AGSconnectionfile.ags'

for (wrkspc, dirs, files) in os.walk(wrkspc):
    for fl in files:
        if fl.lower().endswith(".mxd"):
            
            mxd = arcpy.mapping.MapDocument(os.path.join(wrkspc, fl))
            # Provide other service details
            service = mxd
            sddraft = 'C:/Project/BULK/' + str(mxd) + '.sddraft'
            sd = 'C:/Project/BULK/' + str(mxd) + '.sd'
            summary = 'BULK_Test'
            tags = 'BULK_Test'

            # Create service definition draft
            arcpy.mapping.CreateMapSDDraft(mxd, sddraft, mxd, 'ARCGIS_SERVER', con, True, "Project", summary, tags)

            # Analyze the service definition draft
            analysis = arcpy.mapping.AnalyzeForSD(sddraft)

            # Print errors, warnings, and messages returned from the analysis
            print "The following information was returned during analysis of the MXD:"
            for key in ('messages', 'warnings', 'errors'):
                print '----' + key.upper() + '---'
                vars = analysis[key]
                for ((message, code), layerlist) in vars.iteritems():
                    print '    ', message, ' (CODE %i)' % code
                    print '       applies to:',
                    for layer in layerlist:
                        print layer.name,
                    print

            # Stage and upload the service if the sddraft analysis did not contain errors
            if analysis['errors'] == {}:
                # Execute StageService. This creates the service definition.
                arcpy.StageService_server(sddraft, sd)

                # Execute UploadServiceDefinition. This uploads the service definition and publishes the service.
                arcpy.UploadServiceDefinition_server(sd, con)
                print "Service successfully published"
            else: 
                print "Service could not be published because errors were found during analysis."

            print arcpy.GetMessages()

I've tried as a standalone script and from within ArcMap 10.7.1. The error:

File "C:\Project\BULK\PublishAGSBulk.py", line 29, in <module>
IOError: Operation on file C:\Users\...\Temp\{67337BAB-1DDF-4A39-9FB4-B7C8EF8597B0}\<geoprocessing Map object object at 0x036534A0>.sddraft failed. Unknown error.

I am doing this with an active connection to the ArcGIS Server (10.8) in ArcCatalog.

2
  • I have tested your code: mxd and service should be a strings not objects, con needs to be the full path. If you correct those it will work. happy to post my code if needed. – wunderkind Jul 21 '20 at 5:11
  • Thanks, @wunderkind, but after (attempting) to make mxd and service into strings, I'm still getting the I/O error. Please post your code. – Dave Jul 21 '20 at 19:37
0

I made a couple of other changes which suit my needs:

import arcpy
import xml.dom.minidom as DOM

Workspace = r'C:\\Project\\BULK\\MXDs'
arcpy.env.workspace = Workspace
#connection workspace - this is my default location
con_wrkspc = r'C:\\Users\\USERNAME\\AppData\\Roaming\\ESRI\\Desktop10.6\\ArcCatalog'
con = con_wrkspc + '\\' + 'arcgis_connection.ags'

for file in arcpy.ListFiles("*.mxd"):
    print ("Processing: "+ file)
    mxd_path = Workspace + '\\' + file
    mxd = arcpy.mapping.MapDocument(mxd_path)
    # Provide location for drafts
    service = file.strip('.mxd')
    sddraft = 'C:\\temp\\drafts' + '\\' + str(service) + '.sddraft'
    sd = 'C:\\temp\\drafts' + '\\'+ str(service) + '.sd'
    #summary option provided you have previously stored this
    summary = mxd.summary
    #summery = 'Bulk'
    tags = 'Bulk upload'

    # Create service definition draft
    #Syntax
    # CreateMapSDDraft (map_document, out_sddraft, service_name, {server_type}, {connection_file_path}, {copy_data_to_server}, {folder_name}, {summary}, {tags})
    arcpy.mapping.CreateMapSDDraft(mxd,sddraft,service,'FROM_CONNECTION_FILE',con,"FALSE","CYP",summary,tags)

    # change service parameters - I don't want Kml enabled
    doc = DOM.parse(sddraft)
    services___ = doc.getElementsByTagName('TypeName')
    for service__ in services___:
        if service__.firstChild.data == 'KmlServer':
            service__.parentNode.getElementsByTagName('Enabled')[0].firstChild.data = 'false'

    # output to a new sddraft
    New_sddraft = 'C:\\temp\\drafts' + '\\' + str(service) + '_new.sddraft'
    f = open(New_sddraft, 'w')
    doc.writexml( f )
    f.close() 

    # Analyze the service definition draft
    analysis = arcpy.mapping.AnalyzeForSD(New_sddraft)

    # Print errors, warnings, and messages returned from the analysis
    print "The following information was returned during analysis of the MXD:"
    for key in ('messages', 'warnings', 'errors'):
        print '----' + key.upper() + '---'
        vars = analysis[key]
        for ((message, code), layerlist) in vars.iteritems():
            print '    ', message, ' (CODE %i)' % code
            print '       applies to:',
            for layer in layerlist:
                print layer.name,
            print
    # Stage and upload the service if the sddraft analysis did not contain error
    if analysis['errors'] == {}:
        # Execute StageService. This creates the service definition.

        arcpy.StageService_server(New_sddraft, sd)
        # Execute UploadServiceDefinition. This uploads the service definition and publishes the service.

        arcpy.UploadServiceDefinition_server(sd, con)
        print "Service successfully published!"
    else: 
        print "Service could not be published because errors were found during analysis."
        pass
    print "arcpy messages......"
    print arcpy.GetMessages()
3
  • @Dave, the code will skip mxds that have errors and if it crashes, ensure you manually delete everything in the drafts folder. – wunderkind Jul 21 '20 at 22:54
  • @Dave, ps you also need the connection to be active – wunderkind Jul 22 '20 at 2:31
  • Thank you, @wunderkind! I was able to publish two services, but the rest of the 15 MXDs did not publish because of failed analysis results. This will help clear up other issues that I may not have come across. – Dave Jul 22 '20 at 12:36

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.