How to get the context item in Workflow activity (SharePoint)

Although it seems a fairly simple and commonly used task to get the context item (the item that the workflow is currently working on), it took me and Dor Rotman some digging to find the correct way to do it.

The first step is to add 3 properties to your activity, these properties have fixed names, I guess that in runtime the activity is loaded using reflection and the context variables are placed into these properties. To simply add a property in a workflow activity, you can use the code snippet wdp in visual studio 2005 (if you don’t have this snippet download the latest version of WF extensions for VS here)

wdp

The name of the properties need to be added are:

  • __Context
  • __ListId
  • __ListItem

After adding these properties your code should look like this: (note: change YourActivityClass to the name of your assembly)

   1: public static DependencyProperty __ContextProperty = System.Workflow.ComponentModel.DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(YourActivityClass));
   2:  
   3: [Description("Context")]
   4: [Category("Context")]
   5: [Browsable(true)]
   6: [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
   7: public WorkflowContext __Context
   8: {
   9:     get
  10:     {
  11:         return ((WorkflowContext)(base.GetValue(YourActivityClass.__ContextProperty)));
  12:     }
  13:     set
  14:     {
  15:         base.SetValue(YourActivityClass.__ContextProperty, value);
  16:     }
  17: }
  18:  
  19: public static DependencyProperty __ListIdProperty = System.Workflow.ComponentModel.DependencyProperty.Register("__ListId", typeof(string), typeof(YourActivityClass));
  20:  
  21: [Description("List Id")]
  22: [Category("List Id")]
  23: [Browsable(true)]
  24: [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  25: public string __ListId
  26: {
  27:     get
  28:     {
  29:         return ((string)(base.GetValue(YourActivityClass.__ListIdProperty)));
  30:     }
  31:     set
  32:     {
  33:         base.SetValue(YourActivityClass.__ListIdProperty, value);
  34:     }
  35: }
  36:  
  37: public static DependencyProperty __ListItemProperty = System.Workflow.ComponentModel.DependencyProperty.Register("__ListItem", typeof(int), typeof(YourActivityClass));
  38:  
  39: [Description("List Item")]
  40: [Category("List Item")]
  41: [Browsable(true)]
  42: [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  43: public int __ListItem
  44: {
  45:     get
  46:     {
  47:         return ((int)(base.GetValue(YourActivityClass.__ListItemProperty)));
  48:     }
  49:     set
  50:     {
  51:         base.SetValue(YourActivityClass.__ListItemProperty, value);
  52:     }
  53: }

And add the following to the parameters section of the actions file that you will create for you activity (located in: C:\program files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\[LOCALE]\Workflow )

   1: <Parameters>
   2:     <Parameter Name="__Context" Type="Microsoft.SharePoint.WorkflowActions.WorkflowContext, Microsoft.SharePoint.WorkflowActions" Direction="In"/>
   3:     <Parameter Name="__ListId" Type="System.String, mscorlib, mscorlib" Direction="In" />
   4:     <Parameter Name="__ListItem" Type="System.Int32, mscorlib, mscorlib" Direction="In" />
   5: </Parameters>

When you will open this activity in SharePoint Designer you won’t see these parameters as they are identified as system parameters.

Finally, the code to get the item in the Execute function is:

   1: SPWeb contextWeb = __Context.Web;
   2: SPList contextList = contextWeb.Lists[new Guid(__ListId)];
   3: SPListItem contextItem = contextList.GetItemById(__ListItem);
   4: SPFile contextFile = contextItem.File;
Published 21 July 2008 08:30 AM by DavidBi

Comments

# Dor Rotman`s Blog said on 22 July, 2008 10:00 AM

David Birin , a talented colleague of mine who also works at Omnisys , has just published a post about

# Mirrored Blogs said on 14 January, 2009 02:45 AM

Body: I currently have a requirement for a client where there a series of folders inside a document library

# Sharepoint WorkFlow Context said on 17 June, 2009 12:22 PM

Pingback from  Sharepoint WorkFlow Context

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: