Layer file's labelling properties not importing

539
3
Jump to solution
02-11-2022 11:18 AM
KisakyeM
New Contributor III

I am importing symbology from a layer as below. After updating the symbology, I update the label expression  to use the label field in the current data. Once I turn on the labels however, the labels now have default properties; the label design from the imported layer is lost.  Font size and style are off and the halo is removed. How can I maintain the layer file's labelling properties with my new data?

 await QueuedTask.Run(() =>
            {
                var lyrDoc = new LayerDocument(LayerFilePath);
                var cimLyrDoc = lyrDoc.GetCIMLayerDocument();
                var rendFromLyrFile = ((CIMFeatureLayer)cimLyrDoc.LayerDefinitions[0]).Renderer as CIMUniqueValueRenderer;
                rendFromLyrFile.Fields= new string[] { "SiteDesc" };
                facLayer.SetRenderer(rendFromLyrFile);
            });
            //update the labels
            await QueuedTask.Run(() =>
            {    
                var def = facLayer.GetDefinition() as CIMFeatureLayer;
                var listLabelClasses = def.LabelClasses.ToList();
                var theLabelClass = listLabelClasses.FirstOrDefault();
                theLabelClass.Expression = $"$feature.{divFieldnameFac} + ' ' + $feature.{divNameFieldnameFac}";//set the label class Expression to use the Arcade expression
                facLayer.SetDefinition(def);
                facLayer.SetLabelVisibility(true);
            });

 @CharlesMacleod @UmaHarano @Wolf 

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
CharlesMacleod
Esri Regular Contributor

i believe there is a LabelClasses collection property on the layer definition. I think all u need to do is set that. A label class also has a text symbol property I believe - so I think you could just set _that_ on the default classes if, similar to the issue with the renderer, the classes in the layer document are not equivalent to the data in the layer.

View solution in original post

0 Kudos
3 Replies
CharlesMacleod
Esri Regular Contributor

in your code snippet it looks like you set the labelling expression on the (default) label classes from "facLayer" without having set the label classes (imported from the CIMLayerDocument)....but maybe that's just in the code snippet and not in the real addin. Can you verify pls?

0 Kudos
KisakyeM
New Contributor III

You are correct in your assumption. The code I posted is exactly what I am using in my application. How do I go about doing what you're suggesting?

0 Kudos
CharlesMacleod
Esri Regular Contributor

i believe there is a LabelClasses collection property on the layer definition. I think all u need to do is set that. A label class also has a text symbol property I believe - so I think you could just set _that_ on the default classes if, similar to the issue with the renderer, the classes in the layer document are not equivalent to the data in the layer.

0 Kudos