<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic mxd layer data source replacement for SDE  in MSSQL server in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/mxd-layer-data-source-replacement-for-sde-in-mssql/m-p/637980#M49715</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Everybody! Please I really need help with this python script. The objective of the script is to list all the mxd files in the specified folder, identify all the layers within the mxd that were sourced from my old SDE database server, and then change the layer source to my new SDE database server.&lt;/P&gt;&lt;P&gt;In other words, the main purpose of the script is to do a bulk data source change for all the layers in my mxd files&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So far the script is able to list all the mxds and the paths of the layer sources but it is not changing the data sources for layers in my SDE as needed&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;import arcpy, os, getpass&lt;BR /&gt;newServer = "TNR-GISSQL19-P"&lt;BR /&gt;user = getpass.getuser()&lt;BR /&gt;#user_dir = os.path.join(u'C:\\Users\\', user)&lt;BR /&gt;user_dir = u'Y:\\VIEWERS\\T' ### this specifies the directory containing the mxd files&lt;BR /&gt;os.chdir(user_dir)&lt;BR /&gt;############create logfile############&lt;BR /&gt;logfile = open("logfile_for_Shade_upgrade.txt", "w") ##&amp;nbsp; creates&amp;nbsp;a logfile&lt;/P&gt;&lt;P&gt;print&amp;gt;&amp;gt;logfile, "User's home directory: %r" % user_dir&lt;/P&gt;&lt;P&gt;############create mxdList############&lt;BR /&gt;mxdList = []&lt;/P&gt;&lt;P&gt;for dirpath,_,filenames in os.walk(user_dir):&lt;BR /&gt; for name in filenames:&lt;BR /&gt; if name.endswith('.mxd'):&lt;BR /&gt; mapdoc= os.path.join(dirpath, name)&lt;BR /&gt; mxdList.append(mapdoc)&lt;BR /&gt;print&amp;gt;&amp;gt;logfile, "------MXD List:------"&lt;/P&gt;&lt;P&gt;############print MXDs to logfile############&lt;BR /&gt;for mapdoc in mxdList:&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, mapdoc&lt;/P&gt;&lt;P&gt;for file in mxdList:&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "---Searching for SDE connections in: %r" % file&lt;BR /&gt; filePath = os.path.join(user_dir, file)&lt;BR /&gt; mxd = arcpy.mapping.MapDocument(filePath)&lt;BR /&gt; for lyr in arcpy.mapping.ListLayers(mxd):&lt;BR /&gt; try:&lt;BR /&gt; if lyr.supports("dataSource"):&lt;BR /&gt; try:&lt;BR /&gt;## if lyr.supports("SERVICEPROPERTIES") and lyr.serviceProperties['ServiceType'] == u'SDE' and 'Server' in lyr.serviceProperties and lyr.serviceProperties['Server'] == u'Frogmouth':&lt;BR /&gt; if lyr.supports("SERVICEPROPERTIES") and lyr.serviceProperties['ServiceType'] == u'SDE' and lyr.serviceProperties['Server'] == u'frogmouth':&lt;BR /&gt; ## print&amp;gt;&amp;gt;logfile, lyr.serviceProperties&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "layer uses Frogmouth"&lt;BR /&gt; try:&lt;BR /&gt; serverName = lyr.serviceProperties[u'Server']&lt;BR /&gt; databaseName = lyr.serviceProperties[u'Database']&lt;BR /&gt; if not databaseName.endswith('.sde'):&lt;BR /&gt; databaseName += '.sde'&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "Updating: %r, %r:" %(lyr.serviceProperties['Server'], lyr.serviceProperties['Database'])&lt;BR /&gt; layerName = lyr.datasetName&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "Layer name: %r" % layerName&lt;BR /&gt; if lyr.supports("workspacePath"):&lt;BR /&gt; find = lyr.workspacePath ## herein lies the problem, some lyr.workspacePath do not exist&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "Current path: %r" % find&lt;BR /&gt; replacePath = os.path.join(user_dir+"\\AppData\\Roaming\\ESRI\\Desktop10.7\\ArcCatalog\\",os.path.os.path.basename(newServer)+"_"+databaseName)&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "New Path: %r" % replacePath&lt;BR /&gt; mxd.replaceWorkspaces(find,"SDE_WORKSPACE",replacePath,"SDE_WORKSPACE",False)&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "****************%r has been updated successfully" % lyr.datasetName&lt;BR /&gt; else:&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "!!!!!!Connection path not valid! Layer not updated"&lt;BR /&gt; except:&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "Unknown error occurred (level 0)."&lt;BR /&gt; else:&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "Layer does not use a Vultur SDE connection: %r, %r" %(lyr.datasetName, lyr.dataSource)&lt;BR /&gt; except:&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "Unknown error occurred (level 1)."&lt;BR /&gt; else:&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "Layer is basemap and does not need to be updated."&lt;BR /&gt; except:&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "Unknown error occurred (level 2)."&lt;/P&gt;&lt;P&gt;#mxd.save() ## test first with this commented out, because otherwise you will have to revert to Frogmouth if you want to re-test&lt;/P&gt;&lt;P&gt;logfile.close()&lt;BR /&gt;print "Completed."&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 26 Aug 2020 13:24:28 GMT</pubDate>
    <dc:creator>IreneEgbulefu1</dc:creator>
    <dc:date>2020-08-26T13:24:28Z</dc:date>
    <item>
      <title>mxd layer data source replacement for SDE  in MSSQL server</title>
      <link>https://community.esri.com/t5/python-questions/mxd-layer-data-source-replacement-for-sde-in-mssql/m-p/637980#M49715</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Everybody! Please I really need help with this python script. The objective of the script is to list all the mxd files in the specified folder, identify all the layers within the mxd that were sourced from my old SDE database server, and then change the layer source to my new SDE database server.&lt;/P&gt;&lt;P&gt;In other words, the main purpose of the script is to do a bulk data source change for all the layers in my mxd files&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So far the script is able to list all the mxds and the paths of the layer sources but it is not changing the data sources for layers in my SDE as needed&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;import arcpy, os, getpass&lt;BR /&gt;newServer = "TNR-GISSQL19-P"&lt;BR /&gt;user = getpass.getuser()&lt;BR /&gt;#user_dir = os.path.join(u'C:\\Users\\', user)&lt;BR /&gt;user_dir = u'Y:\\VIEWERS\\T' ### this specifies the directory containing the mxd files&lt;BR /&gt;os.chdir(user_dir)&lt;BR /&gt;############create logfile############&lt;BR /&gt;logfile = open("logfile_for_Shade_upgrade.txt", "w") ##&amp;nbsp; creates&amp;nbsp;a logfile&lt;/P&gt;&lt;P&gt;print&amp;gt;&amp;gt;logfile, "User's home directory: %r" % user_dir&lt;/P&gt;&lt;P&gt;############create mxdList############&lt;BR /&gt;mxdList = []&lt;/P&gt;&lt;P&gt;for dirpath,_,filenames in os.walk(user_dir):&lt;BR /&gt; for name in filenames:&lt;BR /&gt; if name.endswith('.mxd'):&lt;BR /&gt; mapdoc= os.path.join(dirpath, name)&lt;BR /&gt; mxdList.append(mapdoc)&lt;BR /&gt;print&amp;gt;&amp;gt;logfile, "------MXD List:------"&lt;/P&gt;&lt;P&gt;############print MXDs to logfile############&lt;BR /&gt;for mapdoc in mxdList:&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, mapdoc&lt;/P&gt;&lt;P&gt;for file in mxdList:&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "---Searching for SDE connections in: %r" % file&lt;BR /&gt; filePath = os.path.join(user_dir, file)&lt;BR /&gt; mxd = arcpy.mapping.MapDocument(filePath)&lt;BR /&gt; for lyr in arcpy.mapping.ListLayers(mxd):&lt;BR /&gt; try:&lt;BR /&gt; if lyr.supports("dataSource"):&lt;BR /&gt; try:&lt;BR /&gt;## if lyr.supports("SERVICEPROPERTIES") and lyr.serviceProperties['ServiceType'] == u'SDE' and 'Server' in lyr.serviceProperties and lyr.serviceProperties['Server'] == u'Frogmouth':&lt;BR /&gt; if lyr.supports("SERVICEPROPERTIES") and lyr.serviceProperties['ServiceType'] == u'SDE' and lyr.serviceProperties['Server'] == u'frogmouth':&lt;BR /&gt; ## print&amp;gt;&amp;gt;logfile, lyr.serviceProperties&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "layer uses Frogmouth"&lt;BR /&gt; try:&lt;BR /&gt; serverName = lyr.serviceProperties[u'Server']&lt;BR /&gt; databaseName = lyr.serviceProperties[u'Database']&lt;BR /&gt; if not databaseName.endswith('.sde'):&lt;BR /&gt; databaseName += '.sde'&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "Updating: %r, %r:" %(lyr.serviceProperties['Server'], lyr.serviceProperties['Database'])&lt;BR /&gt; layerName = lyr.datasetName&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "Layer name: %r" % layerName&lt;BR /&gt; if lyr.supports("workspacePath"):&lt;BR /&gt; find = lyr.workspacePath ## herein lies the problem, some lyr.workspacePath do not exist&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "Current path: %r" % find&lt;BR /&gt; replacePath = os.path.join(user_dir+"\\AppData\\Roaming\\ESRI\\Desktop10.7\\ArcCatalog\\",os.path.os.path.basename(newServer)+"_"+databaseName)&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "New Path: %r" % replacePath&lt;BR /&gt; mxd.replaceWorkspaces(find,"SDE_WORKSPACE",replacePath,"SDE_WORKSPACE",False)&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "****************%r has been updated successfully" % lyr.datasetName&lt;BR /&gt; else:&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "!!!!!!Connection path not valid! Layer not updated"&lt;BR /&gt; except:&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "Unknown error occurred (level 0)."&lt;BR /&gt; else:&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "Layer does not use a Vultur SDE connection: %r, %r" %(lyr.datasetName, lyr.dataSource)&lt;BR /&gt; except:&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "Unknown error occurred (level 1)."&lt;BR /&gt; else:&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "Layer is basemap and does not need to be updated."&lt;BR /&gt; except:&lt;BR /&gt; print&amp;gt;&amp;gt;logfile, "Unknown error occurred (level 2)."&lt;/P&gt;&lt;P&gt;#mxd.save() ## test first with this commented out, because otherwise you will have to revert to Frogmouth if you want to re-test&lt;/P&gt;&lt;P&gt;logfile.close()&lt;BR /&gt;print "Completed."&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Aug 2020 13:24:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/mxd-layer-data-source-replacement-for-sde-in-mssql/m-p/637980#M49715</guid>
      <dc:creator>IreneEgbulefu1</dc:creator>
      <dc:date>2020-08-26T13:24:28Z</dc:date>
    </item>
  </channel>
</rss>

