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()
Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked May 2, 2018 at 11:23
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
answered May 2, 2018 at 12:29
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.