ArcObjects – Repointing Datasource of Layers in ArcGIS Desktop

arcgis-desktoparcobjectsc

I'm currently working on a ArcObjects C# project that will repoint datasources for layers of various types. Basically a database is maintained that holds the old datasource connections and the new connections. When the user presses a button, the application goes through all the current layers in ArcDesktop and compares the layers current datasource with the new one. If there is a difference, it resets the datasource.

However, I'm not quite sure how to access the datasource, and in my perusings of this stackexchange, I noticed someone who was working on something similar, but they were using raster layers. I'm not too familiar with ArcGIS and didn't know if I needed to handle feature layers differently as he mentions he already found a solution for feature layers.

Here's the section I'm trying to change in ArcDesktop:enter image description here

I have everything working just fine except the changing of those datasources. I found this resource on arcgis, but it doesn't appear to change those strings, though I could be mistaken.

This is what I currently have:

//Loop through all layers and get their information
pEnumLayer.Reset();
pLayer = pEnumLayer.Next();
while (pLayer != null)
        {
            Log.Debug("Manipulating the layer: " + pLayer.Name);
            IDataLayer dataLayer = (IDataLayer)pLayer;
            var datasource = dataLayer.DataSourceName; // this is what I believe to be the current datasource

            Log.Debug("Repaired layer: " + pLayer.Name);
            pLayer = pEnumLayer.Next();
        }

Is that the correct datasource I'm looking for? If so, would all I need to do to change the datasource is to set that variable to the new datasource string and then redraw the map?

Best Answer

You should use IDataSourceHelperLayer Interface:

Code Snippet:

IDataSourceHelperLayer dsh = new DataSourceHelper() as IDataSourceHelperLayer;
dsh.ReplaceName(pLayer, newIName, false);

You can also use IDataSourceHelperLayer.FindAndReplaceWorkspaceNamePath method.

Related Question