locked
How to get the root workflow activity for inspecting the children activities RRS feed

  • Question

  • Hi, 

    I need to inspect all the activities in a workflow in order to retrieve the preset values of certain properties of some custom activities. I use this code in the first activity of the workflow: 

    public sealed class PageFlowInitializer : CodeActivity
    {
    	protected override void Execute(CodeActivityContext context)
     	{ 
     		InspectActivity(new PageFlow()); //??? TODO: find a reference to the current workflow instance activity
     	}
    
     	void InspectActivity(Activity root)
     	{
     		// Inspect the activity tree using WorkflowInspectionServices.
     		IEnumerator<Activity> activities =
     			WorkflowInspectionServices.GetActivities(root).GetEnumerator();
    
     		if (root is PageActivity) //other custom activity
     		{
     			string section = ((System.Activities.Expressions.Literal<string>)((PageActivity)root).Section.Expression).Value;
     			string state = ((System.Activities.Expressions.Literal<string>)((PageActivity)root).State.Expression).Value;
     			string url = ((System.Activities.Expressions.Literal<string>)((PageActivity)root).Url.Expression).Value;
    
     			//TODO: save the above information in a custom object list that is put inside an output argument
     		}
    
     		while (activities.MoveNext())
     		{
     			InspectActivity(activities.Current);
     		}
     	}
     }
    

    Now I have not found how to get a reference to the current instance of the workflow (I mean the root activity) in order to inspect all the children activities inside 
    the Execute method of the activity code above. Clearly "new PageFlow()" as argument to the InspectActivity method is not a viable option.
    Every suggestion is well accepted. 
    Thanks, 
       Giuseppe.

     

     




    Tuesday, June 21, 2011 11:19 PM

Answers

  • Hi Giuseppe,

    You can try the following code which use WorkflowInstanceProxy.WorkflowDefinition to retrieve the root.

    public sealed class GetRootActivity : NativeActivity

        {

            public class WorkflowInstanceInfo : IWorkflowInstanceExtension

            {

                public IEnumerable<object> GetAdditionalExtensions()

                {

                    yield break;

                }

     

                public void SetInstance(WorkflowInstanceProxy instance)

                {

                    this.proxy = instance;

                }

     

                WorkflowInstanceProxy proxy;

     

                public WorkflowInstanceProxy GetProxy() { return proxy; }

            }

     

            protected override void Execute(NativeActivityContext context)

            {

                WorkflowInstanceProxy proxy = context.GetExtension<WorkflowInstanceInfo>().GetProxy();

                Activity root = proxy.WorkflowDefinition;

                //Action resumeBookmarkLater = () => { proxy.BeginResumeBookmark(...); };

            }

     

            protected override void CacheMetadata(NativeActivityMetadata metadata)

            {

                base.CacheMetadata(metadata);

                metadata.AddDefaultExtensionProvider<WorkflowInstanceInfo>(() => new WorkflowInstanceInfo());

            }

        }


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Monday, June 27, 2011 9:59 AM