I am trying to write an addin that loads content (tables, featurelayers, relationships, ...) from an external mxd into the current ArcMap session. I do not want to actually load the mxd, because it would confuse users (they may be working on some mxd of their own, but after running my code it would point to my document).
Here is my code so far:
var reader = new MapReaderClass(); reader.Open(mxd.FullName); var contents = reader.Map as IMxdContents; // Test: write contents to external mxd, this works! var mapOut = new MapDocumentClass(); mapOut.Open(@"D:\temp\untitled.mxd"); mapOut.ReplaceContents(contents); mapOut.Save(); // Write contents to current document, this does not work! var thisMapDoc = ArcMap.Document as IMapDocument; thisMapDoc.Map.ReplaceContents(contents); reader.Close();
My problem is that copying the contents to another mxd works flawlessly, but copying it into the current document does not. Line 13 throws an exception, for some documents: Not implemented (Exception from HRESULT: 0x80004001 (E_NOTIMPL)), and for others The table does not have an OID Field. [NAME_OF_MY_TABLE] (One table does in fact have no OID field, but why that causes an error is beyond me).
I tried in both 10.1 and 10.3.1 with the same results.