AnsweredAssumed Answered

Set datasource with ArcObjects

Question asked by DCSlatunn on Sep 12, 2018
Latest reply on Sep 13, 2018 by jeffmatson

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.

Outcomes