ArcGIS Pro 3.7 API Reference Guide
ArcGIS.Core.CIM Namespace / CIMExpressionInfo Class / Expression Property
Example

In This Topic
    Expression Property (CIMExpressionInfo)
    In This Topic
    Gets or sets the Arcade expression used to evaluate and return the value that a property expects.
    Syntax
    Public Property Expression As String
    public string Expression {get; set;}
    Example
    Basic Query
    {
      //Consult https://github.com/Esri/arcade-expressions/ and
      //https://developers.arcgis.com/arcade/ for more examples
      //and arcade reference
      // Note: the following should be embedded in a QueuedTask.Run() statement
      {
        //construct an expression
        var query = @"Count($layer)";//count of features in "layer"
    
        //construct a CIMExpressionInfo
        var arcade_expr = new CIMExpressionInfo()
        {
          Expression = query.ToString(),
          //Return type can be string, numeric, or default
          //When set to default, add-in is responsible for determining
          //the return type
          ReturnType = ExpressionReturnType.Default
        };
    
        //Construct an evaluator
        //select the relevant profile - it must support Pro and it must
        //contain any profile variables you are using in your expression.
        //Consult: https://developers.arcgis.com/arcade/profiles/
        using var arcade = ArcadeScriptEngine.Instance.CreateEvaluator(
                                arcade_expr, ArcadeProfile.Popups);
        //Provision  values for any profile variables referenced...
        //in our case '$layer'
        var variables = new List<KeyValuePair<string, object>>() {
            new("$layer", featureLayer)
          };
        //evaluate the expression
        try
        {
          var result = arcade.Evaluate(variables).GetResult();
          System.Diagnostics.Debug.WriteLine($"Result: {result}");
        }
        //handle any exceptions
        catch (InvalidProfileVariableException)
        {
          //something wrong with the profile variable specified
          //TODO...
        }
        catch (EvaluationException)
        {
          //something wrong with the query evaluation
          //TODO...
        }
      }
    }
    Basic Query using Features
    {
      //Consult https://github.com/Esri/arcade-expressions/ and
      //https://developers.arcgis.com/arcade/ for more examples
      //and arcade reference
    
      // Note: the following should be embedded in a QueuedTask.Run() statement
      {
        //construct an expression
        var query = @"$feature.AreaInAcres * 43560.0";//convert acres to ft 2
    
        //construct a CIMExpressionInfo
        var arcade_expr = new CIMExpressionInfo()
        {
          Expression = query.ToString(),
          //Return type can be string, numeric, or default
          //When set to default, add-in is responsible for determining
          //the return type
          ReturnType = ExpressionReturnType.Default
        };
    
        //Construct an evaluator
        //select the relevant profile - it must support Pro and it must
        //contain any profile variables you are using in your expression.
        //Consult: https://developers.arcgis.com/arcade/profiles/
        using var arcade = ArcadeScriptEngine.Instance.CreateEvaluator(
                                arcade_expr, ArcadeProfile.Popups);
        //we are evaluating the expression against all features
        using var rc = featureLayer.Search();
    
        while (rc.MoveNext())
        {
          //Provision  values for any profile variables referenced...
          //in our case '$feature'
          var variables = new List<KeyValuePair<string, object>>() {
              new("$feature", rc.Current)
            };
          //evaluate the expression (per feature in this case)
          try
          {
            var result = arcade.Evaluate(variables).GetResult();
            var val = ((double)result).ToString("0.0#");
            System.Diagnostics.Debug.WriteLine(
              $"{rc.Current.GetObjectID()} area: {val} ft2");
          }
          //handle any exceptions
          catch (InvalidProfileVariableException)
          {
            //something wrong with the profile variable specified
            //TODO...
          }
          catch (EvaluationException)
          {
            //something wrong with the query evaluation
            //TODO...
          }
        }
      }
    }
    Modify renderer using Arcade
    {
      // Note: the following should be embedded in a QueuedTask.Run() statement
      {
        // GetRenderer from Layer (assumes it is a unique value renderer)
        if (featureLayer.GetRenderer() is not CIMUniqueValueRenderer uvRenderer)
        {
          // not a unique value renderer, leave
          return;
        }
        //layer has STATE_NAME field if using the community sample Data\Admin\AdminSample.aprx
        string expression = "if ($view.scale > 21000000) { return $feature.STATE_NAME } else { return 'All' }";
        CIMExpressionInfo updatedExpressionInfo = new()
        {
          Expression = expression,
          Title = "Custom" // can be any string used for UI purpose.
        };
        //set the renderer's expression
        uvRenderer.ValueExpressionInfo = updatedExpressionInfo;
    
        //SetRenderer on Layer
        featureLayer.SetRenderer(uvRenderer);
      }
    }
    Requirements

    Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)

    ArcGIS Pro version: 3.0 or higher.
    See Also