0

I am trying to write some code that will get the current workspace name and if it matches a particular datasource path, it will re-path the SDE layers to the new database. Initially I had the code working by just running the re-source code on ArcMap startup but this caused problems with the MXD's "Store relative pathnames" option in MXD and would turn it off. The problem I am having is that I am not sure how to tell it "if the workspace is "blah.sde" then only run the resource code. The MXD's connect to 3 SDE's. Here is what I have:

import arcpy
import pythonaddins
from arcpy import env

class ExtensionClass1(object):
"""Implementation for python_addin.extension2 (Extension)"""
def __init__(self):
    # For performance considerations, please remove all unused methods in 
this class.
    self.enabled = True
def startup(self):
    mxd = arcpy.mapping.MapDocument("CURRENT")
    if arcpy.env.workspace = r"C:\ArcGIS\blah.sde":
        mxd.findAndReplaceWorkspacePaths(r"C:\ArcGIS\blah.sde", 
                                     r"C:\ArcGIS_10.3.1\blah.sde")
        mxd.findAndReplaceWorkspacePaths(r"C:\ArcGIS\blah2.sde", 
                                     r"C:\ArcGIS_10.3.1\blah2.sde")
        mxd.findAndReplaceWorkspacePaths(r"C:\ArcGIS\blah3.sde", 
                                     r"C:\ArcGIS_10.3.1\blah3.sde")
        arcpy.RefreshActiveView()
        arcpy.RefreshTOC()


def openDocument(self):
    mxd = arcpy.mapping.MapDocument("CURRENT")
    if arcpy.env.workspace = r"C:\ArcGIS\blah.sde":
        mxd.findAndReplaceWorkspacePaths(r"C:\ArcGIS\blah.sde", 
                                     r"C:\ArcGIS_10.3.1\blah.sde")
        mxd.findAndReplaceWorkspacePaths(r"C:\ArcGIS\blah2.sde", 
                                     r"C:\ArcGIS_10.3.1\blah2.sde")
        mxd.findAndReplaceWorkspacePaths(r"C:\ArcGIS\blah3.sde", 
                                     r"C:\ArcGIS_10.3.1\blah3.sde")
        arcpy.RefreshActiveView()
        arcpy.RefreshTOC()
0

1 Answer 1

2

One option would be to check the layers source in the mxd and use the .find() method to check the returned string for any instance of ".sde":

for lyr in arcpy.mapping.ListLayers(mxd):
    found = lyr.dataSource.find('.sde')
    if found > -1:
        print 'sde layer found'
        # run resource code
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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