ArcGIS Pro 3.7 API Reference Guide
ArcGIS.Core.Data Namespace / AttributeRuleDefinition Class / GetScriptExpression Method
Example

In This Topic
    GetScriptExpression Method
    In This Topic
    Gets the Arcade expression that defines the rule. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    Public Function GetScriptExpression() As String
    public string GetScriptExpression()

    Return Value

    The Arcade expression.
    Exceptions
    ExceptionDescription
    This method or property must be called within the lambda passed to QueuedTask.Run
    A geodatabase-related exception has occurred.
    Example
    Evaluate AttributeRule Expression
    {
      //Consult https://github.com/Esri/arcade-expressions/ and
      //https://developers.arcgis.com/arcade/profiles/attribute-rules/ for
      //more examples and arcade reference
    
      // Note: the following should be embedded in a QueuedTask.Run() statement
      {
        //Retrieve the desired feature class/table
        FeatureClassDefinition def = featureLayer.GetFeatureClass().GetDefinition();
    
        //get the desired attribute rule whose expression is to be evaluated.
        //AttributeRuleType.All, Calculation, Constraint, Validation
        AttributeRuleDefinition validation_rule = def.GetAttributeRules(
                                                  AttributeRuleType.Validation).FirstOrDefault();
        if (validation_rule == null)
          return;
    
        //Get the expression
        var expr = validation_rule.GetScriptExpression();
        //construct a CIMExpressionInfo
        var arcade_expr = new CIMExpressionInfo()
        {
          Expression = expr,
          //Return type can be string, numeric, or default
          ReturnType = ExpressionReturnType.Default
        };
    
        System.Diagnostics.Debug.WriteLine($"Evaluating {expr}:");
        //Construct an evaluator
        //we are using ArcadeProfile.AttributeRules profile...
        //Consult: https://developers.arcgis.com/arcade/profiles/
        using var arcade = ArcadeScriptEngine.Instance.CreateEvaluator(
                                arcade_expr, ArcadeProfile.AttributeRuleValidation);
        //we are evaluating the expression against all features
        using var rc = featureLayer.Search();
    
        while (rc.MoveNext())
        {
          // Provision  values for any profile variables referenced...
          // we assume '$feature' here
          //...use arcade.ProfileVariablesUsed() if necessary...
          var variables = new List<KeyValuePair<string, object>>() {
              new("$feature", rc.Current)
            };
    
          //evaluate the expression per feature
          try
          {
            var result = arcade.Evaluate(variables).GetResult();
            //'Validation' attribute rules return true or false...
            var valid = bool.Parse(result.ToString());
            System.Diagnostics.Debug.WriteLine(
              $"{rc.Current.GetObjectID()} valid: {valid}");
          }
          //handle any exceptions
          catch (InvalidProfileVariableException)
          {
            //something wrong with the profile variable specified
            //TODO...
          }
          catch (EvaluationException)
          {
            //something wrong with the query evaluation
            //TODO...
          }
        }
      }
    }
    Get attribute rules of a dataset
    {
      // Must be called within QueuedTask.Run
      void GetAttributeRules(Geodatabase geodatabase, string tableName)
      {
        using (TableDefinition tableDefinition = geodatabase.GetDefinition<TableDefinition>(tableName))
        {
          // Get all attribute rule types
          IReadOnlyList<AttributeRuleDefinition> ruleDefinitions = tableDefinition.GetAttributeRules();
    
          // Iterate rule definitions
          foreach (AttributeRuleDefinition ruleDefinition in ruleDefinitions)
          {
            AttributeRuleType ruleType = ruleDefinition.GetAttributeRuleType();
            string ruleDescription = ruleDefinition.GetDescription();
            bool isAttributeFieldEditable = ruleDefinition.GetIsFieldEditable();
            string arcadeVersionToSupportRule = ruleDefinition.GetMinimumArcadeVersion();
            int ruleEvaluationOrder = ruleDefinition.GetEvaluationOrder();
            AttributeRuleTriggers triggeringEvents = ruleDefinition.GetTriggeringEvents();
            string scriptExpression = ruleDefinition.GetScriptExpression();
    
            // More properties ...
          }
        }
      }
    }
    Requirements

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

    ArcGIS Pro version: 3.2 or higher.
    See Also