我正在将自定义解决方案升级到Sharepoint 2010.我想使用 WorkflowCompleted 事件处理程序,但我似乎无法获得相关的 SPListItem < / strong>来自事件属性。
我尝试使用 SPWorkflowEventProperties.ActivationProperties ,但这总是返回null(即使在 WorkflowStarted 事件处理程序中)。
如何从工作流事件处理程序( SPListItem , SPWeb , SPSite 等)获取上下文?
答案 0 :(得分:2)
我发现了同样的事情。 SPWorkflowEventProperties几乎没用,因为几乎所有东西都是null。它不会告诉状态(已批准,已拒绝等)。而且,最重要的是,它没有(直接)说明完成了哪个项目。希望这将在未来版本中得到解决。与此同时,我使用了以下内容:
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;
}
答案 1 :(得分:1)
您可以使用InstanceId属性从工作流任务列表中检索SPListItem,如本文所示
http://blog.symprogress.com/2011/09/sp-2010-get-workflow-status-workflowcompleted-event/
答案 2 :(得分:0)
如果你对细节更加慷慨,我可以更具体地回答。
但这通常是你需要做的事情:
设置特定于上下文的属性
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
的所有内容,如下所示:
受保护的覆盖ActivityExecutionStatus 执行(ActivityExecutionContext executionContext){ //提升调用事件以在工作流中执行自定义代码。 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 { /**/ }
}