*NOTE* Working code at the bottom.
I am looking to see if there is a way to grab the current version that is connected to within an MXD. I have a tool that requires me to add the following code:
edit = arcpy.da.Editor(workspace)
edit.startEditing(False, True)
edit.startOperation()
*OPERATION HERE*
edit.stopOperation()
edit.stopEditing(True)
This allowed my tool to run, but what I didn't understand when testing is that since the workspace variable is hardcoded to the file path of the database connection itself, it's actually referencing the default version regardless of what version the user is connected to within their MXD. Whenever the user tries to run this script while connected to their version, they get an error that says "workspace already in transaction mode". Connecting back to the default version allows the tool to run properly, but that is not ideal.
Aside from having a user input to assign the workspace version, is there a property somewhere that allows me to grab the version that is being edited? I found some possible leads through google, but I can't seem to get any of them to do what I need.
ListVersions—ArcPy Functions | ArcGIS Desktop
Current Workspace (Environment setting)—Help | ArcGIS for Desktop
ArcGIS Help 10.1 - Current Workspace
ListLayers—Help | ArcGIS for Desktop
Using environment settings in Python—Geoprocessing and Python | ArcGIS Desktop
I attempted to run the list layers operation to see if I could grab a single layer and then access the version properties of that layer, but it didn't seem to work. In testing, I couldn't even figure out how to print the layer:
layers = arcpy.mapping.ListLayers(mxd)
for layer in layers:
if layer == '*my layer*':
print layer
I couldn't even get this to print, let alone access the properties of it. Just a note, if I leave out the IF statement in the above code, all layers print just fine. I can't seem to get one to print when calling out a single layer, though. The print is unimportant, I was just trying to get access to it but I couldn't get past this part of it.
*NOTE* Working code:
desc = arcpy.Describe(r"link_to_sde")
cp = desc.connectionProperties
version = cp.version
This assigns your current version to the "version" variable.