Sharepoint 2010:使用VS2012动态关联工作流与列表

时间:2013-07-12 09:41:57

标签: sharepoint visual-studio-2012 sharepoint-2010 sharepoint-workflow

我使用VS2012创建了自定义列表和任务列表。现在在我创建工作流的同一解决方案中,我想将工作流与列表和任务列表相关联,但我没有在创建工作流的列表下拉列表中看到名称。有没有办法与列表相关联?在运行应用程序时,我不想手动为列表添加工作流程。

1 个答案:

答案 0 :(得分:1)

右键单击VS解决方案资源管理器中的功能,为您的功能添加事件接收器。

由于您正在使用网站集功能,因此在功能激活处理程序中获取对您的库的引用:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
     SPSite site = (SPSite) properties.Feature.Parent;
     SPWeb web = site.RootWeb;
     SPList list = web.Lists["Name of the Document Library"];
}

这假设您的网站是根网站集Web,然后您可以调用以下方法:

public void EnsureWorkflowAssociation(SPList list, string workflowTemplateName, string associationName, bool allowManual, bool startCreate, bool startUpdate)
        {
            var workflowAssociation =
                list.WorkflowAssociations.Cast<SPWorkflowAssociation>().FirstOrDefault(i => i.Name == associationName);
            if (workflowAssociation!=null)
            {
                list.WorkflowAssociations.Remove(workflowAssociation);
                list.Update();
            }

            var web = list.ParentWeb;
            var lcid = (int)web.Language;
            var defaultCulture = new CultureInfo(lcid);

            var template = web.WorkflowTemplates.GetTemplateByName(workflowTemplateName, defaultCulture);
            var association = SPWorkflowAssociation.CreateListAssociation(template, associationName,
                                    web.Lists[Lists.Tasks.Description()],
                                   web.Lists[Lists.WorkflowHistory.Description()]
                                                                   );
            association.AllowManual = true;
            association.AutoStartChange = true;
            association.AutoStartCreate = true;

            list.WorkflowAssociations.Add(association);
            list.Update();


            association = list.WorkflowAssociations[association.Id];
            association.AllowManual = allowManual;
            association.AutoStartChange = startUpdate;
            association.AutoStartCreate = startCreate;
            association.AssociationData = "<Dummy></Dummy>";
            association.Enabled = true;
            list.WorkflowAssociations.Update(association);
            list.Update();

        }

workflowTemplateName 应该是工作流程的名称

associationName 您必须为关联选择的名称