locked
Get the root of a custom activity RRS feed

  • Question

  • Hello,
    Is there a way to get the workflow activity inside a custom activity, in wf 3.0/3.5 i've use this code:

    Activity GetRootActivity(Activity activity)
    {
             Activity root = null;
             while(activity.Parent != null)
                    root = activity.Parent;
    
             return root;
    }
    with this i could get some information of the workflow that was the host of the custom activity.

    But how to do this in the new wf 4.0
    Frederico Regateiro
    Thursday, February 25, 2010 3:21 PM

Answers

  • Hi, Frederico

    If you're trying to do this during design time, you can get the very root object (workflow service) through EditingContext.Services.GetService<ModelTreeManager>.Root, which is a ModelItem of WorkflowService object.

    Congyu

    Wednesday, August 25, 2010 5:44 AM

All replies

  • Hi Frederico,
    Is this something you want to do at runtime after the workflow is loaded and executing?
    Tim
    Thursday, February 25, 2010 7:19 PM
  • Hello tilovell,

    Yes, in my project my custom activities must know in what type of workflow there executing.

    The problem is that in wf 4.0 there isn't activity.Parent

    Is there any other option,

    Thanks

    Frederico Regateiro
    Thursday, February 25, 2010 10:59 PM
  • Try this:

        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());
            }
        }
    

    Tim
    Friday, February 26, 2010 9:09 AM
  • Hello tilovell,

    Thanks for the reply, it's a start i cloud work around with this.

    But is there a way to go further, i mean get the actualy workflow and not the first activity in it?

    One more thing where to look for more extensions documentation.

    Thanks again.
    Frederico Regateiro
    Friday, February 26, 2010 11:40 AM
  • I haven't tested, but once you have the root of the workflow, I think you can use

    Activity.Id,
    String.SubString(),
    and
    WorkflowInspectionServices.Resolve(root, id)

    to write a GetParent() function.

    WorkflowInspectionServices.GetActivities() is already a 'GetChildren()' function.

    Tim

    Friday, February 26, 2010 7:56 PM
  • Hello Tim,

    Thanks a lot for your help,

    The WorkflowInspectionServices doesn't resolve my problem, and maybe i didn't explaind myselft very well,

    If i create a workflow service i have to put has a first activity a flowchart or a sequence activity, and from there build my service, from the code you provided i can acess the flowchart activity but what i want is to get acess to the workflow service type that my custom activity is in.

    Thanks,

    Frederico Regateiro
    Monday, March 1, 2010 11:07 AM
  • If i create a workflow service i have to put has a first activity a flowchart or a sequence activity, and from there build my service, from the code you provided i can acess the flowchart activity but what i want is to get acess to the workflow service type that my custom activity is in.


    OK, I see. I don't know a specific way to solve the problem yet. I'll make some inquiries.

    UPDATE:

    Because you are trying to get the service type, and not an activity type, there is no good way to get at it through the activities runtime API.

    However if you implement your own service host, I can see possibility of you to create your own service instance and provide access to your service type through the hosting and extension APIs, e.g. WorkflowApplication.Extensions. Unfortunately I don't know the details.

    Tim

    Monday, March 1, 2010 6:09 PM
  • Hi Frederico,
    Is this something you want to do at runtime after the workflow is loaded and executing?
    Tim

    Hi Tilovell,

     

    I want to do same thing during design time. Actually i want to get parent activity when my activity is dropped in designer workflow. With some method i want to know its parent. Am thinking to do this in 

    protected override void CacheMetadata

            (

            NativeActivityMetadata metadata

            )

    but problem is that if am debugging the code i can get (metadata.activity).Parent which returns the sequence in which my activity is dropped using sequence i can get its childs. but (metadata.activity).Parent is not available while coding.

     

    Please help me soon.

     

    Thanks,

    Mohsan


    ilogik
    Sunday, August 22, 2010 4:04 PM
  • Hi Mohsan,

    At design time the WorkflowInspectionServices approach is not usable. I would recommend trying an approach based on the ModelItem tree instead. ModelItem has a .Parent property which usually works to give you a parent object in the workflow (it can be an activity, a pick branch, etc).

    Tim

    Monday, August 23, 2010 8:26 PM
  • Hi, Frederico

    If you're trying to do this during design time, you can get the very root object (workflow service) through EditingContext.Services.GetService<ModelTreeManager>.Root, which is a ModelItem of WorkflowService object.

    Congyu

    Wednesday, August 25, 2010 5:44 AM
  • Hi Congyu

    That's seen like what i want, i try it, thanks


    Frederico Regateiro
    Wednesday, August 25, 2010 1:37 PM