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");