开发者

Workflow has SPContext.Current when run manually but not when run "On Change"?

开发者 https://www.devze.com 2023-02-12 12:51 出处:网络
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

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);
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消