Does anyone know how to change a datasource with code with ArcObjects? In 10.0 I used:
IDataSourceHelperLayer dsh = new DataSourceHelper() as IDataSourceHelperLayer; dsh.FindAndReplaceWorkspaceNamePath(layer, pDatasetName.WorkspaceName.PathName.ToString(), newName, false);
This appears to be doing something in 10.6.1, but it is not changing the source as it should. To explain, if I run the code and then open ArcMap, all of my layers still have "!" next to them. However, if I check the datasource with:
IFeatureLayer layer = enumLayer.Next() as IFeatureLayer; while (layer != null) { IDataLayer2 pDataLayer = layer as IDataLayer2; if (pDataLayer.DataSourceName is IDatasetName) { IDatasetName pDatasetName = pDataLayer.DataSourceName as IDatasetName; string oldName = pDatasetName.WorkspaceName.PathName.ToString().ToLower(); } layer = enumLayer.Next() as IFeatureLayer; }
The oldName shows that the path HAS been changed. Why isn't it displaying in the map? Am I missing a step (I save the map)? What is the best way to programmatically change datasources in 10.6.1? I need to create a method to loop through lots of MXDs and fix the datasources from relative paths to UNC paths.
Solved! Go to Solution.
Here is some old code from 10.3, you can use IMapAdmin2:
If newFC IsNot Nothing Then
featureLyr.FeatureClass = newFC
Dim mapAdmin2 As IMapAdmin2 = map
mapAdmin2.FireChangeFeatureClass(ds, newFC)
m_bSetDirty = True
End If
I think to get to the bottom of this you need to show the full code, how you get a handle on the MXD, how you get a handle on the layers in the TOC, your update code and then when you save it. It's not clear where your checking code above is run.
Also looking at IDataSourceHelperLayer.FindAndReplaceWorkspaceNamePath why are you not setting the final parameter to TRUE to validate? Never used this interface so unsure what happens if validation fails.
Here is some old code from 10.3, you can use IMapAdmin2:
If newFC IsNot Nothing Then
featureLyr.FeatureClass = newFC
Dim mapAdmin2 As IMapAdmin2 = map
mapAdmin2.FireChangeFeatureClass(ds, newFC)
m_bSetDirty = True
End If