2012-08-03 55 views
1

場景:如何在代碼中執行工作流?

  • 用戶點擊該工作流的命令
  • 工作流自定義動作進行許多檢查
  • 工作流自定義動作在相同的工作流程依賴於結果執行另一個命令

我到目前爲止的代碼是:

Database db = Factory.GetDatabase("master"); 
if (Request.QueryString["_id"] != null) 
{ 
    var itm = db.GetItem(new ID(Request.QueryString["_id"])); 
    WorkflowCommand[] availableCommands = wf.GetCommands(itm.Fields["__Workflow state"].Value); 
    wf.Execute(Request.QueryString["command"], itm, "Testing working flow new screens", false, new object[] { }); // Execute the workflow step. 
} 

但是,我得到一個對象未​​設置爲wf.Execute線上的實例錯誤 - 但沒有任何有意義的堆棧跟蹤或任何東西:(

我已經在wf.GetCommands行只是爲了檢查事情實際上是在哪裏我期望他們,並且availableCommands被填充了一個很好的命令列表。

我檢查了commandId是有效的,並存在。

Itm不爲空,並且是與工作流相關聯的Content Item(我希望工作流在上下文中運行)。

我檢查過用戶上下文等是有效的,並且沒有權限問題。

唯一的區別是,我運行的.aspx頁是Sitecore的內內執行這段代碼 - hopeever,我也沒有想到這會導致一個問題,除非有沒有被正確設定上下文項。

+0

確實出現了,因爲我在aspx頁面上運行;調用GetWorkFlowState失敗,因爲Context.ContentDatabase爲null。現在如何排序... – 2012-08-03 12:51:34

回答

4

工作流程需要在啓用ContentDatabase和工作流程的SiteContext內運行。在您的網站中執行此操作的最簡單方法是使用SiteContextSwitcher更改爲「外殼」網站。

using (new SiteContextSwitcher(SiteContextFactory.GetSiteContext("shell"))) 
{ 
    wf.Execute(Request.QueryString["command"], itm, "Testing working flow new screens", false, new object[] { }); // Execute the workflow step. 
} 

可以在WeBlog Sitecore模塊的代碼中找到此示例。

http://svn.sitecore.net/WeBlog/Trunk/Website/Pipelines/CreateComment/WorkflowSubmit.cs

+1

啊,很好的回答! – 2012-08-03 19:55:15

相關問題