Repair symbology with categories

Discussion created by vbmart on Aug 5, 2013
We are migrating from ArcMap 9.3.1 to 10.1.  In the process, we have to repair some issues with broken SDE data sources for our existing map documents.  We can do most of that using Python, we are able to recover/replace, the data source, simple symbology, definition query, and joins for our map documents (or layers therein). 

However, we  have not found a way manually or using Python (or ArcObjects) to preserve any of the more complex layer symbology, such a symbology that uses categories (e.g. draw categories using unique values of one field).  Has anyone been able to do this? 

We have thousands of map documents, so clearly we would prefer to be able to automate this task, as otherwise it will be thousands of hours of work. 

Below is what we have working so far (except the part about suppressing the popup, but that is a minor issue for now):

Ideas? Links to the solution?

import arcpy as ap
import arcpy.mapping as am
import os.path
import os 
wd = r"C:\WORKSPACE"
source_mxd = r"%s\%s" % (wd, "source_mxd")
target_mxd = r"%s\%s" % (wd, "target_mxd")

new_Source_connect_file = r"C:\workspace\TempConnection.sde"

# get the current location
cwd = os.getcwd()

if cwd.upper() <> wd.upper():
# cleanout the target directory    
for  fl in os.listdir(target_mxd):
    if fl.endswith(".mxd"):
        mxd = r"%s\%s" % (target_mxd, fl)
        print mxd 
        if os.path.exists(mxd):
# just to supress the loggon popup
ConxFile = 'TempConnection.sde'
Database = ''
ServiceName = 'SERVICENAME'
authType = 'DATABASE_AUTH'
Pwd = 'myPassword'
SaveUserInfo = 'SAVE_USERNAME'
saveVersInfo = 'SAVE_VERSION'

print 'wd: ' + wd + '     newSourceConnectFile: ' + new_Source_connect_file

sdeFile = r"%s\%s" % ( wd, r"TempConnection.sde")
if os.path.exists(sdeFile):
ap.CreateArcSDEConnectionFile_management(wd, ConxFile,\
                                            Server, ServiceName, Database, authType,\
                                            UserName, Pwd, SaveUserInfo, versName,\
# Process the souce map documents
mdw = None
md  = None
for  fl in os.listdir(source_mxd):
    if fl.endswith(".mxd"):
        mxd = r"%s\%s" % (source_mxd, fl)
        print "Processing %s" % (mxd)
        # make a working copy in the target directory
        mdw = am.MapDocument(mxd)
        temp_mdw = r"%s\%s" % (target_mxd, "Temp.mxd")
        print "Created Temp"
        del mdw
        #working copy is now the target copy
        md = am.MapDocument(temp_mdw)
        LstBrokenDataSources = am.ListBrokenDataSources(md)
        for lyr in LstBrokenDataSources:
            print "Processing " + lyr.longName + " for " + mxd 
            if lyr.supports("DATASOURCE"):
                layer = lyr.dataSource
                if lyr.supports("WORKSPACEPATH"):
                    print lyr.workspacePath
                if layer.upper().find(r"TRMS.SDE") >= 0:
                    print "repairing Data Source" 
                    lyr.replaceDataSource(new_Source_connect_file, "SDE_WORKSPACE", "", False)
        del md
        if os.path.exists(temp_mdw):

Thanks for any help you may have!