1

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

1 Answer 1

1

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

    }
3
  • 1
    the above code that you used is for arcmap. i'm writing a standalone arcengine application and it doesn't support the IExportOperation interface. Any other ideas?
    – Derek
    Commented Dec 7, 2011 at 21:10
  • thanks for the code sample... i figured out what was wrong with my code... it was how i was casting the input displaytable that was messing things up...
    – Derek
    Commented Dec 8, 2011 at 16:54
  • Great! works perfectly.
    – EvilInside
    Commented Apr 1, 2015 at 20:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.