July 2008 - Posts

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;
dlroW olleH (no, there isn’t a problem with the encoding)

It is an unwritten law that the first post in a blog must have the title “Hello World”, I can’t break this law or I will be banned from the programmers / bloggers / geeks guild, I decided to give this post the title “dlroW olleH” as I hope that this blog will give you a different point of view in the flow of information that we are surrounded with (and no, you won’t need a mirror to view my future posts).

About myself:
Currently working in Omnisys as a software development team leader.
Main points of interest:

  • .NET and C# (The real hardcore…)
  • ASP.NET
  • WSS / MOSS
  • Project server
  • Extreme troubleshooting

 

“Do, or do not. There is no 'try.'"
Jedi Master Yoda

If you understood this quote as a detailed design for your next project (as I did), you are welcome to fasten your seatbelt as we start our journey.

I will be more than happy to receive comments to my posts.