Question

Python private parameter for ArcSDE version name

  • 25 April 2014
  • 5 replies
  • 1 view

Badge +5
  • Contributor
  • 25 replies
I'm not sure if this is more a Python, FME, or Esri question...

 

 

So here's the situation - a user has a MXD document open connected to an ArcSDE database with a version.  I have a workbench to write to this database but the user doesn't want to manually type in the ArcSDE version name as a user parameter (how it's currently setup).  Instead they want what ever version they are current connected to in the MXD to be used in the workbench.  

 

 

I've scraped together some Python code I've found to create a text file of the current version name for an open MXD but I'd like to use this value (or maybe instead the returned value) as a new private parameter to use in the workbench.  

 

 

Two issues right now: 
  1.   It lists the version name for all layers (I just want one record)
  2.   It writes to a text file 
    •   Should it just return the version name so FME can use it as a parameter?  If so, how do I do this?
     
 Here's the code I have so far (I'm a Python novice so please bear with me!)

 

 import arcpy, os mxd = arcpy.mapping.MapDocument(r'CURRENT') text_file = open("C:\\temp\\FME_Testing\\Temp\\Test3.txt","w") for lyr in arcpy.mapping.ListLayers(mxd):     if lyr.supports("SERVICEPROPERTIES"):         servProp = lyr.serviceProperties         if lyr.serviceProperties["ServiceType"] != "SDE":             print "Service Type: " + servProp.get('ServiceType', 'N/A')         else:             text_file.write (servProp.get('Version')) text_file.close()         del mxd
 

 


5 replies

Userlevel 4
Hi,

 

 

a scripted parameter must return one string value, so you could do something like this:

 

 

---

 

import arcpy, os

 

mxd = arcpy.mapping.MapDocument(r'CURRENT')

 

version = 'UNKNOWN VERSION' # Default value

 

for lyr in arcpy.mapping.ListLayers(mxd):

 

    if lyr.supports("SERVICEPROPERTIES"):

 

        servProp = lyr.serviceProperties

 

        if lyr.serviceProperties["ServiceType"] != "SDE":

 

            version = servProp.get('ServiceType', 'N/A')

 

        else:

 

            version = servProp.get('Version')

 

del mxd

 

return version

 

---

 

 

Remember that you'll have to use the Python interpreter that has the arcpy module installed, which isn't usually the case for the Python interpreter installed with FME.

 

 

Have a look here (http://fmepedia.safe.com/articles/How_To/Choosing-a-different-Python-Interpreter-installation) for more details about selecting the Python interpreter used by ArcGIS.

 

 

David
Userlevel 4
The script above will return the version for the last layer found in the MXD, which might not always be the best solution, e.g. if the last layer if a WMS base layer or such.

 

 

This version will return the first SDE version found and ignore the rest:

 

 

---

 

import arcpy, os

 

mxd = arcpy.mapping.MapDocument(r'CURRENT')

 

version = 'UNKNOWN VERSION' # Default value

 

for lyr in arcpy.mapping.ListLayers(mxd):

 

    if lyr.supports("SERVICEPROPERTIES"):

 

        servProp = lyr.serviceProperties

 

        if lyr.serviceProperties["ServiceType"] == "SDE":

 

            return servProp.get('Version')

 

del mxd

 

return version

 

---

 

 

David
Badge +5
@David R.

 

 

I'm using the code from your seond post which works great!  Thank you!

 

 

The issue I have now is with: 
 mxd = arcpy.mapping.MapDocument(r'CURRENT')
 I have my workbench saved as an FME ETL Tool in a ArcToolbox which I've added as a tool to the ArcMap toolbar for the end user to use.  This works great but i get an error when running the tool with the map document proptery in Python is set to CURRENT.  The error I get is:

 

 RuntimeError: Object: CreateObject cannot open map document
 This makes sense since FME doesn't know that I have a MXD open.  Is there a solution for this?  The workaround I can think of is to hardcode the map document path (not ideal) but the real kicker is that the MXD has to be saved prior to running FME.  If I don't save before I run the workbench, it writes to whatever version it was connected to at the last save.  

 

 

Userlevel 4
Hi,

 

 

not too surprised about that error. I recommend you create a published parameter called "CURRENT_MXD_DOCUMENT", which you can then reference in your script:

 

 

mxd = arcpy.mapping.MapDocument(FME_MacroValues['CURRENT_MXD_DOCUMENT'])

 

 

You will need to use the full path to the MXD file in the published parameter.

 

 

And yes, you will have to save the MXD first, I don't think there's any way around that...

 

 

David
Badge +5
@David R.

 

 

Is there a way to only look in a specific feature dataset for version names?  I'm seeing situations where there are feature classes from multiple feature datasets in an MXD and I'm only concerned about one particular feature dataset (i.e. P_Electric_Distribution that contains 21 feature classes I need to write to).

Reply