How to grab current version

1111
5
Jump to solution
10-09-2017 09:27 AM
by Anonymous User
Not applicable

*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. 

0 Kudos
1 Solution

Accepted Solutions
JamesCrandall
MVP Frequent Contributor

You should be able to get the version info from the feature classes themselves.  Just add to my previous example some ESRI sample code:

import arcpy
import os

mxdpath = os.path.join('PathToTheMxd','SomeMXDFile.mxd')
mxd = arcpy.mapping.MapDocument(mxdpath)
df = arcpy.mapping.ListDataFrames(mxd)[0]

for lyr in arcpy.mapping.ListLayers(mxd, "", df):
    print lyr.name
    dirname = os.path.dirname(arcpy.Describe(lyr).catalogPath)
    desc = arcpy.Describe(dirname)

    # Print workspace properties
    #
    print("%-24s %s" % ("Connection String:", desc.connectionString))
    print("%-24s %s" % ("WorkspaceFactoryProgID:", desc.workspaceFactoryProgID))
    print("%-24s %s" % ("Workspace Type:", desc.workspaceType))

    # Print Connection properties
    #
    cp = desc.connectionProperties
    print("\nDatabase Connection Properties:")
    print("%-12s %s" % ("  Server:", cp.server))
    print("%-12s %s" % ("  Instance:", cp.instance))
    print("%-12s %s" % ("  Database:", cp.database))
    print("%-12s %s" % ("  User:", cp.user))
    print("%-12s %s" % ("  Version:", cp.version))

View solution in original post

5 Replies
JamesCrandall
MVP Frequent Contributor

I'm not fully clear on your issue(s) but to access the layers within an mxd you could try something like:

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
            
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
  if lyr.name == "SomeLayerName":
      'do stuff to the lyr
0 Kudos
by Anonymous User
Not applicable

I need to access the version that the users are working in, though.  The particular tool in question is one that I recently had GeoNet help on where I wanted to take all entries from one feature class, and create a user specified number of entries in a second related table that then concatenated an ID for each new entry.  That tool works just fine, but to get it to work I had to use the startEditing/stopEditing functions as I kept getting some sort of "state" error (I don't recall at this time).  The problem, though, is that my arcpy.da.Editor() function is referencing the workspace variable, which is defined by using the file path to our SDE connection.  Using this method of referencing the database connection, it is going to grab the default version of that database.  I need to figure out how to define my workspace as the current version that I am connected to, not the default. 

I can have the user input their version, then use that to help define the version, but that isn't really a streamlined process.  I want to pull that parameter from somewhere, if it is possible to do so, and then use it to set define the workspace within the tool. 

0 Kudos
JamesCrandall
MVP Frequent Contributor

You should be able to get the version info from the feature classes themselves.  Just add to my previous example some ESRI sample code:

import arcpy
import os

mxdpath = os.path.join('PathToTheMxd','SomeMXDFile.mxd')
mxd = arcpy.mapping.MapDocument(mxdpath)
df = arcpy.mapping.ListDataFrames(mxd)[0]

for lyr in arcpy.mapping.ListLayers(mxd, "", df):
    print lyr.name
    dirname = os.path.dirname(arcpy.Describe(lyr).catalogPath)
    desc = arcpy.Describe(dirname)

    # Print workspace properties
    #
    print("%-24s %s" % ("Connection String:", desc.connectionString))
    print("%-24s %s" % ("WorkspaceFactoryProgID:", desc.workspaceFactoryProgID))
    print("%-24s %s" % ("Workspace Type:", desc.workspaceType))

    # Print Connection properties
    #
    cp = desc.connectionProperties
    print("\nDatabase Connection Properties:")
    print("%-12s %s" % ("  Server:", cp.server))
    print("%-12s %s" % ("  Instance:", cp.instance))
    print("%-12s %s" % ("  Database:", cp.database))
    print("%-12s %s" % ("  User:", cp.user))
    print("%-12s %s" % ("  Version:", cp.version))
by Anonymous User
Not applicable

Okay, that page I linked is making more sense now.  Instead of running the print operation, I can simply do something like this:

desc = arcpy.Describe(r"C:data\Connection to state.sde")
cp = desc.connectionProperties
version = cp.version

That worked in regards to getting a variable that equals the current version that I am connected to. Thanks for that.  Now I just need to figure out how to reference that in the workspace.

Since that last suggestion answered my question, I'll mark it as the correct answer. 

0 Kudos
JamesCrandall
MVP Frequent Contributor

This thread shows how they set the workspace to a specific version:

https://gis.stackexchange.com/questions/123621/how-to-get-a-search-cursor-on-a-particular-version-in...