How to get the context from workflow event handler in Sharepoint 2010
I am in the process of upgrading our custom solutions to Sharepoint 2010. I wanted to utilize the WorkflowCompleted event handler but I don't seem to be able to get the relevant SPListItem from the event properties.
I tried using SPWorkflowEventProperties.ActivationProperties but this always retu开发者_如何学Crns null (even in the WorkflowStarted event handler).
How do I get the context from workflow event handlers (SPListItem, SPWeb, SPSite etc)?
I've found the same thing. SPWorkflowEventProperties is practically useless since just about everything is null. It doesn't tell status (Approved, Rejected, etc). And, most importantly, it doesn't (directly) tell what item was completed. Hopefully this will be addressed in future versions. In the meantime, I used the following:
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;
}
You can use the InstanceId property to retrieve the SPListItem from the workflow task list as shown in this post
http://blog.symprogress.com/2011/09/sp-2010-get-workflow-status-workflowcompleted-event/
If you can be more generous with details I can be more specific with answer.
But this is generally what you need to do:
Set context specific properties
public static 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);
}
}
You will get context in
__Context
and everything else from__Context
like this:protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext) { // Raise Invoke Event to execute custom code in the workflow. 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 { /**/ }
}
精彩评论