SharePoint 2007:以编程方式启动工作流命中错误

时间:2011-04-03 14:58:49

标签: sharepoint workflow

我想在Pages Library中的itemAdded时以编程方式启动工作流程。 我这样做:

public override void ItemAdded(SPItemEventProperties properties)
{
    SPListItem listItem = properties.ListItem;

    StartWF(listItem);
}

public void StartWF(SPListItem listItem)
{
    using(SPWeb web = listItem.Web) {
        using(SPSite site = web.Site) {
            SPWorkflowManager manager = site.WorkflowManager;
            SPList parentList = listItem.ParentList;
            SPWorkflowAssociationCollection associationCollection =
                parentList.WorkflowAssociations;

            foreach(SPWorkflowAssociation association in
                associationCollection) {
                if (association.Name == "APWFAnn2010") {
                    string data =
                        association.AssociationData;
                    SPWorkflow wf =
                        manager.StartWorkflow(listItem,
                                  association,
                                  association.
                                  AssociationData);
                }
            }
        }
    }
}

然后我点击此错误“系统找不到指定的文件。(HRESULT异常:0x80070002)

“APWFAnn2010”是我想要开始的工作流程名称。它是在页面库中创建的SP默认审批工作流程。

请帮助,提前谢谢。

2 个答案:

答案 0 :(得分:0)

首先,using site中的using web应该是另一种方式 - 另一方面,我甚至没有看到你使用SPWeb,为什么要在那里使用它? 。其次我会重写代码:

public override void ItemAdded(SPItemEventProperties properties)
{
    SPListItem listItem = properties.ListItem;
    spSite = properties.OpenWeb().Site;
    spWeb = properties.Web;
    using(spSite) {
        StartWF(listItem, spSite);
    }
}

public void StartWF(SPListItem listItem, SPSite spSite)
{

    SPList parentList = listItem.ParentList;
    SPWorkflowAssociationCollection associationCollection =
        parentList.WorkflowAssociations;

    foreach(SPWorkflowAssociation association in associationCollection) {
        if (association.Name == "APWFAnn2010") {
            association.AutoStartChange = true;
            association.AutoStartCreate = false;
            association.AssociationData = string.Empty;

            spSite.WorkflowManager.StartWorkflow(listItem,
                                 association,
                                 association.AssociationData);
        }
    }
}

答案 1 :(得分:0)

您也可以通过这种方式将名称传递到上面的函数中,然后您将拥有一个可以在任何项目上启动任何工作流程的函数:

public void StartWF(SPListItem listItem, SPSite spSite, string wfName)  
{ 
   SPList parentList = listItem.ParentList;      
   SPWorkflowAssociationCollection associationCollection = parentList.WorkflowAssociations;        
   foreach (SPWorkflowAssociation association in associationCollection) 
   {
       if (association.Name == wfName) 
       {
           association.AutoStartChange = true;
           association.AutoStartCreate = false;
           association.AssociationData = string.Empty; 
           spSite.WorkflowManager.StartWorkflow(listItem, association,  association.AssociationData);
      }
   }
}