I'm writing a C# addin to change all the data sources in an MXD from one SDE datasource to another. I can iterate through the layers and change feature layers and raster layers, but what about tables?
There is very little doc on this - it can be done in arcpy, but I could only find one old unanswered question for how to do it in arcobjects.
I've used the TableCollection to iterate through the tables in the map, but it's not clear how to update the source for each one. Any ideas?
Solved! Go to Solution.
Anyone? Never mind, I've figured it out:
// focus map IMap map = ... // new data source for table IFeatureWorkspace workspace = ... ITableCollection tableCollection = (ITableCollection)map; List<ITable> tables = new List<ITable>(); for (int i = 0; i < tableCount; i++) { tables.Add(tableCollection.get_Table(i)); } // iterate over a copy of the tables so we can add and remove foreach (ITable oldTable in tables) { IDataset dataset = (IDataset)oldTable; IDatasetName datasetName = (IDatasetName)dataset.FullName; // assumes table name is same in new data source // get the new table ITable newTable = workspace.OpenTable(datasetName); // replace the table in the TOC ITableCollection tableCollection = (ITableCollection)map; tableCollection.RemoveTable(oldTable); tableCollection.AddTable(newTable); // fire change event to change table IMapAdmin2 mapAdmin2 = (IMapAdmin2)map; mapAdmin2.FireChangeTable(oldTable, newTable); } // Redraw the map IActiveView activeView = (IActiveView)map; activeView.Refresh(); // Refresh TOC ESRI.ArcGIS.ArcMapUI.IContentsView contentsView = ArcMap.Document.CurrentContentsView; contentsView.Refresh(null);
Anyone? Never mind, I've figured it out:
// focus map IMap map = ... // new data source for table IFeatureWorkspace workspace = ... ITableCollection tableCollection = (ITableCollection)map; List<ITable> tables = new List<ITable>(); for (int i = 0; i < tableCount; i++) { tables.Add(tableCollection.get_Table(i)); } // iterate over a copy of the tables so we can add and remove foreach (ITable oldTable in tables) { IDataset dataset = (IDataset)oldTable; IDatasetName datasetName = (IDatasetName)dataset.FullName; // assumes table name is same in new data source // get the new table ITable newTable = workspace.OpenTable(datasetName); // replace the table in the TOC ITableCollection tableCollection = (ITableCollection)map; tableCollection.RemoveTable(oldTable); tableCollection.AddTable(newTable); // fire change event to change table IMapAdmin2 mapAdmin2 = (IMapAdmin2)map; mapAdmin2.FireChangeTable(oldTable, newTable); } // Redraw the map IActiveView activeView = (IActiveView)map; activeView.Refresh(); // Refresh TOC ESRI.ArcGIS.ArcMapUI.IContentsView contentsView = ArcMap.Document.CurrentContentsView; contentsView.Refresh(null);
Hi Jon,
Thanks for posting this. Could you also post the code you used for changing the datasource for featurelayers and raster??
Thanks,
There's a snippet here that can change feature layer data source. The key thing is to call FireChangeFeatureClass after you've made the change to make sure the map is updated.
// Change FeatureClass of layer featureLayer.FeatureClass = newFeatureClass; mapAdmin2.FireChangeFeatureClass(oldFeatureClass, newFeatureClass);