Select to view content in your preferred language

How To Verify That A Feature Service's Version Was Changed

150
4
Jump to solution
Wednesday
JackC
by
Occasional Contributor

Hello and Happy New Year!

I am writing a Python script that ingests a layer from a branch-versioned feature service, changes the version, and then updates the data. I've not found any way to verify that arcpy.ChangeVersion_management() actually changed the version, other than assuming that no exception being thrown means it was successful.

There is no workspace property on the feature layer's Describe object, and no other properties other than canVersion and isVersioned. Has anyone else encountered this and found a way to check the active version? Thanks

- Jack C

0 Kudos
1 Solution

Accepted Solutions
AlfredBaldenweck
MVP Regular Contributor

Try using a layer's connection properties?

aprx = arcpy.mp.ArcGISProject("CURRENT")
mp = aprx.activeMap
lay = mp.listLayers()[0]
print(lay.connectionProperties["connection_info"]["version"])
#dbo.DEFAULT

Try checking it before and after running your changer.

 Caveat here is I've never worked with branch-versioned stuff, but this is how it works for traditional versioning.

View solution in original post

4 Replies
AlfredBaldenweck
MVP Regular Contributor

Try using a layer's connection properties?

aprx = arcpy.mp.ArcGISProject("CURRENT")
mp = aprx.activeMap
lay = mp.listLayers()[0]
print(lay.connectionProperties["connection_info"]["version"])
#dbo.DEFAULT

Try checking it before and after running your changer.

 Caveat here is I've never worked with branch-versioned stuff, but this is how it works for traditional versioning.

JackC
by
Occasional Contributor

Hey Alfred,

Thanks for the response! Implementing an ArcGISProject into the script was my next attempt and I should have tried that before posting because indeed I can get the version of a layer in the ToC.

0 Kudos
MikeMillerGIS
Esri Frequent Contributor

Here is a function I use

 

def get_workspace(desc, version) -> tuple[str, str]:
    if getattr(desc, "dataType", None) == "ShapeFile":
        return desc.path, "folder"
    if desc.path.casefold().startswith("http"):
        if not version or version.casefold() == "sde.default":
            return desc.path, "service"
        else:
            return f"{desc.path};version={version}", "service"
    else:
        return get_workspace_desc(desc).catalogPath, "gdb"


def active_edits(self, layer):
	# Dont ask me how this works, I forget
	if not layer or not layer.connectionProperties:
		return True
	else:
		return False

def change_version(self, layer_name):
        # Note, this does not appear to work with a layer object, use the layer name
        if self.workspace_version is None or self.workspace_version.casefold() == "sde.default":
            return None
        if not self.is_service:
            return None
        # Not sure if the normalization is needed
        logger.debug(self.workspace)
        pth = get_workspace(arcpy.Describe(layer_name), None)[0]
        logger.debug(pth)
        if os.path.normpath(self.workspace) != os.path.normpath(pth):
            return None

        logger.debug(f"Changing {getattr(layer_name, 'name', layer_name)} to {self.workspace_version}")
        result = arcpy.ChangeVersion_management(layer_name, "BRANCH", self.workspace_version, None, "EXCLUDE")[0]
        logger.debug(result)

        if self.active_edits(result):
            logger.error("Cannot preform Calculation with Active Edit Session")
            raise SystemExit(1)
        try:
            logger.debug(result.connectionProperties)
            if result.connectionProperties["connection_info"]["version"] != self.workspace_version:
                logger.error("Version was not changed, exiting")
                raise SystemExit(1)
        except Exception as e:
            logger.error(f"Unable to change version. {e}")
            raise SystemExit(1)
        return result

 

JackC
by
Occasional Contributor

Hey Mike,

This is a great code reference for future use, thank you!

0 Kudos