Select to view content in your preferred language

arcpy.AddJoin Not Working In ArcGIS Pro Script

4086
10
10-25-2019 06:53 AM
KennethGibson
Emerging Contributor

I am trying to join a layer in an ArcGIS Pro map to a table using a scripting tool. The layer is called 'Heights', and is being joined to a table called 'Names'. The Python code below is working when run from the Python window within ArcGIS Pro. However, when I try to run it as a script from the Catalog Pane then the join does not happen, even though the geoprocessing detail does not throw up any error messages. Am I missing a step or does this just not work in ArcGIS Pro?

import arcpy
#Reference map document from within ArcGIS Pro
aprx = arcpy.mp.ArcGISProject('CURRENT')
map = aprx.activeMap
# Local variables:
Heights = "Heights"
toTable = r"C:\#workspace\Names"
# Process: Add Join
arcpy.AddJoin_management(Heights, 'LINK_ID', toTable, 'AI_LINK_ID', 'KEEP_ALL')‍‍‍‍‍‍‍‍‍‍‍
10 Replies
jdyerLACSD
Emerging Contributor

I was struggling with a similar problem and came across this thread.  According to the documentation:

To see the results of a join created in a script tool, the tool must include the layer as a derived output parameter. Similarly, the Updated Input Layer or Table View parameter must be set as a derived output parameter in a model tool to see the joined results.

So in your case, you'd have to add a derived output parameter to your script, save the output of the Add Join tool to a variable, and set that parameter as that variable, like this:

 

import arcpy
#Reference map document from within ArcGIS Pro
aprx = arcpy.mp.ArcGISProject('CURRENT')
map = aprx.activeMap
# Local variables:
Heights = "Heights"
toTable = r"C:\#workspace\Names"
# Process: Add Join
heights_joined = arcpy.AddJoin_management(Heights, 'LINK_ID', toTable, 'AI_LINK_ID', 'KEEP_ALL')‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍  # save result to heights_joined
arcpy.SetParameter(0, height_joined)  # "0" is parameter index

 


 In my case, I wanted to add a definition query and apply symbology to the joined layer, which I couldn't do in that script, since the joined layer isn't available until the script ends (since it's an output).  So I just had to add those steps to a second script, and then I used model builder to run the first script and use the derived output as an input for the second script, which set the definition query and applied the symbology.