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

21 ביולי 2008

4 תגובות

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;

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. (*) שדות חובה מסומנים

4 תגובות

  1. Pablo12 בנובמבר 2010 ב 12:28

    hi, i've an issue then __Contex objet olwais is null, what's the problem?

    להגיב
  2. Sukhvinder23 בנובמבר 2010 ב 9:05

    hi , Even I get the __Context and the listId objects always null.

    להגיב
  3. unodei22 באוגוסט 2011 ב 8:14

    Actually, __ListId and ListItem need to be set manually from __Context.ListId and __Context.ItemId

    להגיב