import arcpy, os Layer_Replacements={old_path:[new_dataset,lyr]} for root, dirs, files in os.walk(top_directory_of_search): mxds=[x for x in files if x.endswith('.mxd')] if len(mxds)!=0: for mxd_path in [os.path.join(root,mxd) for mxd in mxds]: paths.append(mxd_path) for mxd_path in paths: mxd=arcpy.mapping.MapDocument(mxd_path) for df in arcpy.mapping.ListDataFrames(mxd): for layer in arcpy.mapping.ListLayers(df): if layer.dataSource in Layer_Replacements: was_visible=layer.visible layer.replaceDataSource(Layer_Replacements[r'Database Connections\OSA@sde@bay-gis.sde','SDE_WORKSPACE',Layer_Replacements[layer.dataSource][0]) symbology=arcpy.mapping.Layer(Layer_Replacements[layer.dataSource][1]) arcpy.mapping.UpdateLayer(df,layer,symbology) layer.visible=was_visible mxd.save
To re-open this old thread, as this is exactly what I am trying to find out... Has ESRI added the ability to determine/set whether a layer in the table of contents is collapsed/expanded yet?? I am writing a script that will 'touch' over a thousand layers in my organization's web maps housed in a Pro project, and from what I have read on the Pro/Py3/mp help docs I don't see this as a property of the Layer object yet.
Please, someone tell me I am wrong and that functionality exists!
This will not be supported for arcpy.mapping (ArcMap). But for Pro 2.4 we added finer grained access to many more capabilities via the CIM - Esri's Cartographic Information Model. Here is a link to the help topic:
Python CIM access—ArcPy | Documentation
And here is a sample:
#The following script will expand all group layers in a map's TOC
# but will collapse all feature and raster layers.
p = arcpy.mp.ArcGISProject('current')
m = p.listMaps('Map')[0]
for l in m.listLayers():
if l.isGroupLayer:
l_cim = l.getDefinition('V2') #get the layer's CIM definition
l_cim.expanded = True #expand group layer
l.setDefinition(l_cim) #set the layer's CIM definition
if l.isFeatureLayer or l.isRasterLayer:
l_cim = l.getDefinition('V2')
l_cim.expanded = False #collapse layers
l.setDefinition(l_cim)
Jeff
Thanks Jeff, that is definitely exciting to see!
Starting with Pro 2.4, Python CIM access is available which will provide finer-grained access to more settings/capabilities.
Help topic: https://pro.arcgis.com/en/pro-app/arcpy/mapping/python-cim-access.htm
Video: https://www.youtube.com/watch?v=8wgt8bKD0Ww&feature=youtu.be
Specific to collapsing layers in the TOC, here is an example:
p = arcpy.mp.ArcGISProject('current')
m = p.listMaps('Map')[0]
l = m.listLayers()[0]
l_cim = l.getDefinition('V2') #Get the layer's CIM definition
if l_cim.expanded == True:
l_cim.expanded = False #Collapse the layer
l.setDefinition(l_cim) #Set the updated layer definition