다음을 통해 공유


Getting to Workflow Information

Hi, I'm Phil Allen, a developer with the SharePoint Designer team who works primarily on Workflow. Today I'd like to describe how you can get some important data about your workflow as usable data within a workflow action.

This is all about creating new workflow actions, and so, Visual Studio 2005 with the Extension for Windows WF installed will be very helpful. Boot Visual Studio, File -> New -> Project, Other Languages -> Visual C# -> Workflow, Workflow Activity Library is the place to start. Much of the early portions of this article is similar to the blog post on creating new actions for SPD, but is intended to show you how to get to some of the information about the workflow itself as part of the workflow.

It also builds on top of this MSDN magazine article:
https://msdn.microsoft.com/msdnmag/issues/06/12/windowsworkflow/

Suppose for a moment that you wanted to write an action that calculated the time since the workflow started and returns the number of minutes as a string. Create the shell of your activity, giving it the namespace and class name you want it to have. From there, you'll need to start by thinking about the Execute method of your activity:

namespace MyNamespace

{

    public class MyActivity : Activity

    {

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

        {

            //More goes here later

            return ActivityExecutionStatus.Closed;

        }

    }

}

First, you need the current time:

DateTime currentDateTime = System.DateTime.UtcNow;

Notice the use of UtcNow instead of Now; Now is done in the timezone of the local machine, and not the timezone of the website that you are working with.

Second, you need the time the workflow was started. This is a bit trickier, but what you need to get access to is the SPWorkflowActiviationProperties for the current workflow. Every workflow receives one of these as part of OnWorkflowActivated; however, the goal is to make this work seamlessly in SharePoint Designer.

SharePoint Designer's workflow authoring capabilities has a few special names for properties that will automatically be set to certain values that should always be available in the workflow. Specifically, there's a table of the property names, which value they bind to, and the .Net type of that property.

Property Name

Value it is set to

.Net type

__ActivationProperties

__initProps, the SPWorkflowActivationProperties that was passed in via OnWorkflowActivated

Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties

__ListId

__list, the name of the SPList the workflow is executing on. This is typically a string datatype which contains the text of a System.Guid.

System.String

__ListItem

__item, the integer value of the list item in the list that the workflow is running on.

System.Int32

__Context

__context, the object that contains many helper methods and objects for use as part of workflow actions

Microsoft.SharePoint.WorkflowActions.WorkflowContext

In this case, browsing some of the MSDN pages and most importantly their properties, we find that SPWorkflowActivationProperties has a Workflow property of type SPWorkflow, which has Created property which is a DateTime representing the time the workflow started.

To get access to the SPWorkflowActivationProperties in your Execute method, you need to create a property that SharePoint Designer will automatically set to __initProps.

public static DependencyProperty __ActivationPropertiesProperty = DependencyProperty.Register("__ActivationProperties", typeof(Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties), typeof(MyActivity));

 

[ValidationOption(ValidationOption.Required)]

public Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties __ActivationProperties

{

    get

    {

        return (Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties)base.GetValue(__ActivationPropertiesProperty);

    }

    set

    {

        base.SetValue(__ActivationPropertiesProperty, value);

    }

}

SharePoint Designer won't automatically do the hookup yet; you'll need to add a little bit to your actions file in order to do so. I'll come back to that later.

At this point, you have access to an object that will give you the SPWorkflowObject you want. You also need to have an output parameter of type System.Int32 to output your number of minutes to. Code for that will look like this:

public static DependencyProperty NumberOfMinutesProperty = DependencyProperty.Register("NumberOfMinutes", typeof(System.Int32), typeof(MyActivity));

 

[ValidationOption(ValidationOption.Required)]

public System.Int32 NumberOfMinutes

{

    get

    {

        return (System.Int32)base.GetValue(NumberOfMinutesProperty);

    }

    set

    {

        base.SetValue(NumberOfMinutesProperty, value);

    }

}

This is your output variable.

The last piece of the activity is to finish writing the Execute method:

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

    DateTime currentDateTime = System.DateTime.UtcNow;

//Set this to a recognizable value that would never be a valid value for the started time.

    DateTime startedDateTime = System.DateTime.MinValue;

//Set this to a recognizable value that would never be the result of the real calculation.

    NumberOfMinutes = System.Int32.MaxValue;

Comments

  • Anonymous
    September 30, 2007
    PingBack from http://www.artofbam.com/wordpress/?p=4142

  • Anonymous
    October 07, 2007
    Después de una semana un tanto ajetreada en el CII, noi podíamos faltar al tradicional recopilatorio

  • Anonymous
    October 09, 2007
    Hi, i have problem with __Context property, the value is alway null. I have followed step by step instructions on this article but not work for me. I'm using WSS 3.0 only and not SPD. Can you help me? Thanks

  • Anonymous
    October 30, 2007
    Después de algún tiempo sin postear el habitual recopilatorio de recursos interesantes de WSS 3.0 &

  • Anonymous
    October 30, 2007
    Slightly off topic, but I'm making a workflow in SPD and trying to pull the initiating user information. I don't see any intuitive way of doing this. Am I missing something here? Any help is appreciated.

  • Anonymous
    January 07, 2008
    I've create BaseItemActivity class which declares __Context, __ListId and __ListItem properties. Activities inherited from BaseItemActivity doesn't work correctly in SPD. Does it mean that every activity which uses current item must declare its own __Context, __ListId and __ListItem? public abstract class BaseItemActivity : Activity {    // Fields    public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(BaseItemActivity));    public static DependencyProperty __ListIdProperty = DependencyProperty.Register("__ListId", typeof(string), typeof(BaseItemActivity));    public static DependencyProperty __ListItemProperty = DependencyProperty.Register("__ListItem", typeof(int), typeof(BaseItemActivity)); ... } Correct binding generated by SPD: __ListItem="{ActivityBind ROOT,Path=__item}" __Context="{ActivityBind ROOT,Path=__context}" __ListId="{ActivityBind ROOT,Path=__list}" Binding generated by SPD when inheriting from BaseItemActivity (__ListId and __ListItem are empty, __Context is ???) <... __ListItem="0" __ListId="" ...>  <ns1:MyActivity.__Context>  <ns2:C__153 xmlns:ns2="clr-namespace:My.WorkflowActions.ProxySupport;Assembly=My.WorkflowActions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />  </ns1:MyActivity.__Context>

  • Anonymous
    January 09, 2008
    Hi i have used ur blog to create the custom activity which returns me the UTC time, but when i use the sharepiouint designer string builder to cretae the string and append the datetime varible to it, it converts the datetime backt ot the local time while appending it to the string, but the same things work when i assing the valut ot datetime column, any solution for this. Sandeep

  • Anonymous
    February 10, 2008
    La semana pasada surgió el siguiente requerimiento algo especial, en este escenario se cuenta con un

  • Anonymous
    July 11, 2008
    Hi there, I’m Jordan Hull, a Software Development Engineer in Test on the SharePoint Designer Workflow

  • Anonymous
    January 25, 2009
    The comment has been removed