Return Value
The Arcade expression.
| Exception | Description |
|---|---|
| ArcGIS.Core.CalledOnWrongThreadException | This method or property must be called within the lambda passed to QueuedTask.Run |
| ArcGIS.Core.Data.Exceptions.GeodatabaseException | A geodatabase-related exception has occurred. |
{
//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...
}
}
}
}
{
// 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 ...
}
}
}
}
Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)