Select to view content in your preferred language

Pro SDK Create Legend by Layers

06-26-2018 08:12 AM
MVP Alum

Is there a way to add or subtract Layers to a Legend using the Pro SDK?  I see you can create a legend for a specific map frame, but I cannot figure out how to customize the legend once it is created.  

Tags (3)
0 Kudos
1 Reply
New Contributor III

This is not elegant, but it seems to work. You can use the LayoutElementFactory to create the legend. Then loop through all of the CIMLegendItem(s) in the LegendDefinition. Make an array out of the ones that you want to keep. You can identify them by name. Then set your new array as the CIMLegendItem(s) in the Definition and reset the Definition. This is definitely not as intuitive as it was in ArcObjects:

Legend legendElm = LayoutElementFactory.Instance.CreateLegend(layout, leg_env, mapFrame);

CIMLegend cimLeg = legendElm.GetDefinition() as CIMLegend;
CIMLegendItem[] myLegendItems = new CIMLegendItem[1];
foreach (CIMLegendItem legItem in cimLeg.Items)
   if (legItem.Name.Equals(Constants.MAPS_ELEV_ZONE))

      myLegendItems[0] = legItem;
if (myLegendItems[0] != null)
   cimLeg.Items = myLegendItems;

0 Kudos