I captured (stopped) a Layer (featureclass) from being deleted from the Table of contents of arcmap. When i re-add it back, the datasource is broken.

(See question :Using ArcObjects to stop items from being deleted from Table of Contents?)

I'm wondering what I need to cast my ILayer object to, in order to just activate the datasource?

The workspace is sde, but i find it odd that if the whole object(layer) is available when caught by the itemdeleted handlers, why is the datasource off?

I'm having trouble finding the right interface to use.

Here is the code that I have implemented: It readds the layer back to the ToC but the connection is not connected. (Exten is the Addin.extension object for the addin.)

 public void On_TOCItemDeleted(object Item)
            IWorkspaceName workspace;
            var msgText = "You tried to delete an item from the TOC.";

        // check that the item is of type ILayer (lyr file, raster, etc) before doing anything.
        var DeletedLayer = Item as ILayer;

        if (DeletedLayer == null)
            throw new Exception("Not iLayer file");

        IDataLayer2 src = DeletedLayer as IDataLayer2;
        var groupname = FindParentGroup(DeletedLayer.Name);

        if (src.DataSourceName != null)
            workspace = (src.DataSourceName as IDatasetName).WorkspaceName;
            switch (groupname)
                case BLayerName: // basemaps etc
                    workspace = DeletedLayer.SetLayerSDEConnection(Exten.Analysis_WIP);

                default: //analysis connection
                    workspace = DeletedLayer.SetAnalysisSDEConnection(Exten.Analysis_WIP);
        src.Connect((workspace as IName));
        msgText = string.Format("You tried to delete the layer '{0}'. This operation was stopped.", DeletedLayer.Name);
        //Read the item

        var grouplyr = GetGroupByName(ArcMap.Document.FocusMap.Layers, groupname);

        grouplyr.Add(src as ILayer);


        Exten.InformUser("App", msgText);
  • 1
    can you show some code? What is the workspace type? Jan 7, 2016 at 13:38
  • 1
    Cast the layer to IDatalayer2. Maybe the DataSourceName is still populated, and you can just re-connect it. Typically this points to an IFeatureclassName for a featurelayer. Jan 7, 2016 at 18:15

1 Answer 1


So, I found out that the COM object itelf has already be disposed of; only the metadata is available. Due to this, any attempt to re connect the IPropertySet causes a E_FAIL.

To avoid writing too much 'dumb' code, I have changed my approach to just have internal metadata look up, to find the Lyr file location on the hard disk. Once found from a OpenFileDialog(), I then re-added the layer to the existing Group Layer, if applicable.

The method is able to see if the 'source' to be added is of type IFeatureLayer or IRasterLayer and adds to the map using arcobjects.

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.