Select to view content in your preferred language

Retrieving programatically added Project Items (Layouts)

12-05-2019 08:07 AM
New Contributor III

I'm developing an add-in that needs to programatically import a Layout from a PAGX file into a Project. The import itself is working fine, but I need to use the Layout object immediately after it is added. This becomes a problem when there are name collisions, given that Arc Pro automatically appends a number when a Layout with an existing name is added. I do have the unmodified name in memory (stored in a custom object), so when duplicate names are NOT an issue, I can "search" for the new Layout with the call

Layout myNewLayout = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name == knownLayoutName).GetLayout();

When there is a name collision, however, this call just returns the Layout with the given name that existed pre-import (since the new Layout is now named something like "myNewLayout1").

Thus far, the only way I have found to get the correct LayoutProjectItem is to first find the latest SourceModifiedTime of all of the Project's LayoutProjectItems, and then filter on item.SourceModifiedTime == maxModifiedTime. This seems to be working, but I don't actually know why...when I step through the code with the debugger, it seems that the SourceModifiedTime attribute for every LayoutProjectItem is just some default value.

Part of the issue is that the actual import has to be done with ItemFactory.Instance.Create(), which returns an IProjectItem value. That value cannot be cast to a LayoutProjectItem, and from what I gather, the LayoutProjectItem doesn't even exist until Project.Current.AddItem() is called. The originally returned IProjectItem and the corresponding LayoutProjectItem don't have the same CacheID property, either, and other than SourceModifiedTime, I don't see any other distinguishing attributes.

So, once I make that call to AddItem(), is there a better way to retrieve exactly the Layout (or LayoutProjectItem) I just added? The fact that a Layout's unique identifier is its name seems to be the root of the issue here.



Tags (3)
0 Kudos
1 Reply
by Esri Regular Contributor
Esri Regular Contributor

You could listen to the Project Items changed event:

                ProjectItemsChangedEvent.Subscribe((args) =>
                    System.Diagnostics.Debug.WriteLine($@"{args.Action.ToString()} {args.ProjectItem.Title}");
                }, false);

the 'Action' is 'Add' and 'args.ProjectItem' should be your newly added LayoutProjectItem.

0 Kudos