I am making a feature layer and applying an expression to essentially apply a definition query to the source feature class. I then want to add the created feature layer to the map. I am aware of addDataFromPath but I don't want to add the entire feature class. I want to add the layer with a definition query applied. I could save the new layer to file and add it that way but it seems redundant to have the feature class as well as its subset stored on file. Does anyone know how I can add this type of layer to the map in ArcGIS Pro?

arcpy.MakeFeatureLayer_management ("pts","pts_layer", 'Solution = 1')

You can use the Map.addLayer method. You'll need to get a reference to the layer from the Result object that MakeFeatureLayer returns.

#  MakeFeatureLayer returns a Result object. 
#  Use the Result.getOutput method to get a reference to the Layer object
pts_layer = arcpy.MakeFeatureLayer_management ("pts","pts_layer", 'Solution = 1').getOutput(0) 
  • Is this documented anywhere? I couldn't locate it in their MakeFeatureLayer help. – ketar Feb 27 '19 at 19:08
  • @ketar It's documented in the Tool Output section of the Using tools in Python page. But yes I think it should be referred to in the help pages of each tool. – user2856 Feb 27 '19 at 20:34
  • 1
    @ketar, if you are writing a toolbox script or python toolbox tool, you can also just define an output parameter, then set the layer to the output parameter and it will get added to the map automatically when the tool completes. – user2856 Feb 28 '19 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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