I am using updateConnectionProperties() method to change data sources for layer files. The layer is "C:/gis/layer.lyrx". The old path is "C:/gis/oldGDB.gdb/oldDS/FC". The new path is "C:/gis/newGDB.gdb/FC". As you can see, the new data source is not in a feature dataset like the old one. First I tried:
lyr = arcpy.mp.LayerFile('C:/gis/layer.lyrx')
lyr.updateConnectionProperties('C:/gis/oldGDB.gdb', 'C:/gis/newGDB.gdb')
lyr.saveACopy('C:/gis/newLayer.lyrx')
When I check the property of newLayer.lyrx, its data source is unchanged - still the FC in oldDS in oldGDB.gdb. Then I tried:
lyr = arcpy.mp.LayerFile('C:/gis/layer.lyrx')
lyr.updateConnectionProperties('', 'C:/gis/newGDB.gdb')
lyr.saveACopy('C:/gis/newLayer.lyrx')
The data source did change this time, but it assumed that the new source is still in a dataset, and thus it could not find the new source. Lastly, I tried:
lyr = arcpy.mp.LayerFile('C:/gis/layer.lyrx')
lyr.updateConnectionProperties('', {'connection_info':
{'database': 'C:/gis/newGDB.gdb'},
'dataset': '',
'workspace_factory': 'File Geodatabase'})
lyr.saveACopy('C:/gis/newLayer.lyrx')
Again, the data source did not change. How can I make it work?