I am trying to use the CIM definition on a query layer to set it to use its own metadata. This causes the layer to get disconnected. The code is along the following lines
arcpy.management.MakeQueryLayer(..paras..)
arcpy.management.SaveToLayerFile(layername, layerfile, "ABSOLUTE")
layer = map.addLayer(arcpy.mp.LayerFile(layerfile), "BOTTOM")[0]
l_cim = layer.getDefinition('V2')
l_cim.useSourceMetadata = False
# This causes the layer to become disconnected, regardless of whether any properties were set on the
#CIM definition
layer.setDefinition(l_cim)
Can anyone offer any suggestions?
Thanks,
Solved! Go to Solution.
I already detected this same issue of disconnection for Query Layers in ArcGIS Pro 2.4... seems nothing changed...
For the particular case I had, I worked around the issue by dumping the layer to a Pro layer file (*.lyrx) by saving it out, and editing the JSON directly using the Python 'json' module and "json.load" / "json.dump" methods.
Note that this is wholly outside the realm of safe edits that ESRI probably supports, but if you are careful, and understand the CIM and know how to mentally translate it to the respective JSON structure, you can in fact edit the json directly by modifying the hierarchical dictionary structure that "json.load" will give you access to, and then dumping it back to the layer file, and subsequently re-load the layer file in ArcGIS.
Note that the *.lyrx layer files are in fact stored in text format, not binary, so you can luckily inspect them and the CIM structure in a text editor, which helps getting it right.
I know..., it makes you grind your teeth... knowing you should be able to do this no problem using "layer.getDefinition/setDefinition"...
Python CIM access—ArcGIS Pro | Documentation
Did you read the documentation and the first caution, before you proceed with what can be done?
I already detected this same issue of disconnection for Query Layers in ArcGIS Pro 2.4... seems nothing changed...
For the particular case I had, I worked around the issue by dumping the layer to a Pro layer file (*.lyrx) by saving it out, and editing the JSON directly using the Python 'json' module and "json.load" / "json.dump" methods.
Note that this is wholly outside the realm of safe edits that ESRI probably supports, but if you are careful, and understand the CIM and know how to mentally translate it to the respective JSON structure, you can in fact edit the json directly by modifying the hierarchical dictionary structure that "json.load" will give you access to, and then dumping it back to the layer file, and subsequently re-load the layer file in ArcGIS.
Note that the *.lyrx layer files are in fact stored in text format, not binary, so you can luckily inspect them and the CIM structure in a text editor, which helps getting it right.
I know..., it makes you grind your teeth... knowing you should be able to do this no problem using "layer.getDefinition/setDefinition"...
Thanks Marco. My aim is to set the description on a layer. This was previously available as a property on the layer. I will have a look at the json load approach as I am already creating a layer file.
Thanks I worked around this issue using json load and dump.
How can I get the cim_version? From a normal open map in Pro, how do I open things in json like the docs describe?
I need the cim_version but have no idea how to get it (in Pro).