Wanting to add layer to Table of Contents via Python

1058
2
03-29-2011 05:42 AM
StevenMenefee
New Contributor
I am trying to do something that I thought would be simple!  We use a number of shapefiles frequently and I'd like to create buttons within ArcMap that you click that will add a particular shapefile to the Table of Contents.

I decided I would write the code in Python, add that as a Script in Arctoolbox and somehow make a button that will look at that Script.  Anyway, the code I'm using SEEMS to be working.  However, I can't seem to see the layer within the Table of Contents.  Below is the code I'm using that I am adding a Script to (no Parameters).

import arcpy
mxd = arcpy.mapping.MapDocument("C:\\Gis\\Python\\mxd\\test2.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
addLayer = arcpy.mapping.Layer("C:\\Gis\\GisMaps\\Attica\\layer.lyr")
arcpy.mapping.AddLayer(df, addLayer)
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
del mxd, addLayer

Any help would be much appreciated, thanks!
Tags (2)
0 Kudos
2 Replies
StevenMenefee
New Contributor
That worked great, thank you!!
0 Kudos
ArjunDongre
Occasional Contributor

Adding RefreshActiveView and RefreshTOC get the the layer to stay in the mxd before saving. Weird this is not in the documentation for AddLayer.

0 Kudos