Style feature layers with an add-in

This tutorial demonstrates how to build an add-in that can assign a renderer to your feature layer to change feature symbology.

With the ArcGIS Pro SDK for .NET, you can extend ArcGIS Pro with your own unique tools and workflows. Using Microsoft Visual Studio and the Pro SDK, developers can build Pro add-ins and solution configurations that provide users with custom functionality, such as the ability to interact with feature layers and their symbology, specific to their organization or industry. You can update layer symbology based upon an existing system style or a custom style that is loaded within your project.

In this tutorial you will create an add-in with two buttons that update the symbology of a point feature layer with either a new symbol or graduated colors.

Prerequisites

Steps

Create a new ArcGIS Pro Add-In solution

  1. Start Visual Studio.

  2. Choose File > New > Project and then from the ArcGIS templates group, select ArcGIS Pro Module Add-in. Name the add-in project "LayerSymbology". Click OK to proceed.

Create new Pro buttons and update DAML

  1. Right-click the project and choose Add > New Item and then from the ArcGIS Pro Add-ins group select ArcGIS Pro Button from the list of item templates.

  2. When prompted, name the new class file SingleSymbol.cs and click Add to close the dialog box.

  3. Repeat these steps to add a second button, naming this class GraduatedColor.cs.

  4. Add the following code to the OnClick method of SingleSymbol.cs:

    Use dark colors for code blocksCopy
    1
    Module1.ApplySymbol("single symbol");
  5. Add the following code to the OnClick method of GraduatedColor.cs:

    Use dark colors for code blocksCopy
    1
    Module1.ApplySymbol("graduated color");
  6. Update the Config.daml file AddInInfo section so that the DAML appears as follows. This updates the captions for each of the buttons, and the caption of the control group, which will appear on the Add-In tab.

    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    </AddInInfo>
    <!-- *** Add *** -->
    <modules>
        <insertModule id="LayerSymbology_Module" className="Module1" autoLoad="false" caption="Module1">
        <groups>
            <group id="LayerSymbology_Group1" caption="Point Layer Symbols" appearsOnAddInTab="true">
            <!-- host controls within groups -->
            <button refID="LayerSymbology_SingleSymbol" size="large" />
            <button refID="LayerSymbology_GraduatedColor" size="large" />
            </group>
        </groups>
        <controls>
            <button id="LayerSymbology_SingleSymbol" caption="Apply Single Symbol" className="SingleSymbol" loadOnClick="true" smallImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonBlue16.png" largeImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonBlue32.png">
            <tooltip heading="Tooltip Heading">Tooltip text<disabledText /></tooltip>
            </button>
            <button id="LayerSymbology_GraduatedColor" caption="Apply Graduated Color" className="GraduatedColor" loadOnClick="true" smallImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonBlue16.png" largeImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonBlue32.png">
            <tooltip heading="Tooltip Heading">Tooltip text<disabledText /></tooltip>
            </button>
        </controls>
        </insertModule>
    </modules>

Create a new method in the module file

  1. Copy the following code to create a new ApplySymbol method, and paste it into the Module1.cs file below the Overrides region.

    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
        #region Business Logic
    
        public static void ApplySymbol(string symbolChoice)
        {
            QueuedTask.Run(() =>
            {
                // Check for an active 2D mapview, if not, then prompt and exit.
                if (MapView.Active == null || (MapView.Active.ViewingMode != ArcGIS.Core.CIM.MapViewingMode.Map))
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("An active 2D MapView is required to use this tool. Exiting...", "Info");
                    return;
                }
                // Get the layer(s) selected in the Contents pane, if there is not just one, then prompt then exit.
                if (MapView.Active.GetSelectedLayers().Count != 1)
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("One feature layer must be selected in the Contents pane. Exiting...", "Info");
                    return;
                }
                // Check to see if the selected layer is a feature layer, if not, then prompt and exit.
                var featLayer = MapView.Active.GetSelectedLayers().First() as FeatureLayer;
                if (featLayer == null)
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("A feature layer must be selected in the Contents pane. Exiting...", "Info");
                    return;
                }
                // Check if the feature layer shape type is point, if not, then prompt and exit.
                else if (featLayer.ShapeType != esriGeometryType.esriGeometryPoint)
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Selected feature layer must be shape type POINT. Exiting...", "Info");
                    return;
                }
                try
                {
                    // If symbolChoice is a single symbol
                    if (symbolChoice == "single symbol")
                    {
                        // Get all styles in the project
                        var styles = Project.Current.GetItems<StyleProjectItem>();
    
                        // Get a specific style in the project
                        StyleProjectItem style = styles.First(s => s.Name == "ArcGIS 2D");
    
                        // Get the Push Pin 1 symbol
                        var pt_ssi = style.SearchSymbols(StyleItemType.PointSymbol, "Push Pin 1").FirstOrDefault();
    
                        // Create a new renderer definition and reference the symbol
                        SimpleRendererDefinition srDef = new SimpleRendererDefinition();
                        srDef.SymbolTemplate = pt_ssi.Symbol.MakeSymbolReference();
    
                        // Create the renderer and apply the definition
                        CIMSimpleRenderer ssRenderer = (CIMSimpleRenderer)featLayer.CreateRenderer(srDef);
    
                        // Update the feature layer renderer
                        featLayer.SetRenderer(ssRenderer);
                    }
                    else if (symbolChoice == "graduated color")
                    {
                        // Get a style and color ramp to apply to the renderer
                        StyleProjectItem style = Project.Current.GetItems<StyleProjectItem>().First(s => s.Name == "ColorBrewer Schemes (RGB)");
                        IList<ColorRampStyleItem> colorRampList = style.SearchColorRamps("Greens (Continuous)");
                        ColorRampStyleItem aColorRamp = colorRampList[0];
    
                        // Create a graduated color renderer definition with 3 breaks, a populated numeric field named "Symbol" is required
                        GraduatedColorsRendererDefinition gcDef = new GraduatedColorsRendererDefinition()
                        {
                            ClassificationField = "Symbol",
                            ClassificationMethod = ArcGIS.Core.CIM.ClassificationMethod.EqualInterval,
                            BreakCount = 4,
                            ColorRamp = aColorRamp.ColorRamp,
                            SymbolTemplate = SymbolFactory.Instance.ConstructPointSymbol().MakeSymbolReference(),
                        };
    
                        // Create the renderer and apply the definition
                        CIMClassBreaksRenderer cbRndr = (CIMClassBreaksRenderer)featLayer.CreateRenderer(gcDef);
    
                        // Update the feature layer renderer
                        featLayer.SetRenderer(cbRndr);
                    }
                }
                catch (Exception exc)
                {
                    // Catch any exception found and display in a message box
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Exception caught: " + exc.Message);
                    return;
                }
            });
        }
    
        #endregion

Update the dataset and test the add-in

To update layer symbology to graduated colors requires a numeric attribute named Symbol on which to apply an equal interval classification. One method is to add a new field to the previously downloaded ArcGIS Pro SDK community sample dataset, FeatureTest, which is typically located in C:\Data\FeatureTest\.

The following steps assume that you are using this dataset for testing.

  1. Build your project and resolve any issues.

  2. Click Start Debugging to run your project, which will launch ArcGIS Pro. Open the FeatureTest.aprx project.

  3. In the map view, find the layer named TestPoints. Add a new integer attribute named Symbol to this layer and then calculate its values to match the values in the ObjectID attribute.

    You can use the following Geoprocessing tools to add a new field and calculate values:

    • <https://pro.arcgis.com/en/pro-app/tool-reference/data-management/add-field.htm>
    • <https://pro.arcgis.com/en/pro-app/tool-reference/data-management/calculate-field.htm>
  4. Once the new Symbol attribute is available and populated, from the Add-In tab find your new control group with your two new buttons. Test both of your buttons with the TestPoints point feature layer to see it rendered with either a new single symbol or graduated colors.

What's Next?

Learn how to use additional ArcGIS Pro SDK for .NET features in these tutorials:

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.