Hello Everyone, first post. Thanks for being a great community.
I am trying to update layer connection properties in an ArcGIS Pro project with arcpy. The feature classes were formerly in a feature dataset in a file geodatabase. Now they are in the same file geodatabase, but no feature dataset.
I have seen how the feature dataset can be updated using layer CIM, but I haven't figured out how to completely remove the feature dataset attribute to fix the broken data sources that no longer reside in a feature dataset.
Is this possible?
Thank you!
Marshal
Solved! Go to Solution.
I found a solution, although there may be an easier way to directly remove the feature dataset attribute from a layer's CIM dataConnection properties.
I created a template aprx with layer that has the desired connection properties, other than the feature class name. I will use this template aprx/layer to create CIM dataConnection properties, update the feature class name, and then apply to the broken layers.
I believe I can scale this logic to iterate through all my broken projects and fix the broken data connections. Please note, this works in my specific case because all my datasets have the same feature class name and same file geodatabase as the broken layers (only needed to remove feature dataset). If you need to change other attributes, additional code will be required.
Here is the code that worked for me. Hope this helps someone!
# Get connection properties from template project/layer with corrected data connection
template_aprx = arcpy.mp.ArcGISProject(r'P:\GIS\PROJECTS\TEMPLATE\FIX_DATASOURCE.aprx')
template_map = template_aprx.listMaps()[0]
template_lyr = template_map.listLayers('FIXED_LAYER')[0]
templateCIM = template_lyr.getDefinition('V2')
template_dc = templateCIM.featureTable.dataConnection
# Fix all broken data connections in current project using template connection properties
aprx = arcpy.mp.ArcGISProject('CURRENT')
map = aprx.listMaps()[0]
layers = map.listLayers()
for lyr in layers:
if lyr.isFeatureLayer:
if lyr.isBroken:
print('Updating {} layer'.format(lyr))
source = lyr.dataSource
fc_name = source.split('\\')[-1]
template_dc.dataset = fc_name
layerCIM = lyr.getDefinition('V2')
layerCIM.featureTable.dataConnection = template_dc
lyr.setDefinition(layerCIM)
I found a solution, although there may be an easier way to directly remove the feature dataset attribute from a layer's CIM dataConnection properties.
I created a template aprx with layer that has the desired connection properties, other than the feature class name. I will use this template aprx/layer to create CIM dataConnection properties, update the feature class name, and then apply to the broken layers.
I believe I can scale this logic to iterate through all my broken projects and fix the broken data connections. Please note, this works in my specific case because all my datasets have the same feature class name and same file geodatabase as the broken layers (only needed to remove feature dataset). If you need to change other attributes, additional code will be required.
Here is the code that worked for me. Hope this helps someone!
# Get connection properties from template project/layer with corrected data connection
template_aprx = arcpy.mp.ArcGISProject(r'P:\GIS\PROJECTS\TEMPLATE\FIX_DATASOURCE.aprx')
template_map = template_aprx.listMaps()[0]
template_lyr = template_map.listLayers('FIXED_LAYER')[0]
templateCIM = template_lyr.getDefinition('V2')
template_dc = templateCIM.featureTable.dataConnection
# Fix all broken data connections in current project using template connection properties
aprx = arcpy.mp.ArcGISProject('CURRENT')
map = aprx.listMaps()[0]
layers = map.listLayers()
for lyr in layers:
if lyr.isFeatureLayer:
if lyr.isBroken:
print('Updating {} layer'.format(lyr))
source = lyr.dataSource
fc_name = source.split('\\')[-1]
template_dc.dataset = fc_name
layerCIM = lyr.getDefinition('V2')
layerCIM.featureTable.dataConnection = template_dc
lyr.setDefinition(layerCIM)
Hi there, we did something almost the same:
1. We created a new project file with the corrected datasources.
2. Extracted the dataConnection properties.
3. Stepped through a folder structure that had all the project files, opened up each project and found all that were broken and replaced them with the corrected datasource from step 2.
Note:
A. We had V3 (version 3) of the layer definition file compared to V2 in your code.
B. Very surprised that the initial change where the featureDataSet was removed from the EGDB, that it didn't break anything in the project files.