Conditional Statement UnregisterAsVersioned

904
4
Jump to solution
07-02-2018 01:59 PM
LarryAdgate
Occasional Contributor

I need assistance writing a conditional statement that says if the dataset is already Unregistered to skip the balance and go directly to comp = arcpy.Compress_management(editDB)  

Thank You in advance........Larry Adgate

import arcpy
editDB = "Database Connections\\Arden@gsw_sde.sde"
workspace = editDB

arcpy.env.workspace = workspace

#Need a conditonal Statment: if dataset is already UnregisterAsVersioned to go directly to comp
datasets = arcpy.ListDatasets("*", "Feature")
print datasets
for dataset in datasets:
fcList = arcpy.ListFeatureClasses("*","",dataset)
for fc in fcList:
print fc
arcpy.UnregisterAsVersioned_management(fc,"NO_KEEP_EDIT","COMPRESS_DEFAULT")

#if already Unregistered, skip the balance and go to below:   

comp = arcpy.Compress_management(editDB)

datasets = arcpy.ListDatasets("*", "Feature")
for dataset in datasets:
fcList = arcpy.ListFeatureClasses("*","",dataset)
for fc in fcList:
arcpy.RegisterAsVersioned_management(fc, "NO_EDITS_TO_BASE")

0 Kudos
1 Solution

Accepted Solutions
JoeBorgione
MVP Emeritus

Your if statement as written is looking for a feature class named 'Versioned'.  Take a look at Dataset properties—ArcPy Functions | ArcGIS Desktop 

You need to check :

arcpy.Describe(fc).isVersioned
# returns true if it is

if arcpy.Describe(fc).isVersioned:
  do what you want
else
  do something else
That should just about do it....

View solution in original post

4 Replies
DanPatterson_Retired
MVP Emeritus

Larry can you format your code so people can check with the proper indentation

Code Formatting the Basics ++

0 Kudos
LarryAdgate
Occasional Contributor

I apologize, here is my revised code with corrected indentations.......This script works except when I get to my conditional statement........My goal.....If a featureclass is already unregistered or is not versioned, the script needs to continue to the next featureclass w/o an error   

import arcpy
editDB = "Database Connections\\Arden@gsw_sde.sde"
workspace = editDB

arcpy.env.workspace = workspace


datasets = arcpy.ListDatasets("*", "Feature")
print datasets
for dataset in datasets:
     fcList = arcpy.ListFeatureClasses("*","",dataset)
     for fc in fcList:
         #My error begins below...

          if fc == 'Versioned':
          continue
          print fc
         arcpy.UnregisterAsVersioned_management(fc,"NO_KEEP_EDIT","COMPRESS_DEFAULT")

arcpy.Compress_management(editDB)        

Runtime error Traceback (most recent call last): File "<string>", line 16, in <module> File "d:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\management.py", line 16702, in UnregisterAsVersioned raise e ExecuteError: ERROR 000131: The dataset is not versioned.
>>>

0 Kudos
DanPatterson_Retired
MVP Emeritus

The if statement isn't indented properly... use the code formatting the link I sent.

Also you can't compare a featureclass (fc) to a text string.  Was that the line you were using?  

JoeBorgione
MVP Emeritus

Your if statement as written is looking for a feature class named 'Versioned'.  Take a look at Dataset properties—ArcPy Functions | ArcGIS Desktop 

You need to check :

arcpy.Describe(fc).isVersioned
# returns true if it is

if arcpy.Describe(fc).isVersioned:
  do what you want
else
  do something else
That should just about do it....