2

我正在将我们的自定义解决方案升级到 Sharepoint 2010。我想使用WorkflowCompleted事件处理程序,但我似乎无法从事件属性中获取相关的SPListItem 。

我尝试使用SPWorkflowEventProperties.ActivationProperties但这总是返回 null (即使在WorkflowStarted事件处理程序中)。

如何从工作流事件处理程序(SPListItemSPWebSPSite等)获取上下文?

4

3 回答 3

2

我发现了同样的事情。SPWorkflowEventProperties 实际上没用,因为几乎所有内容都为空。它不告诉状态(已批准、已拒绝等)。而且,最重要的是,它不会(直接)告诉您完成了哪些项目。希望这将在未来的版本中得到解决。与此同时,我使用了以下内容:

public override void WorkflowCompleted(SPWorkflowEventProperties properties)
{
   using (SPSite site = new SPSite(properties.WebUrl))
   {
       using (SPWeb web = site.OpenWeb())
       {
           SPListItem task = GetApprovedTask(properties, web);
           SPListItem item = GetApprovedItem(web, task);
           if (null != item)
           {
               // TODO : process approved item
           }
       }
   }
}

private SPListItem GetApprovedItem(SPWeb web, SPListItem task)
{
   SPListItem item = null;
   if (null != task)
   {
       SPList list = web.Lists[new Guid(task[SPBuiltInFieldId.WorkflowListId].ToString())];
       item = list.GetItemById((int)task[SPBuiltInFieldId.WorkflowItemId]);
   }
   return item;
}

private SPListItem GetApprovedTask(SPWorkflowEventProperties properties, SPWeb web)
{
   SPListItem item = null;
   string caml = @"<Where><And><And><And><Eq><FieldRef Name='WorkflowOutcome' /><Value Type='Text'>Approved</Value></Eq><Eq><FieldRef Name='WorkflowInstanceID' /><Value Type='Guid'>{0}</Value></Eq></And><IsNotNull><FieldRef Name='WorkflowListId' /></IsNotNull></And><IsNotNull><FieldRef Name='WorkflowItemId' /></IsNotNull></And></Where>";
   SPQuery query = new SPQuery();
   query.Query = string.Format(caml, properties.InstanceId);
   query.RowLimit = 1;
   SPList list = web.Lists["Tasks"];
   SPListItemCollection items = list.GetItems(query);
   if (items.Count > 0)
   {
       item = items[0];
   }
   return item;
}
于 2011-05-04T19:25:39.073 回答
1

您可以使用 InstanceId 属性从工作流任务列表中检索 SPListItem,如本文所示

http://blog.symprogress.com/2011/09/sp-2010-get-workflow-status-workflowcompleted-event/

于 2011-09-17T12:16:09.077 回答
0

如果您可以更慷慨地提供细节,我可以更具体地回答。

但这通常是您需要做的:

  1. 设置特定于上下文的属性

    公共静态 DependencyProperty _ ContextProperty = System.Workflow.ComponentModel.DependencyProperty.Register(" _ Context", typeof(WorkflowContext), typeof(MyCustomActivity));

[Description("Site Context")] [Category("User")] [Browsable(true)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

public WorkflowContext __Context
{
    get
    {
        return ((WorkflowContext)(base.GetValue(MyCustomActivity.__ContextProperty)));
    }

    set
    {
        base.SetValue(MyCustomActivity.__ContextProperty, value);
    }
}

public static DependencyProperty __ListIdProperty
    = System.Workflow.ComponentModel.DependencyProperty.Register("__ListId",
    typeof(string), typeof(MyCustomActivity));

[ValidationOption(ValidationOption.Required)]

public string __ListId
{
    get
    {
        return ((string)(base.GetValue(MyCustomActivity.__ListIdProperty)));
    }

    set
    {
        base.SetValue(MyCustomActivity.__ListIdProperty, value);
    }
}

public static DependencyProperty __ListItemProperty
    = System.Workflow.ComponentModel.DependencyProperty.Register("__ListItem",
    typeof(int), typeof(MyCustomActivity));

[ValidationOption(ValidationOption.Required)]

public int __ListItem
{
    get
    {
        return ((int)(base.GetValue(MyCustomActivity.__ListItemProperty)));
    }

    set
    {
        base.SetValue(MyCustomActivity.__ListItemProperty, value);
    }
}

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

[ValidationOption(ValidationOption.Required)]

public Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties __ActivationProperties
{
    get
    {
        return (Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties)base.GetValue(MyCustomActivity.__ActivationPropertiesProperty);
    }

    set
    {
        base.SetValue(MyCustomActivity.__ActivationPropertiesProperty, value);
    }
}
  1. 您将获得上下文__Context 以及其他所有内容,__Context如下所示:

    protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext) { // 引发调用事件以在工作流中执行自定义代码。this.RaiseEvent(MyCustomActivity.InvokeEvent, this, EventArgs.Empty);

    SPWeb _cxtWeb = null;
    String _strLinfo = "Dll;";
    String _strTo = String.Empty;
    
    // Set Context
    try
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (_cxtWeb = __Context.Web)
            {
                //_cxtWeb = __Context.Web;
                Guid _cListId = new Guid(__ListId);
                SPList _cSPList = _cxtWeb.Lists[_cListId]; // TimeLog
                SPListItem _cListItem = _cSPList.GetItemById(__ListItem);
                SMTPSERVER = _cxtWeb.Site.WebApplication
                                            .OutboundMailServiceInstance.Server.Address;
            }
        });
    }
    catch { /**/ }
    

    }

于 2011-05-03T23:12:46.590 回答