arcpy.ChangeVersion_management

1577
3
10-23-2018 02:44 AM
MartinHolleufer-Sørensen
New Contributor II

Quick question:

When I execute the lines in the python script below from the Python console within ArcGIS Pro (2.1) I am getting the expected result (the current layer is "changed" to the Version). Please refer to the screenshot ChangeVersion_management.jpg where two database connections (one of them being the SDE.DEFAULT and the other one the versioned one containing the changed layer) are shown.

BUT when I try to do the same by executing the script "as a tool" nothing seems to happen and the layer is NOT "changed" to the Version 😞

import arcpy
import os

def getcurrentuser():
username = os.environ.get("USERNAME")
username = username.upper()
return username

# Set local variables
arcpy.env.workspace = r"C:\geodat1p.sde"
parentVersion = "SDE.DEFAULT"
versionName = sys.argv[1]
fullversionName = 'OPS$'+getcurrentuser()+'.'+versionName

# Execute CreateVersion
arcpy.CreateVersion_management(arcpy.env.workspace, parentVersion, versionName, "PUBLIC")
arcpy.AddMessage("Version: {0} created".format(fullversionName))

# Changes version on JackupSpud (the layer has already been added to the Map Content-menu/pane)
arcpy.ChangeVersion_management("Jack-up Spud",'TRANSACTIONAL', fullversionName,'')

What part of the picture am I not seeing?  🙂 Do I need to create a new database connection referencing the Version.

I've tried to find a workaround by using the standard Change Version geoprocessing tool but running this on the particular layer referencing the new Version unfortunately doesn't seem to affect it either.

I was thinking that perhaps the "List by Data Sources" wasn't automaticly getting updated when executing the function from "outside" the environment but it doesn't help to switch back and forth between the tabs and when I afterwards append data into the layer it unfortunately ends up directly in the SDE.DEFAULT-connection and not in my version-connection where I would like it to go.

Kind regards,

Martin

0 Kudos
3 Replies
MartinHolleufer-Sørensen
New Contributor II

This problem has now been registered as a bug: BUG-000114017: When calling the arcpy tool Change Version from a cu.. 

JoshuaBixby
MVP Esteemed Contributor

It looks like you just typed the feature layer name in manually in the script.  Did you try passing the layer by reference, not by text, to the script tool and have the geoprocessing tool use that layer reference?

0 Kudos
MartinHolleufer-Sørensen
New Contributor II

Thanks a lot for suggesting this, Joshua. Using the following lines in my script make the things work as I want them to (data appended to my SDE-version and not directly to SDE.DEFAULT) 🙂

arcpy.management.MakeFeatureLayer("c:\test.sde\JackupSpud", "JackupSpud")
arcpy.ChangeVersion_management("JackupSpud",'TRANSACTIONAL', fullversionName,'')

arcpy.Append_management(r"%scratchGDB%\NewJackupSpuds", 'JackupSpud', "TEST")

0 Kudos