<?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 how to insert a layer to multiple mxds in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-insert-a-layer-to-multiple-mxds/m-p/692366#M53666</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'd like to insert or add a single .lyr feature into the first data frame of each of two mxds,&amp;nbsp; named "One" &amp;amp; "Two", both stored in the same folder. Each mxd has two data frames, "Layers1" &amp;amp; "Layers2". The layer feature I want to insert is called "new".lyr&amp;nbsp;&amp;nbsp; I've checked the arcpy site package for sample code and read a lot of the old python threads and can't find the code to set a variable "mxd" = multiple mxds &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I also don't know the code to save the two mxds after the new layer has been added. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; Ken&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 11 Jan 2013 00:55:09 GMT</pubDate>
    <dc:creator>KenLucas</dc:creator>
    <dc:date>2013-01-11T00:55:09Z</dc:date>
    <item>
      <title>how to insert a layer to multiple mxds</title>
      <link>https://community.esri.com/t5/python-questions/how-to-insert-a-layer-to-multiple-mxds/m-p/692366#M53666</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'd like to insert or add a single .lyr feature into the first data frame of each of two mxds,&amp;nbsp; named "One" &amp;amp; "Two", both stored in the same folder. Each mxd has two data frames, "Layers1" &amp;amp; "Layers2". The layer feature I want to insert is called "new".lyr&amp;nbsp;&amp;nbsp; I've checked the arcpy site package for sample code and read a lot of the old python threads and can't find the code to set a variable "mxd" = multiple mxds &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I also don't know the code to save the two mxds after the new layer has been added. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; Ken&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 11 Jan 2013 00:55:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-insert-a-layer-to-multiple-mxds/m-p/692366#M53666</guid>
      <dc:creator>KenLucas</dc:creator>
      <dc:date>2013-01-11T00:55:09Z</dc:date>
    </item>
    <item>
      <title>Re: how to insert a layer to multiple mxds</title>
      <link>https://community.esri.com/t5/python-questions/how-to-insert-a-layer-to-multiple-mxds/m-p/692367#M53667</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Do you have to add to both mxds at the same time. Easiest way would be iteration over mxd files and addin lyr to one document at time (as below)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Arek&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
add_layer = arcpy.mapping.Layer('C:\\Path\\to\\lyrfile.lyr')
mxds_path = 'c:\\path\\to\\mxds\\'
mxds = ['one.mxd', 'two.mxd']
for mxd_name in mxds:
&amp;nbsp; mxd = arcpy.mapping.MapDocument(mxds_path + mxd_name)
&amp;nbsp; df = arcpy.mapping.ListDataFrames(mxd, 'Layers1')[0]
&amp;nbsp; arcpy.mapping.AddLayer(df, addLayer, "TOP")
&amp;nbsp; mxd.save()
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:08:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-insert-a-layer-to-multiple-mxds/m-p/692367#M53667</guid>
      <dc:creator>ArkadiuszMatoszka</dc:creator>
      <dc:date>2021-12-12T05:08:22Z</dc:date>
    </item>
    <item>
      <title>Re: how to insert a layer to multiple mxds</title>
      <link>https://community.esri.com/t5/python-questions/how-to-insert-a-layer-to-multiple-mxds/m-p/692368#M53668</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Arek,&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; Thanks so much for trying to help me. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; I modified your code to insert the paths and change the names.&amp;nbsp; How should I correct this code to get the script to work.&amp;nbsp; After a lot of trial and error I still get an "Invalid MXD filename" error&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;add_layer = arcpy.mapping.Layer('C:\Temp2\Dams.lyr')&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;mxds_path = ('c:\Temp2')&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;mxds = ('T1.mxd', 'T2.mxd')&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;for mxd_name in mxds:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; mxd = arcpy.mapping.MapDocument(mxds_path + mxd_name)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; df = arcpy.mapping.ListDataFrames(mxd, "Layers1")[0]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; arcpy.mapping.AddLayer(df, addLayer, "TOP")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; mxd.save()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Ken&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 12 Jan 2013 00:30:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-insert-a-layer-to-multiple-mxds/m-p/692368#M53668</guid>
      <dc:creator>KenLucas</dc:creator>
      <dc:date>2013-01-12T00:30:54Z</dc:date>
    </item>
    <item>
      <title>Re: how to insert a layer to multiple mxds</title>
      <link>https://community.esri.com/t5/python-questions/how-to-insert-a-layer-to-multiple-mxds/m-p/692369#M53669</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Use double backslashes ("\\") in paths (ie. on add_layer)&amp;nbsp; instead of single ones, also mxds_path should be 'c:\\Temp2\\', not ('c:\Temp2') - no parenthesis, '\\' at end of string. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;That should do.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Arek&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 14 Jan 2013 06:47:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-insert-a-layer-to-multiple-mxds/m-p/692369#M53669</guid>
      <dc:creator>ArkadiuszMatoszka</dc:creator>
      <dc:date>2013-01-14T06:47:05Z</dc:date>
    </item>
    <item>
      <title>Re: how to insert a layer to multiple mxds</title>
      <link>https://community.esri.com/t5/python-questions/how-to-insert-a-layer-to-multiple-mxds/m-p/692370#M53670</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Arek,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; It took another full hour of trial and error, after recieving your last thread, but I fianally got your sample code to work. It turns out that the script has to be saved to, and run, from a diff folder; I've always ran script from the same folder. Even with researching the the arcgis help I would not have been able to Addlayer to multiple mxds without your kindness and patience helping me. Thanks so much.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Ken&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 15 Jan 2013 00:22:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-insert-a-layer-to-multiple-mxds/m-p/692370#M53670</guid>
      <dc:creator>KenLucas</dc:creator>
      <dc:date>2013-01-15T00:22:48Z</dc:date>
    </item>
    <item>
      <title>Re: how to insert a layer to multiple mxds</title>
      <link>https://community.esri.com/t5/python-questions/how-to-insert-a-layer-to-multiple-mxds/m-p/692371#M53671</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Good thread.&amp;nbsp; Is their an easy way to do this where you want to do it to all MXDs in a folder?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Jan 2014 21:29:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-insert-a-layer-to-multiple-mxds/m-p/692371#M53671</guid>
      <dc:creator>TomMagdaleno</dc:creator>
      <dc:date>2014-01-27T21:29:35Z</dc:date>
    </item>
    <item>
      <title>Re: how to insert a layer to multiple mxds</title>
      <link>https://community.esri.com/t5/python-questions/how-to-insert-a-layer-to-multiple-mxds/m-p/692372#M53672</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Good thread.&amp;nbsp; Is their an easy way to do this where you want to do it to all MXDs in a folder?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Sure, you could just modify the existing code with the listfiles() function and leverage the wildcard argument to identify MXDs in a given folder.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# Import the arcpy module
import arcpy
# Identify the path to the Layer File you want to insert on disk.
InsertLayer = arcpy.mapping.layer(r"C:\MyGIS\Layers\TheLayerFileToInsert.lyr")
# Identify the Folder containing the MXDs you want to insert the 'InsertLayer' into
MXDsFolder = r"C:\MyGIS\MXDsFolder"
# Set the Environment Workspace to the MXDs Folder so that the 'ListFiles()'
# function will search this folder for MXDs when called
arcpy.env.workspace = MXDsFolder
# Call the 'arcpy.ListFiles()' function and invoke the wildcard parameter to only 
# return MXDs from the folder. Populate the results in the variable 'MXDsList'
MXDsList = arcpy.ListFiles("*.mxd")
# Loop through each MXD in the 'MXDsList'
for MXD in MXDsList:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Establish the current MXD as the 'TargetMXD' to insert the Layer into
&amp;nbsp;&amp;nbsp;&amp;nbsp; TargetMXD = arcpy.mapping.MapDocument(MXDsFolder + "\\" + str(MXD))
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Get a reference to the first dataframe in the Target MXD
&amp;nbsp;&amp;nbsp;&amp;nbsp; TargetDF = arcpy.mapping.ListDataFrames(TargetMXD)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Add the Layer to the TargetMXD
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddLayer(TargetDF, InsertLayer, "TOP")
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Save the MXD
&amp;nbsp;&amp;nbsp;&amp;nbsp; TargetMXD.save()
print "Complete"
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:08:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-insert-a-layer-to-multiple-mxds/m-p/692372#M53672</guid>
      <dc:creator>JohnDye</dc:creator>
      <dc:date>2021-12-12T05:08:25Z</dc:date>
    </item>
    <item>
      <title>Re: how to insert a layer to multiple mxds</title>
      <link>https://community.esri.com/t5/python-questions/how-to-insert-a-layer-to-multiple-mxds/m-p/692373#M53673</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Awesome!&amp;nbsp; Thank you John Dye!&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Is their a way to batch "Repair data sources" and point everything to one geodatabase and let it find the right feature?&amp;nbsp; Somehow the path to my geodatabase got jumbled.&amp;nbsp; I tried to do it here&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy, os
folderPath = r"C:\\01Working_Directory\\LandscapeMaps"
# Set the Environment Workspace to the MXDs Folder so that the 'ListFiles()'
# function will search this folder for MXDs when called
arcpy.env.workspace = folderPath
# Call the 'arcpy.ListFiles()' function and invoke the wildcard parameter to only 
# return MXDs from the folder. Populate the results in the variable 'MXDsList'
MXDsList = arcpy.ListFiles("*.mxd")
# Loop through each MXD in the 'MXDsList'
for MXD in MXDsList:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Establish the current MXD as the 'TargetMXD' to insert the Layer into
&amp;nbsp;&amp;nbsp;&amp;nbsp; TargetMXD = arcpy.mapping.MapDocument(folderPath + "\\" + str(MXD))
for filename in os.listdir(folderPath):
 fullpath = os.path.join(folderPath, filename)
 if os.path.isfile(fullpath):
&amp;nbsp; basename, extension = os.path.splitext(fullpath)
 if extension.lower() == ".mxd":
&amp;nbsp; mxd = arcpy.mapping.MapDocument(fullpath)
mxd.findAndReplaceWorkspacePaths(r"c:\01Working_Directory\LandscapeMaps\LandscapeDistrict.gdb", r"G:\CamarilloGIS\Projects\PublicWorks\STREETS\Landscape Districts\LandscapeDistricts.gdb")
TargetMXD.save()
print "All Repathed!"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I get this error &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "C:/01Working_Directory/Python/RepathToolLoop.py", line 20, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(fullpath)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\arcobjects\mixins.py", line 609, in __init__&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;AssertionError: Invalid MXD filename.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Any ideas?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:08:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-insert-a-layer-to-multiple-mxds/m-p/692373#M53673</guid>
      <dc:creator>TomMagdaleno</dc:creator>
      <dc:date>2021-12-12T05:08:28Z</dc:date>
    </item>
    <item>
      <title>Re: how to insert a layer to multiple mxds</title>
      <link>https://community.esri.com/t5/python-questions/how-to-insert-a-layer-to-multiple-mxds/m-p/692374#M53674</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The first problem I see here is that you are using double backslashes inside of a raw string literal. A raw string literal is the exact string written inside of quotes when prefaced with an 'r'.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When you specify a path with as a raw string literal, you don't need the double-backslashes because python is going to use the exact string specified within the quotes. Just use a single backslash, the same as you would in Windows to navigate to a specific path. For me, this is why I always specify a path as a raw string literal. I can use the same formatting to define a path as Windows.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you don't use the raw string literal prefix (r), then you do need to use double-backslashes because a single backslash is an escape in python. If you use a single backslash without the raw string literal prefix,&amp;nbsp; you will likely end up escaping some of the characters in your path.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As a result of having those double-backslashes inside of a raw string literal, when you perform the 'os.path.join()' function, the return contains an excessive number of backslashes.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Take this example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;gt;&amp;gt;&amp;gt; folderPath = r"C:\\01Working_Directory\\LandscapeMaps"
&amp;gt;&amp;gt;&amp;gt; file = "MyGISFile"
&amp;gt;&amp;gt;&amp;gt; os.path.join(folderPath, file)
'C:\\\\01Working_Directory\\\\LandscapeMaps\\MyGISFile'
&amp;gt;&amp;gt;&amp;gt;
&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;That's a lot of backslashes. The last part where the file was joined to the folderPath is ok because the 'os.path.join()' inserted the appropriate seperator when executing the join. Replacing the double-backslashes in your 'folderPath' variable with single backslashes should resolve the issue.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In regards to fixing your broken data sources, it is completely possible. You already know how to loop through a list of MXDs, so now it's just a matter of invoking the appropriate functions to &lt;/SPAN&gt;&lt;SPAN style="font-style: italic; text-decoration: underline;"&gt;&lt;STRONG&gt;list the broken data sources&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN&gt;, loop through that list and &lt;/SPAN&gt;&lt;SPAN style="font-style: italic; text-decoration: underline;"&gt;&lt;STRONG&gt;replace their data sources&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN&gt;. Take a look at &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//00s30000004p000000" rel="nofollow noopener noreferrer" target="_blank"&gt;this help article&lt;/A&gt;&lt;SPAN&gt;. It should point you in the right direction.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:08:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-insert-a-layer-to-multiple-mxds/m-p/692374#M53674</guid>
      <dc:creator>JohnDye</dc:creator>
      <dc:date>2021-12-12T05:08:30Z</dc:date>
    </item>
    <item>
      <title>Re: how to insert a layer to multiple mxds</title>
      <link>https://community.esri.com/t5/python-questions/how-to-insert-a-layer-to-multiple-mxds/m-p/1388500#M69994</link>
      <description>&lt;P&gt;Hi! Is there a way to insert the feature layers from a gdb with a specific formatting, using as a base the code that&amp;nbsp;&lt;A href="https://community.esri.com/t5/user/viewprofilepage/user-id/7865" target="_self"&gt;TomMagdaleno&lt;/A&gt;&amp;nbsp;used? I'm in ArcMap 10.8. Maybe there is something I can fix &lt;STRONG&gt;in&lt;/STRONG&gt; the feature layers in the gdb so they always are added with the same specific format. Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Thu, 29 Feb 2024 02:12:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-insert-a-layer-to-multiple-mxds/m-p/1388500#M69994</guid>
      <dc:creator>CarolinaDByl</dc:creator>
      <dc:date>2024-02-29T02:12:04Z</dc:date>
    </item>
  </channel>
</rss>

