[GIS] (arcengine) export selected features from sde to shapefile (with joined fields)

arcgis-enginearcobjects

I have a SDE featureclass that is joined to an access table in my map control. I want to export selected features of the SDE layer to a shapefile AND retain the joined fields.
this following method works great (except for retaining the joined fields)

public void ExportMapLyrToShapefile(ESRI.ArcGIS.Carto.ILayer pInLayer, string inputName, string outputPath, string qry)
    {
        // Set the GeoFeatureLayer to the layer in the map so that we can use it as the input feature later
        IGeoFeatureLayer pInGFLayer = (IGeoFeatureLayer)pInLayer;
        IFeatureLayer pInFeatureLayer = (IFeatureLayer)pInGFLayer;
        IFeatureClass inputFeatureClass = pInFeatureLayer.FeatureClass;
        IDataset inputDataset = (IDataset)inputFeatureClass;
        IDatasetName inputDatasetName = (IDatasetName)inputDataset.FullName;

        // Get the layer's selection set (the selected/highlighted features of the layer in the mapcontrol)
        IFeatureSelection featureSelection = (IFeatureSelection)pInFeatureLayer;
        ISelectionSet selectionSet = featureSelection.SelectionSet;

        // Create a feature class name for the output shapefile and open the output's workspace
        IWorkspaceFactory shpWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
        IWorkspace shpWorkspace = shpWorkspaceFactory.OpenFromFile(outputPath, 0);
        IDataset shpWorkspaceDataset = (IDataset)shpWorkspace;
        IWorkspaceName workspaceName = (IWorkspaceName)shpWorkspaceDataset.FullName;
        IFeatureClassName shpFeatureClassName = new FeatureClassNameClass();
        IDatasetName shpDatasetName = (IDatasetName)shpFeatureClassName;
        shpDatasetName.WorkspaceName = workspaceName;
        shpDatasetName.Name = qry.Replace(" = ", "");

        // Use the IFieldChecker interface to make sure all of the field names are valid for the output shapefile.
        IFieldChecker fieldChecker = new FieldCheckerClass();
        IFields shapefileFields = null;
        IEnumFieldError enumFieldError = null;
        fieldChecker.InputWorkspace = inputDataset.Workspace;
        fieldChecker.ValidateWorkspace = shpWorkspace;
        fieldChecker.Validate(inputFeatureClass.Fields, out enumFieldError, out shapefileFields);

        // We also need to retrieve the GeometryDef from the input feature class.
        int shapeFieldPosition = inputFeatureClass.FindField(inputFeatureClass.ShapeFieldName);
        IFields inputFields = inputFeatureClass.Fields;
        IField shapeField = inputFields.get_Field(shapeFieldPosition);
        IGeometryDef geometryDef = shapeField.GeometryDef;

        // Now we create a feature data converter to "Export" the selected features to a shapefile
        IFeatureDataConverter2 featureDataConverter2 = new FeatureDataConverterClass();
        IEnumInvalidObject enumInvalidObject = featureDataConverter2.ConvertFeatureClass(inputDatasetName, null, selectionSet, null, shpFeatureClassName, geometryDef, shapefileFields, "", 1000, 0);
    }

the issue is with the IFieldChecker interface. it validates all the fields and removed the joined fields before exporting to a shapefile.

Does anyone have any insight on this issue? If so, please help! I'm stumpped!

btw… i also tried using an IDisplayTable as my input feature and I get the same output (missing joined fields)

Thanks,
Derek

Best Answer

I'm not familiar with IFieldChecker, but I typically use the code below to export a joined featureclass to a shapefile and it works fine for me (does use IDisplayTable).

 private void Export2Shapefile(IFeatureLayer pBlockLayer)
    {
        IDisplayTable pBlkTbl = pBlockLayer as IDisplayTable;
        IFeatureClass pBlkClass = pBlkTbl.DisplayTable as IFeatureClass;

        IDataset pDataset = pBlkClass as IDataset;
        IDatasetName pInDSname = pDataset.FullName as IDatasetName;
        IWorkspaceFactory pOutWSFact = new ShapefileWorkspaceFactoryClass();
        IWorkspace pOutWS = pOutWSFact.OpenFromFile(ScratchPath, 0);

        IDataset pDData = pOutWS as IDataset;
        IWorkspaceName pWSname = pDData.FullName as IWorkspaceName;
        IDatasetName pOutDSname = new FeatureClassNameClass();
        pOutDSname.Name = "gridshape";
        pOutDSname.WorkspaceName = pWSname;

        IExportOperation pExpOp = new ExportOperationClass();
        pExpOp.ExportFeatureClass(pInDSname, null, null, null, pOutDSname as IFeatureClassName, 0);

    }