我正在将我们的定制解决方案升级到Sharepoint 2010。我想利用WorkflowCompleted事件处理程序,但我似乎无法从事件属性中获得相关的SPListItem。
我尝试使用 spworkflowventproperties。ActivationProperties,但这总是返回null(即使在WorkflowStarted事件处理程序中)。
如何从工作流事件处理程序(SPListItem, SPWeb, SPSite等)中获取上下文?
我发现了同样的事情。spworkflowventproperties实际上是无用的,因为几乎所有东西都是空的。它不告诉状态(批准,拒绝等)。而且,最重要的是,它不会(直接)告诉你完成了哪个项目。希望这将在未来的版本中得到解决。同时,我使用了以下代码:
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;
}
您可以使用InstanceId属性从工作流任务列表中检索SPListItem,如本文所示
http://blog.symprogress.com/2011/09/sp - 2010 -让-工作流-状态- workflowcompleted event/
如果你能更慷慨地提供细节,我可以更具体地回答。
但这通常是你需要做的:
-
设置特定于上下文的属性
public static DependencyProperty _ContextProperty= System.Workflow.ComponentModel.DependencyProperty.Register(" _Context",typeof (WorkflowContext) typeof (MyCustomActivity));
[描述("网站背景")](类别("用户"))(可浏览的(真正的))[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);
}
}
您将获得
__Context
的上下文和__Context
的其他所有内容,如下所示:protected override ActivityExecutionStatus执行(ActivityExecutionContext) {//引发Invoke Event以执行工作流中的自定义代码。this.RaiseEvent (MyCustomActivity.InvokeEvent这个,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 { /**/ }
}