I'm in trouble on trying to programmatically change connection properties for all layers on a map. I can do manually, but not by coding.
All layers are based on Oracle views not registered into SDE. All these Oracle views have not a defined key.
My approach is to get new feature class from a workspace based on my new connection and set this resulting feature class to original feature layer. The problem is that I loose definition for Unique Identifier Field (or OID): after setting new feature class, hasOID = false. But if I do manually, all works properly:
1) I press "Change Query",
2) I choose Oracle view from my new sde connection
3) ArcMap show me window for choosing Unique Identifier Field: I set it
..and all is ok.
Here you are my code, the part in bold is where I try to set OID: it gives no errors, but if I try to open data window I get an error.
So, my question is: how can I define OID field name to a feature class got from IFeatureWorkspace:OpenFeatureClass?
foreach(IFeatureLayer featureLayer in featureLayers)
//check connection properties
IDataset dataSet = (IDataset)featureLayer;
IDatabaseConnectionInfo4 databaseConnectionInfo = (IDatabaseConnectionInfo4)dataSet.Workspace;
string connectedUser = databaseConnectionInfo.ConnectedUser;
Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory");
IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(@"C:\test\mySdeFile.sde", 0);
//set new feature class by opening on new workspace
IFeatureClass oldFestureClass = featureLayer.FeatureClass;
featureLayer.FeatureClass = featureWorkspace.OpenFeatureClass(featureLayer.FeatureClass.AliasName);
//set old spatial reference to new feature class
IGeoDatasetSchemaEdit geoDatasetSchemaEdit = (IGeoDatasetSchemaEdit)featureLayer.FeatureClass;
//set old OID to new feature class
int idx = featureLayer.FeatureClass.Fields.FindFieldByAliasName(oldOIDFieldName);
IFieldEdit field = (IFieldEdit)featureLayer.FeatureClass.Fields.Field[idx];
field.Type_2 = esriFieldType.esriFieldTypeOID;
Anybody has any idea on what I can do to set OID field?..