If I run this workflow manually it works fine. When I let it run automatically "on edit" SPContext.Current is null. How do I get access to S开发者_运维百科PContext.Current when it is run automatically?
SharePoint routinely runs your workflows under completely different processes. At least, you can expect your workflow activities to appear under:
- an IIS worker process,
- the
owstimer.exe
process, - any arbitrary executable that interfaces with SharePoint (e.g. a console application).
In respect to the event that triggered the workflow and the complexity of the workflow scenario (!!) SharePoint selects the process that will actually execute it. Thus a long running workflow when triggered from ASP.NET (i.e. the IIS worker process) is automatically rescheduled to run under owstimer.exe
.
The result is you cannot use SPContext.Current
. In workflow activities you have to use a WorkflowContext
instance which provides a Web
property. Your activites shall declare a dependency property of type WorkflowContext
to get access to it — read more in MSDN here. The VS item template will provide you with the necessary code.
Example:
public partial class LogEventActivity: Activity
{
public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(LogEventActivity));
[Browsable(true)]
[ValidationOption(ValidationOption.Required)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public WorkflowContext __Context
{
get
{
return (WorkflowContext)base.GetValue(__ContextProperty);
}
set
{
base.SetValue(__ContextProperty, value);
}
}
}
精彩评论