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

发布于 2024-10-29 22:37:12 字数 1240 浏览 3 评论 0原文

我想在页面库中添加项目时以编程方式启动工作流程。 我执行以下操作:

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 默认审批工作流程。

请帮忙,先谢谢你了。

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;

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间不值得 2024-11-05 22:37:12

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

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

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);
      }
   }
}
攀登最高峰 2024-11-05 22:37:12

首先,您在 using web 中拥有您的 using site ,这应该是相反的 - 另一方面,我什至没有看到您使用 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);
        }
    }
}

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);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文