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

后端 未结 6 1748
萌比男神i
萌比男神i 2021-02-20 06:56

I am writing custom activity for sharepoint workflow and I don\'t know how I can use current workflow item, SPWeb or SPSite.

I see http://blogs.microsoft.co.il/blogs/dav

6条回答
  •  独厮守ぢ
    2021-02-20 07:40

    The answer to this is a couple steps:

    1. Add the properties to your Custom Activity .cs
    2. Link the properties in your .actions file (so SPD knows how to map to your properties)
    3. Use the properties in your code

    STEP 1: Here is the code for the properties (my class is named GetEmails which you will need to rename to be your class):

    public static DependencyProperty __ContextProperty = System.Workflow.ComponentModel.DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(GetEmails));
    
    [Description("The site context")]
    [Category("User")]
    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public WorkflowContext __Context
    {
        get
        {
            return ((WorkflowContext)(base.GetValue(GetEmails.__ContextProperty)));
        }
        set
        {
            base.SetValue(GetEmails.__ContextProperty, value);
        }
    }
    
    public static DependencyProperty __ListIdProperty = System.Workflow.ComponentModel.DependencyProperty.Register("__ListId", typeof(string), typeof(GetEmails));
    
    [ValidationOption(ValidationOption.Required)]
    public string __ListId
    {
        get
        {
            return ((string)(base.GetValue(GetEmails.__ListIdProperty)));
        }
        set
        {
            base.SetValue(GetEmails.__ListIdProperty, value);
        }
    }
    
    public static DependencyProperty __ListItemProperty = System.Workflow.ComponentModel.DependencyProperty.Register("__ListItem", typeof(int), typeof(GetEmails));
    
    [ValidationOption(ValidationOption.Required)]
    public int __ListItem
    {
        get
        {
            return ((int)(base.GetValue(GetEmails.__ListItemProperty)));
        }
        set
        {
            base.SetValue(GetEmails.__ListItemProperty, value);
        }
    }
    
    public static DependencyProperty __ActivationPropertiesProperty = DependencyProperty.Register("__ActivationProperties", typeof(Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties), typeof(GetEmails));
    
    [ValidationOption(ValidationOption.Required)]
    public Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties __ActivationProperties
    {
        get
        {
            return (Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties)base.GetValue(GetEmails.__ActivationPropertiesProperty);
        }
        set
        {
            base.SetValue(GetEmails.__ActivationPropertiesProperty, value);
        }
    }
    

    STEP 2: Then in your .actions file add to your block the mappings for those properties (note the entries for __ListID, __ListItem, __Context, and __ActivationProperties):

    
      
        
        
      
      
        
        
        
        
        
        
      
    
    

    STEP 3: Here is an example execute function:

    protected override ActivityExecutionStatus Execute(ActivityExecutionContext provider)
    {
        Output = string.Empty;
    
        try
        {
            SPWeb web = __Context.Web;
            // get all of the information we currently have about the item
            // that this workflow is running on
            Guid listGuid = new Guid(__ListId);
            SPList myList = web.Lists[listGuid];
            SPListItem myItem = myList.GetItemById(__ListItem);
    
            //...
        }
        catch (Exception e)
        {
            //...
        }
    
        return ActivityExecutionStatus.Closed;
    }
    

提交回复
热议问题