Select to view content in your preferred language

Add lyr layer via code in ArcGIS Pro 3

913
5
12-05-2022 12:09 PM
JonathanDewalt
Occasional Contributor

Is it possible to add a layer file (.lyr) to the active map using code in Pro 3?  My Pro 2 code does not work in 3.

0 Kudos
5 Replies
KenBuja
MVP Esteemed Contributor

Did you see the Map, Layout, and Layer Files and Packages section of the 3.0 Migration Guide?

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

This will work for lyrx files.   Lyr file are from ArcGIS Desktop so i am not sure if 3.0 will still load those.

protected override async void OnClick()
{
  string url = @"c:\temp\Acme Service Roads.lpkx";  // load layer file

  Uri uri = new Uri(url);
  await QueuedTask.Run(() => LayerFactory.Instance.CreateLayer(uri, MapView.Active.Map));
}
0 Kudos
DanNarsavage_IDWR
Regular Contributor

To be sure, Pro 3.x will do the appropriate thing with a .LYR file that I drag onto a map. But the Pro SDK steps for adding a LYRX file do not support LYR files (throws `System.Runtime.InteropServices.COMException (0x80004005): Could not determine encoding`).  We're trying to transition our users from an ArcMap add-in to a Pro add-in, and we are hoping to skip the step of converting all those LYRs to LYRXs until after the transition is complete (so we can skip supporting twice the amount of layer files during the transition). I'll reply to my own post here if I find a way to do this, but I'd appreciate any guidance anyone might have.

0 Kudos
JonathanDewalt
Occasional Contributor

Are you still looking how to load lyr files? I have a custom layer manager add-in I could send your way to try out.  If it does what you want, I can pull out the code for you.

0 Kudos
DanNarsavage_IDWR
Regular Contributor

Yes please!!  I'll send you a direct message . . .

0 Kudos