Select to view content in your preferred language

Update data sources. enterprise server. dictionary. updateConnectionProperties

556
11
Jump to solution
04-04-2024 06:21 AM
SGTomlins
New Contributor III

Greetings,

I have successfully updated connection properties in all scenarios EXCEPT enterprise to enterprise (dev to production).  The following code is my example.  I have 2.9 (yes i know outdated)  Python 1.9.1 ( again. outdated).

Is this a limitation of the updateConnectionProperties?

Is this related the attached screenshot, where the Update Data Sources is full of "Unsupported Items"

Feel like I am missing something obvious...  ?   help... 

import arcpy,pprint
# ## SGTomlins 3/27/24
# ## Examples of workspaces that can be used. Either a .gdb or a connection string.
# ## arcpy.env.workspace = r"C:\SGT\ESRI\ArcGIS_Pro\LoadLayerFiles\LoadLayerFiles.gdb"
# ## arcpy.env.workspace = r"C:\SGT\ESRI\_ConnectStrings\SDE@underdog-az@gis_prod_util_test.sde"
#

# ## find_dict = r'C:\SGT\SDE@underdog-az@gis_prod_util_test.sde.gdb'
# ## replace_dict = r'C:\Data\!Database_Connections\GISReader@underdog-az@gis_prod_util.sde'

find_dict = {'connection_info': {'user': 'sde',
'password': 'guessmenow!',
'version': 'sde.DEFAULT',
'server': 'underdog-az',
'instance': 'sde:sqlserver:underdog-az',
'dbclient': 'sqlserver',
'database' : 'gis_prod_util',
'db_connection_properties' : 'underdog-az'
}}

replace_dict ={'connection_info': {'user': 'sde',
'password': 'guessmelater',
'version': 'sde.DEFAULT',
'server': 'tinkerbell-az',
'instance': 'sde:sqlserver:tinkerbell-az\TINKER2019',
'dbclient': 'sqlserver',
'database' : 'gis_prod_util',
'db_connection_properties' : 'tinkerbell-az'
}}



# ## sets some variables and the output path for the created .lyrx files
aprfilenew = r'c:\SGT\TINKER2019-ResourceNoUtil31.aprx'

# ## Set your ArcGIS Project to excute the code on.
aprfilein = r'c:\SGT\TINKER2019-Resource_NoUtil.aprx'
p = arcpy.mp.ArcGISProject(aprfilein)

# ## Create lists for "Maps" and "Layers" in the .aprx
cnt=1
aprx = arcpy.mp.ArcGISProject(aprfilein)
for m in aprx.listMaps():
for lyr in m.listLayers():
print (lyr)
lyr.updateConnectionProperties(find_dict, replace_dict)

# ## Return the values of variable to the screen
print("working on it")

# ## Save a copy of the .aprx used in the code above.
p.saveACopy(aprfilenew)

print (".")
print ("..")
0 Kudos
11 Replies
SGTomlins
New Contributor III

Worked like a champ....  !!  Much Obliged.... !!

-Steve

0 Kudos
SGTomlins
New Contributor III

Here is my "working" .py to accomplish my goal of re-sourcing URL's at while being constrained by Pro 2.x

import arcpy,pprint,re
from arcpy.cim.__init__ import GetJSONForCIMObject

from datetime import date, datetime
import time
from timeit import default_timer as timer

# ## Set a bunch of variables
StartTime = datetime.now()
start = timer()
today = date.today()
d1 = today.strftime("%Y%m%d")
now = datetime.now()
seconds = now.strftime("%S")
milliseconds = (seconds * 10)
# Current Time
Time = time.strftime("%I:%M:%S %p", time.localtime())
# Current Day
Day = time.strftime("%m-%d-%Y", time.localtime())

# Create the logging file
txtFile = open("c:\SGT\Logs\ChangeSrcDB_Connection_CIM.txt", "a")
print('Count Feature classes: ' + str(Day) + " " + str(Time))
txtFile.write('#####################################################################################' + "\n")
txtFile.write('Change connection properties started at: ' + str(Day) + " " + str(Time) + "\n")
txtFile.write('#####################################################################################')
txtFile.write("\n")
txtFile.close()

# ## Set your ArcGIS Project to excute the code on.
aprfilein = r'C:\SGT\ESRI\Python\ChangeSource\OriginalSourcePROD.aprx'

# ## Set the name of the aprx to be saved with new data sources
aprfilenew = r'C:\SGT\ESRI\Python\ChangeSource\NewSourceDEV.aprx'

p = arcpy.mp.ArcGISProject(aprfilein)
m = p.listMaps()[0]
l = m.listLayers()[0]

# ## Set the new URL connection string
urlprfx = 'URL=https://tinkerbell-az.ci.janesville.wi.us/devserver/rest/services/'
replaceurl = 'URL=https://tinkerbell-az.ci.janesville.wi.us/devserver/rest/services/Utilities/Utilities/FeatureServer'

# Just having fun with example of taking string apart for use later.. not needed in this example
def extract_base_url(url):
# Define the regex pattern to match the base URL
pattern = r"URL=(https?://[^/]+/[^/]+/[^/]+/[^/]+/[^/]+/[^/]+/)"
match = re.search(pattern, url)
if match:
return match.group(1)
else:
return None


# Get the Layer name, get some information to satisfy need for operation to change source url
for lyr in m.listLayers():
#pprint.pprint(lyr.connectionProperties)
# with open (txtFile, 'a') as f:
# pprint.pprint(lyr.connectionProperties, stream=f)


txtFile = open("c:\SGT\Logs\ChangeSrcDB_Connection_CIM.txt", "a")
txtFile.write('The is the layer name: ' + str(lyr))
txtFile.write("\n")
txtFile.write(' ------------------------------------------------------------------' + '\n')
txtFile.close()

print ('This is the layer name :' + str(lyr))

# Get the layer definition information CIM
lyrCIM = lyr.getDefinition("V2")
c = lyr.getDefinition('V2')
dc = c.featureTable.dataConnection
j = GetJSONForCIMObject(dc, 'V2')
print(j)

connstr = lyrCIM.featureTable.dataConnection.workspaceConnectionString
print ("This is the connection string from the CIM= " + connstr)

# Extract the base URL
base_url = extract_base_url(connstr)
# This loop needs some error trapping or string reconstruction depending on the from/to you are using
if base_url:
print("Base URL:", base_url)
print("Changing the CIM connection properties")
# Change the URL data source
lyrCIM.featureTable.dataConnection.workspaceConnectionString = replaceurl
lyr.setDefinition(lyrCIM)
#lyrCIM = lyr.getDefinition("V2")
c = lyr.getDefinition('V2')
dc = c.featureTable.dataConnection
j = GetJSONForCIMObject(dc, 'V2')
print("J again" + j)

else:
print("Base URL not found.")

print('...... next .....')

# Save a copy of the new apr with it's newly sourced services.
p.saveACopy(aprfilenew)
0 Kudos