开发者

SharePoint 2007: Programmatically Start workflow hit error

I wanted to programmatically start workflow when itemAdded in Pages Library. I do as following :

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

    Sta开发者_JS百科rtWF(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);
                }
            }
        }
    }
}

then I hit this error "The system cannot find the file specified. (Exception from HRESULT: 0x80070002)"

"APWFAnn2010" is the workflow name I wanted to start. It is SP default Approval Workflow created in Pages Library.

Please help, thank you in advance.


You can also pass in the name into the function above by doing it this way, and then you'd have a function that can start any workflow on any item:

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


First of you have your using site within the using web it should be the other way around - on the other hand I don't even see you using the SPWeb, so why have it in there?. And secondly I would rewrite the code a bit:

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜