1

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"); 
Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked May 25, 2018 at 22:43
1

1 Answer 1

2

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; 
 }
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered May 27, 2018 at 22:43
3
  • 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. Commented May 28, 2018 at 0:48
  • @KirkKuykendall, thank you for your suggestion. I will add it to the code. Commented May 28, 2018 at 1:56
  • Does this code suppose that the layer name in the database is the same in both environments? Commented Jun 22, 2018 at 8:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.