Change connection properties from EGDB to Feature Service

2110
12
11-01-2021 12:13 PM
JoeBorgione
MVP Emeritus

I have a number of ArcGIS Pro projects where I need to change one layer that used to be a feature class in our EGDB (aka SDE) to a feature service.

The online help provides various approaches and in this post@GaetanPRU provides some code to update the connection properties of a shape file.  Following that lead:

 

 

# I have a list created a priori of my Pro projects:
for a in aprxList:
    aprx = arcpy.mp.ArcGISProject(a[0])
    map = aprx.listMaps("Live SDE")[0]
    layer = map.listLayers("MSD.SLCOMSD.AddressGridMSD")[0]
    cp = layer.connectionProperties

 

 

If I list cp, I get the dictionary of the connection properties:

 

 

cp
{'dataset': 'MSD.SLCOMSD.AddressGridMSD',
 'workspace_factory': 'SDE',
 'connection_info': {'authentication_mode': 'DBMS',
  'database': 'myDB',
  'dbclient': 'sqlserver',
  'db_connection_properties': '"bla bla bla',
  'password': '<yeahRight>',
  'instance': '"sde:sqlserver:bla bla bla',
  'server': 'bla bla bla',
  'user': 'myUser',
  'version': 'sde.DEFAULT'}}

 

 

However, I'm unsure as to what my next step is to update the connection properties to a feature service.

That should just about do it....
0 Kudos
12 Replies
arunachalammak
New Contributor II

Hi All,

Here is the simple solution, after a couple of research I found properties are updating correctly for me. You missed to add other properties from existingDict to replaceDict and set its value to an empty string, like in the sample below. Try it and see the magic.

replaceDict = lyr.connectionProperties
replaceDict["dataset"] = str(dsID)
replaceDict["workspace_factory"] = "FeatureService"
replaceDict["connection_info"]["url"] = str(baseURL)
replaceDict["connection_info"]["authentication_mode"] = ""
replaceDict["connection_info"]["dbclient"] = ""
replaceDict["connection_info"]["db_connection_properties"] = ""
replaceDict["connection_info"]["password"] = ""
replaceDict["connection_info"]["instance"] = ""
replaceDict["connection_info"]["server"] = ""
replaceDict["connection_info"]["user"] = ""
replaceDict["connection_info"]["version"] = ""
lyr.updateConnectionProperties(lyr.connectionProperties, replaceDict)

Thanks

Arun

 

0 Kudos
JoeBorgione
MVP Emeritus

@arunachalammak - Thanks for your suggestion!  I retired at the end of 2021 and haven’t written a line of Python since!

 

That should just about do it....
DorothyShreve
New Contributor II

I'm running into this same issue in ArcGIS Pro 3.1.2. Figured it was a bug, and glad I found this thread as it was very helpful. Posting code here to hopefully help someone else.

This code specifically re-sources a layerfile as we have different symbology in ArcGIS Pro and simply removing and replacing the layer with the symbology from the feature service is not what we want.

# Get AGOL Layer information
new_cp = agol_lyr.connectionProperties
new_cp['connection_info']['dataset'] = '0'
new_cp['connection_info']['alias'] = 'Layer Alias'
new_cp['connection_info']['workspace_factory'] = 'FeatureService'

for lyr in temp_m.listLayers()[2:]:
    old_cp_info = lyr.connectionProperties['connection_info']
    # if the key in the original connection_info is not in the new cp
    # add the key as a blank value to the new cp
    for key in old_cp_info:
        if key not in list(new_cp['connection_info'].keys()):
            new_cp['connection_info'][key] = ''
    lyr.updateConnectionProperties(lyr.connectionProperties,
                                   new_cp)
    print(lyr.connectionProperties)

 

Would be great if this was fixed sometime soon.