I want to change data source information of a layer. Lets say I have a MXD file in dev env configured with dev environment datasource details like server,database,username,password and I want to use this MXD in test environment pointing to test datasource details. I have created a Add-In button. On click on this button, ArcMap Database Connection windows pops up asking database details to which layer should point in test environment. Once user add the details I capture this information in IWorkspace workspace:

//Code for popping up a window to ask user for datasource details

IPropertySet propertySet = new PropertySetClass();
propertySet.SetProperty("SERVERINSTANCE", "sde:sqlserver:devserver"); //sde:sqlserver:(servername)
propertySet.SetProperty("dbclient", "SQLServer");
propertySet.SetProperty("DATABASE", "databasename"); //(databasename)
Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory");
IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
IWorkspace targetWorkspace = workspaceFactory.Open(propertySet, 0);     

In below code, I am trying to access layer's workspace connection properties and then trying to set it. I dont know how to proceed further with the code.

//Code for trying to set layer datasource.

IDocument doc = ArcMap.Application.Document;
IMxDocument mxDoc = doc as IMxDocument;
IMap map = mxDoc.FocusMap;            
ILayer layer = map.get_Layer(0);// Layer[0]; 
ESRI.ArcGIS.Carto.IFeatureLayer featureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)layer;
ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureLayer.FeatureClass;
ESRI.ArcGIS.Geodatabase.IDataset dataSet = (ESRI.ArcGIS.Geodatabase.IDataset)featureClass;                  
ESRI.ArcGIS.esriSystem.IPropertySet propertySet = dataSet.Workspace.ConnectionProperties;

dataSet.Workspace.ConnectionProperties.SetProperty("Server", "testservername"); 

1 Answer 1


I managed to do it using the below code:

private void ChangeDataSource(IWorkspace targetWorkspace)
            IDocument doc = ArcMap.Application.Document;
            IMxDocument mxDoc = doc as IMxDocument;
            ESRI.ArcGIS.Carto.IMap map = mxDoc.FocusMap;
            ESRI.ArcGIS.Carto.ILayer layer = map.get_Layer(0);

            var dataLayer = (ESRI.ArcGIS.Carto.IDataLayer)layer;
            var datasetName = (IDatasetName)dataLayer.DataSourceName;
            var newWorkspaceName = (IWorkspaceName)((IDataset)targetWorkspace).FullName;

            datasetName.WorkspaceName = newWorkspaceName;            
            dataLayer.DataSourceName = (IName)datasetName; ;
            layer = (ESRI.ArcGIS.Carto.ILayer)dataLayer;            
  • It might be safer to call IDatasetName.Disconnect, then set DataSourceName, then calling IDatasetName.Connect. I think this will help avoid issues if the layer's attribute table is open in arcmap when the command is invoked. May 28, 2018 at 0:48
  • @KirkKuykendall, thank you for your suggestion. I will add it to the code.
    – jay
    May 28, 2018 at 1:56
  • Does this code suppose that the layer name in the database is the same in both environments?
    – O.D.
    Jun 22, 2018 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.