<?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 AddLayer -- dynamically interact with open mxd document? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/addlayer-dynamically-interact-with-open-mxd/m-p/205333#M15806</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have been unsuccessfully attempting to add layers programmatically to an open mxd document via arcpy scripting. I AM, however, able to add the layers to the mxd, save a copy of the mxd and then open the new mxd with the layers successfully added. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Is it not possible to add these layers and refresh the TOC and Active View dynamically with the mxd open? Or does it have to be closed, saved and reopened?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I don't receive any errors when I run my code it is that the layers aren't visibly added to the mxd on the fly. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is a code excerpt:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(fc, fl, whereClause) &amp;nbsp;&amp;nbsp;&amp;nbsp; outLayerFilePath = outputFL + "\\" + fl + ".lyr" &amp;nbsp;&amp;nbsp;&amp;nbsp; # Save to Layer file &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SaveToLayerFile_management(fl, outLayerFilePath) &amp;nbsp;&amp;nbsp;&amp;nbsp; out_layer = arcpy.mapping.Layer(outLayerFilePath) &amp;nbsp;&amp;nbsp;&amp;nbsp; # Line checks to ensure the feature layers were created with the right feature count &amp;nbsp;&amp;nbsp;&amp;nbsp; #countFeatures = arcpy.GetCount_management(out_layer).getOutput(0) &amp;nbsp;&amp;nbsp;&amp;nbsp; #print countFeatures &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddLayer(df, out_layer)&amp;nbsp; arcpy.RefreshTOC() arcpy.RefreshActiveView() mxd.saveACopy(outputMXDpath) # only when this line is added to save to a new mxd can I see the newly added layers.&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 10 Mar 2014 22:12:37 GMT</pubDate>
    <dc:creator>YurikoHashimoto</dc:creator>
    <dc:date>2014-03-10T22:12:37Z</dc:date>
    <item>
      <title>AddLayer -- dynamically interact with open mxd document?</title>
      <link>https://community.esri.com/t5/python-questions/addlayer-dynamically-interact-with-open-mxd/m-p/205333#M15806</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have been unsuccessfully attempting to add layers programmatically to an open mxd document via arcpy scripting. I AM, however, able to add the layers to the mxd, save a copy of the mxd and then open the new mxd with the layers successfully added. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Is it not possible to add these layers and refresh the TOC and Active View dynamically with the mxd open? Or does it have to be closed, saved and reopened?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I don't receive any errors when I run my code it is that the layers aren't visibly added to the mxd on the fly. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is a code excerpt:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(fc, fl, whereClause) &amp;nbsp;&amp;nbsp;&amp;nbsp; outLayerFilePath = outputFL + "\\" + fl + ".lyr" &amp;nbsp;&amp;nbsp;&amp;nbsp; # Save to Layer file &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SaveToLayerFile_management(fl, outLayerFilePath) &amp;nbsp;&amp;nbsp;&amp;nbsp; out_layer = arcpy.mapping.Layer(outLayerFilePath) &amp;nbsp;&amp;nbsp;&amp;nbsp; # Line checks to ensure the feature layers were created with the right feature count &amp;nbsp;&amp;nbsp;&amp;nbsp; #countFeatures = arcpy.GetCount_management(out_layer).getOutput(0) &amp;nbsp;&amp;nbsp;&amp;nbsp; #print countFeatures &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddLayer(df, out_layer)&amp;nbsp; arcpy.RefreshTOC() arcpy.RefreshActiveView() mxd.saveACopy(outputMXDpath) # only when this line is added to save to a new mxd can I see the newly added layers.&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Mar 2014 22:12:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/addlayer-dynamically-interact-with-open-mxd/m-p/205333#M15806</guid>
      <dc:creator>YurikoHashimoto</dc:creator>
      <dc:date>2014-03-10T22:12:37Z</dc:date>
    </item>
    <item>
      <title>Re: AddLayer -- dynamically interact with open mxd document?</title>
      <link>https://community.esri.com/t5/python-questions/addlayer-dynamically-interact-with-open-mxd/m-p/205334#M15807</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;There are two things you must do:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Open the current document in ArcMap using the magic constant 'CURRENT' instead of opening the mxd file.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;mxd = arcpy.mapping.MapDocument("CURRENT")&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You also need to run the script in foreground which is a tool property.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It is possible to save the CURRENT mxd to a file after modifying it.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Mar 2014 08:27:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/addlayer-dynamically-interact-with-open-mxd/m-p/205334#M15807</guid>
      <dc:creator>KimOllivier</dc:creator>
      <dc:date>2014-03-11T08:27:05Z</dc:date>
    </item>
    <item>
      <title>Re: AddLayer -- dynamically interact with open mxd document?</title>
      <link>https://community.esri.com/t5/python-questions/addlayer-dynamically-interact-with-open-mxd/m-p/205335#M15808</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks so much, Kim! Works like a charm! I wish I had only asked sooner! &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks again!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Mar 2014 17:21:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/addlayer-dynamically-interact-with-open-mxd/m-p/205335#M15808</guid>
      <dc:creator>YurikoHashimoto</dc:creator>
      <dc:date>2014-03-11T17:21:42Z</dc:date>
    </item>
    <item>
      <title>Re: AddLayer -- dynamically interact with open mxd document?</title>
      <link>https://community.esri.com/t5/python-questions/addlayer-dynamically-interact-with-open-mxd/m-p/205336#M15809</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If looks like you're trying to add layer layer to your current mapping session, but you don't have a reference to your "df" (dataframe) in the arcpy.mapping.AddLayer&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's the way I usually Add a layer I've been geoprocessing to my current table of contents:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Get your current MXD and DF (assuming you only have one df)
mxd = arcpy.mapping.MapDocument("CURRENT") 
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]

# Get a Layer refernce to the layer you want to add
fc_mapping_reference = arcpy.mapping.Layer(fc)

# Add your layer to your current df
arcpy.mapping.AddLayer(df,fc_mapping_reference,"BOTTOM")

# If you're having problems refreshing your screen
arcpy.RefreshActiveView()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:10:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/addlayer-dynamically-interact-with-open-mxd/m-p/205336#M15809</guid>
      <dc:creator>MattEiben</dc:creator>
      <dc:date>2021-12-11T10:10:02Z</dc:date>
    </item>
    <item>
      <title>Re: AddLayer -- dynamically interact with open mxd document?</title>
      <link>https://community.esri.com/t5/python-questions/addlayer-dynamically-interact-with-open-mxd/m-p/205337#M15810</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Matt, indeed I wasn't actually referencing the mxd with "CURRENT" and was using a stand-alone script that referenced the open mxd which is a no-go. &lt;span class="lia-unicode-emoji" title=":face_with_tongue:"&gt;😛&lt;/span&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Mar 2014 17:26:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/addlayer-dynamically-interact-with-open-mxd/m-p/205337#M15810</guid>
      <dc:creator>YurikoHashimoto</dc:creator>
      <dc:date>2014-03-11T17:26:43Z</dc:date>
    </item>
  </channel>
</rss>

