With ArcPy I have created a tool that creates an empty feature class with pre-defined fields and adds it to the TOC. It works quite well, however if I use the tool to generate a feature class in an unsaved MXD it hangs. I can get around it by naming and saving the MXD (it'S a bit cumbersome).
I'd like to change the code so that I can add a feature class to the TOC without necessarily creating an MXD project each time.

# currently only works on a saved mxd.  needs to be able to add to an unsaved docuement.
import arcpy
#import time feature - to insert date into field, but not functional yet.
from time import strftime
from arcpy import env
env.workspace = "CURRENT"

#--------------DEFINE VARIABLES---------------
in_wrkspace = arcpy.GetParameterAsText(0)
projectNumber = arcpy.GetParameterAsText(1)
FeatureDescription = arcpy.GetParameterAsText(2)
in_GeomType = arcpy.GetParameterAsText(3)
spatialRefCode = arcpy.GetParameterAsText(4)

#-----------------DEFINE EXPRESSIONS-----------------
FeatureClass = '_'+ str(projectNumber) + '_cm_' + str(FeatureDescription) + '_' + strftime("%y%m%d")

#Create feature class within workspace and add fields
FC = arcpy.CreateFeatureclass_management(in_wrkspace,FeatureClass,in_GeomType,"","DISABLED","DISABLED",spatialRefCode)
for FeatureClass in FC:

    # Process: classe
    arcpy.AddField_management(FeatureClass, "classe", "TEXT", "", "", "50", "", "NULLABLE", "NON_REQUIRED", "")

    # Process: objet
    arcpy.AddField_management(FeatureClass, "objet", "TEXT", "", "", "200", "", "NULLABLE", "NON_REQUIRED", "")
    #Process: source
    arcpy.AddField_management(FeatureClass, "source", "TEXT", "", "", "80", "", "NULLABLE", "NON_REQUIRED", "")
    #Process: date
    arcpy.AddField_management(FeatureClass, "date", "TEXT", "", "", "10", "", "NULLABLE", "NON_REQUIRED", "")
    #Process: remarque
    arcpy.AddField_management(FeatureClass, "remarque", "TEXT", "", "", "250", "", "NULLABLE", "NON_REQUIRED", "")
    #Process: traitment
    arcpy.AddField_management(FeatureClass, "traitment", "TEXT", "", "", "250", "", "NULLABLE", "NON_REQUIRED", "")
    #Process: no_seq
    arcpy.AddField_management(FeatureClass,"NO_SEQ", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
    #make feature layer

#Create a layer file to add to MXD
temp = '_'+ str(projectNumber) + '_cm_' + str(FeatureDescription) + '_' + strftime("%y%m%d")

#-----------------add the layer to data frame--------------------------
#source: http://gis.stackexchange.com/questions/62896/how-to-add-feature-class-to-mxd-with-arcpy-python
MXD = arcpy.mapping.MapDocument("CURRENT")
DF = arcpy.mapping.ListDataFrames(MXD)[0]
Layer = arcpy.mapping.Layer(temp)
#delete in memory cache layer

del MXD #, DF, Layer

Many thanks!

  • Thank you Hornbydd and GISGe. Both of your answers worked perfectly well. I opted for Hornbydd's solution although either one would have been excellent.
    – Frederic
    May 22, 2014 at 16:33

2 Answers 2


I just ran your code, works fine for me. I did make the following changes:

  1. After the line where you set the workspace I added the line:

    env.addOutputsToMap = False

  2. I delete the MakeFeatureLayer line

  3. Edited what temp was set to so changed line to

    temp = FeatureClass

  • Worked like a charm.
    – Frederic
    May 22, 2014 at 16:30

What if you just remove MXD.save() and add arcpy.RefreshTOC() to make sure the added layer is visible?


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.