Select to view content in your preferred language

arcpy.management.ChangeVersion BRANCH not working in toolbox

1818
12
04-24-2023 08:06 AM
Labels (3)
HervéW
Emerging Contributor

Hi,

When trying to change the version of a BRANCH service feature class in ArcGIS Pro 2.9.8 with following line of code : arcpy.management.ChangeVersion(in_features=Canalisations_GAZ, version_type="BRANCH", version_name="sde.DEFAULT", date="", include_participating="INCLUDE"), it works well when executed from the Python window or using Change Version (Data Management)—ArcGIS Pro | Documentation in the Geoprocessing window.

But when trying to execute the same line of code in a Python script in a toolbox, no error is triggered, but the Feature Service Feature Class is not changed to the sde.DEFAULT version.

Is there any limitation in using that line of code inside a toolbox ?

I already searched the community forum and this behavior is mentioned in some posts, but without any solution.

The purpose is to have the possibility to change the version of BRANCH service feature classes from one version to another (DEFAULT or not).

Any clue on this issue ?

Regards,

Hervé

0 Kudos
12 Replies
wilmoreno
New Contributor
from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection

class AprxChangeVersion:
    
    def __init__(self, aprx_obj: str, map_name: str, url_fs: str, version_name: str, version_guid: str) -> None:
        self.aprx_obj = aprx_obj
        self.url_fs = url_fs
        self.version_name = version_name
        self.version_guid = version_guid
        self.map_obj = self.aprx_obj.listMaps(map_name)[0]
        self.layers_tables = []
        for lyr in self.map_obj.listLayers():
            self.layers_tables.append({"layer_name":lyr.name, "layer_id":lyr.connectionProperties["dataset"], "current_layer_obj":lyr})
        for lyr in self.map_obj.listTables():
            self.layers_tables.append({"layer_name":lyr.name, "layer_id":lyr.connectionProperties["dataset"], "current_layer_obj":lyr})

    def get_default_lyr_conn_dict(self, layer_id: str, layer):
        return {
            "connection_info": {
                "url": self.url_fs,
                "version": layer.connectionProperties["connection_info"]["version"],
                "versionguid": layer.connectionProperties["connection_info"]["versionguid"],
            },
            "dataset": f"{layer_id}",
            "workspace_factory": "FeatureService",
        }

    def get_version_lyr_conn_dict(self, layer_id: str):
        return {
            "connection_info": {
                "url": self.url_fs,
                "version": f"{self.version_name}",
                "versionguid": f"{self.version_guid}",
            },
            "dataset": f"{layer_id}",
            "workspace_factory": "FeatureService",
        }

    def switch_to_default(self):
        for layer in self.layers_tables:
            default_lyr_conn_dict = self.get_default_lyr_conn_dict(
                layer_id=layer["layer_id"]
            )
            version_lyr_conn_dict = self.get_version_lyr_conn_dict(
                layer_id=layer["layer_id"]
            )

            layer["current_layer_obj"].updateConnectionProperties(
                # version_lyr_conn_dict, default_lyr_conn_dict
                None, default_lyr_conn_dict
            )

    def switch_to_version(self):
        for layer in self.layers_tables:
            # default_lyr_conn_dict = self.get_default_lyr_conn_dict(
            #     layer_id=layer["layer_id"]
            # )
            version_lyr_conn_dict = self.get_version_lyr_conn_dict(
                layer_id=layer["layer_id"]
            )

            layer["current_layer_obj"].updateConnectionProperties(
                # default_lyr_conn_dict, version_lyr_conn_dict
                None, version_lyr_conn_dict
            )
        self.aprx_obj.save()

def change_version():
    print("change_version...")
    gis = GIS("pro")
    name_version = "VERSION_TEST"
    versionNameProcess = "{}.2024_{}".format(gis.users.me.username, name_version)
    for lyr in active_map.listLayers():
        arcpy.management.ChangeVersion(lyr, "BRANCH", version_name=versionNameProcess)
    for lyr in active_map.listTables():
        arcpy.management.ChangeVersion(lyr, "BRANCH", version_name=versionNameProcess)
    aprx.save()
    version_ok = True
    for lyr in active_map.listLayers():
        if lyr.connectionProperties["connection_info"]["version"] != versionNameProcess:
            version_ok = False
            print(f"Error change version {lyr.name}. Se utilizara otro metodo")
            break
    if version_ok is False:
        print("change version AprxChangeVersion...")
        serviceItemId = ""
        gisContentParcelService = gis.content.get(serviceItemId)
        p_flc = FeatureLayerCollection.fromitem(gisContentParcelService)
        version_manager = p_flc.versions
        version = version_manager.get(version=versionNameProcess)
        version_guid = version.properties.versionGuid
        arc_cv = AprxChangeVersion(aprx, "MAP_RTL", gisContentParcelService.url, versionNameProcess, version_guid)
        arc_cv.switch_to_version()
    return True


if __name__ == '__main__':
    change_version()
0 Kudos
LanceKirby2
Frequent Contributor
That’s definitely the same situation I dealt with. Thanks.
0 Kudos