Remove a join programmatically with C#

Discussion created by dpalmetz on Jul 26, 2011
Due to a bug in arcpy (removing a join between an SDE-featureclass and an ODBC-table fails) I was forced to make an add-in with C# in order to remove the join for many layers in a Mapdocument. After hourses of searching for a solution and messing around with RelationShipClass, I found a usefull example in the VBA-documentation (http://help.arcgis.com/en/sdk/10.0/vba_desktop/conceptualhelp/index.html#/How_to_remove_the_last_join_from_a_layer_or_table_in_ArcMap/00010000007s000000/). So, if someone is having the same problem, here is the solution:
ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument = ArcMap.Application.Document as ESRI.ArcGIS.ArcMapUI.IMxDocument; // Dynamic Cast
ESRI.ArcGIS.Carto.IActiveView actView = mxDocument.ActiveView;
IMap focMap = actView.FocusMap;

for (int i = 0; i < focMap.LayerCount; i++)
                IFeatureLayer2 actFeatLyr = focMap.get_Layer(i) as IFeatureLayer2;
                ITable actLyr = focMap.get_Layer(i) as ITable;
                IDisplayTable actDispTable = actFeatLyr as IDisplayTable;
                IRelQueryTable actQueryTable = actDispTable.DisplayTable as IRelQueryTable;
                IDisplayRelationshipClass actDispRelClass = actFeatLyr as IDisplayRelationshipClass;
                actDispRelClass.DisplayRelationshipClass(null, esriJoinType.esriLeftInnerJoin);
//Refreshing the view

//Refreshing the TOC
ESRI.ArcGIS.ArcMapUI.IContentsView contentsView = mxDocument.CurrentContentsView;