Custom rule: how to read the parents of a workflow activity?

My custom rule has to check that a particular activity that has toolbox name AddLogFields is within a State (Workflow-> State Machine->State) activity.

So I want to check the parents of my activity, and I imagine a loop that uses the Parent or ImmediateParent property to climb upwards and check each parent?

Could you advise, maybe with a code snippet, on the best practice for doing this please?

Hello @stephen.newbery

Its little bit confusing to understand the actual requirement. Can you please explain with some samples.


Hi Rahul, ok I have made a screenshot of the workflow, showing the AddLogFields activity in the State that’s called “Init”.

The rule should check that this AddLogFields activity is present in the State. And to do this, I’m thinking that in the WalkThrough code, when I find the AddLogFields activity, then I should look at the parents to see if it is within the “Init” State.

Or, maybe the other way round, when I read the State, then I inspect the children to see if AddLogFields is there? Maybe possible with Linq?

I made some crude code to read the parents but it’s likely not good because the parents won’t all be ActivityModel I guess:

if (activityModel.ToolboxName == "AddLogFields")
  bool foundInit = false;
  IActivityModel parentActivity;
      parentActivity = activityModel.Parent;
      Console.WriteLine(parentActivity.ToolboxName + ": " + parentActivity.DisplayName);
  while (foundInit == false && activityModel.Parent != null);

Ok…here are you trying to print the state name??? Or whats the intnetion of using this activity here?


The rule is that the AddLogFields activity must be present in the State which is here called “Init”, like in the screenshot.

So what I’m trying to do is determine if the AddLogFields activity is in the State “Entry” area:

Now I’m thinking that probably I should inspect the StateMachine, and check the activities in the nodes?