Beispiel #1
0
        public void FeatureActivatedTest()
        {
            SPFeatureReceiverProperties properties = Isolate.Fake.Instance <SPFeatureReceiverProperties>(Members.ReturnRecursiveFakes);
            SPFarm fakeSPFarm = Isolate.Fake.Instance <SPFarm>(Members.ReturnRecursiveFakes);

            Isolate.WhenCalled(() => SPFarm.Local).WillReturn(fakeSPFarm);
            Hashtable farmProperties = new Hashtable();

            Isolate.WhenCalled(() => fakeSPFarm.Properties).WillReturn(farmProperties);

            MockConfigManager.ReturnValue = "http://localhost";
            SharePointServiceLocator.ReplaceCurrentServiceLocator(new ActivatingServiceLocator()
                                                                  .RegisterTypeMapping <IConfigManager, MockConfigManager>()
                                                                  .RegisterTypeMapping <ILogger, MockLogger>());

            SPWeb fakeSPWeb = Isolate.Fake.Instance <SPWeb>(Members.ReturnRecursiveFakes);

            Isolate.WhenCalled(() => properties.Feature.Parent).WillReturn(fakeSPWeb);

            SPWorkflowAssociation fakeAssociation = Isolate.Fake.Instance <SPWorkflowAssociation>(Members.ReturnRecursiveFakes);

            Isolate.WhenCalled(() => SPWorkflowAssociation.CreateListAssociation(null, "", null, null)).WillReturn(fakeAssociation);

            FeatureReceiver target = new FeatureReceiver();

            target.FeatureActivated(properties);

            Isolate.Verify.WasCalledWithAnyArguments(() => fakeSPWeb.Lists["Sub Site Creation Requests"].Update());
        }
Beispiel #2
0
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb web = properties.Feature.Parent as SPWeb;

            IConfigManager hierarchicalConfig = SharePointServiceLocator.Current.GetInstance <IConfigManager>();

            hierarchicalConfig.SetInPropertyBag(Constants.SubSiteCreationConfigSiteKey, web.Url, SPFarm.Local);

            SPList taskList    = web.Lists[taskListName];
            SPList historyList = web.Lists[workflowHistoryName];
            SPList subSiteCreationRequestList = web.Lists[subSiteRequestsListName];

            var existingAssociation = subSiteCreationRequestList.WorkflowAssociations.GetAssociationByName(workflowTemplateName, CultureInfo.CurrentCulture);

            if (existingAssociation == null)
            {
                SPWorkflowManager            workflowManager = web.Site.WorkflowManager;
                SPWorkflowTemplateCollection templates       = workflowManager.GetWorkflowTemplatesByCategory(web, null);
                SPWorkflowTemplate           template        = templates.GetTemplateByBaseID(workflowTemplateId);
                SPWorkflowAssociation        association     = SPWorkflowAssociation.CreateListAssociation(template, template.Name, taskList, historyList);
                association.AllowManual     = true;
                association.AutoStartCreate = true;
                subSiteCreationRequestList.AddWorkflowAssociation(association);
                subSiteCreationRequestList.Update();
                association.Enabled = true;
            }

            ServiceLocatorConfig typeMappings = new ServiceLocatorConfig();

            typeMappings.RegisterTypeMapping <IBusinessEventTypeConfigurationRepository, BusinessEventTypeConfigurationRepository>();
            typeMappings.RegisterTypeMapping <ISubSiteCreationRequestRepository, SubSiteCreationRequestsRepository>();
        }
        private void iInstallListsWorkflowsInstall(SPList oList, string sName, string sDisplayName, SPList oTaskList, SPList oHistoryList,
                                                   XmlNode ndWorkflow)
        {
            SPWorkflowAssociation assocation = null;

            var bAllowManual = false;

            bool.TryParse(ApplicationInstallerHelpers.getAttribute(ndWorkflow, "AllowManual"), out bAllowManual);

            var bStartOnCreate = false;

            bool.TryParse(ApplicationInstallerHelpers.getAttribute(ndWorkflow, "StartOnCreate"), out bStartOnCreate);

            var bStartOnChange = false;

            bool.TryParse(ApplicationInstallerHelpers.getAttribute(ndWorkflow, "StartOnChange"), out bStartOnChange);


            foreach (SPWorkflowTemplate template in oWeb.WorkflowTemplates)
            {
                if (template.Name == sName)
                {
                    assocation = SPWorkflowAssociation.CreateListAssociation(template, sDisplayName, oTaskList, oHistoryList);
                    break;
                }
            }
            if (assocation != null)
            {
                assocation.AllowManual     = bAllowManual;
                assocation.AutoStartChange = bStartOnChange;
                assocation.AutoStartCreate = bStartOnCreate;
                oList.WorkflowAssociations.Add(assocation);
            }
        }
Beispiel #4
0
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb web = properties.Feature.Parent as SPWeb;

            if (web != null)
            {
                SPList taskList         = web.Lists[Lists.RegistrationApprovalTasks];
                SPList historyList      = web.Lists[Lists.WorkflowHistory];
                SPList registrationList = web.Lists[Lists.Registrations];

                SPWorkflowAssociation existingAssociation = null;
                existingAssociation = registrationList.WorkflowAssociations.GetAssociationByName(WorkflowTemplates.RegistrationApprovalNameV2, CultureInfo.CurrentCulture);
                if (existingAssociation == null)
                {
                    //Create a worklow manager and associate the Course Registration Approval Workflow template
                    //to our Registrations list.
                    SPWorkflowManager            workflowManager = web.Site.WorkflowManager;
                    SPWorkflowTemplateCollection templates       = workflowManager.GetWorkflowTemplatesByCategory(web, null);
                    SPWorkflowTemplate           template        = templates.GetTemplateByBaseID(new Guid(WorkflowTemplates.RegistrationApprovalBaseIdV2));
                    SPWorkflowAssociation        association     = SPWorkflowAssociation.CreateListAssociation(template, template.Name, taskList, historyList);
                    association.AllowManual     = true;
                    association.AutoStartCreate = true;
                    registrationList.AddWorkflowAssociation(association);
                    registrationList.Update();
                    association.Enabled = true;
                }
            }
        }
Beispiel #5
0
        private void AssociateListWorkflow()
        {
            SPList targetList = Web.Lists[this.associationParams.TargetListGuid];
            SPWorkflowAssociation association;

            if (associationParams.AssociationGuid == Guid.Empty)
            {
                association = SPWorkflowAssociation.CreateListAssociation(
                    Web.WorkflowTemplates[new Guid(this.associationParams.BaseTemplate)],
                    this.associationParams.AssociationName,
                    Web.Lists[this.associationParams.TaskListGuid],
                    Web.Lists[this.associationParams.HistoryListGuid]);
                PopulateAssociationParams(association);
                targetList.WorkflowAssociations.Add(association);
            }
            else
            {
                association = targetList.WorkflowAssociations[this.associationParams.AssociationGuid];
                association.SetTaskList(Web.Lists[this.associationParams.TaskListGuid]);
                association.SetHistoryList(Web.Lists[this.associationParams.HistoryListGuid]);
                PopulateAssociationParams(association);
                targetList.WorkflowAssociations.Update(association);
            }

            SetDefaultContentApprovalWorkflow(targetList, association);
        }
Beispiel #6
0
        private void DeployListWorkflowAssociationDefinition(ListModelHost modelHost, SPList list, WorkflowAssociationDefinition definition)
        {
            var existingWorkflowAssotiation = FindExistringWorkflowAssotiation(modelHost, definition);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioning,
                Object           = existingWorkflowAssotiation,
                ObjectType       = typeof(SPWorkflowAssociation),
                ObjectDefinition = definition,
                ModelHost        = modelHost
            });

            bool isNew = false;

            if (existingWorkflowAssotiation == null)
            {
                var workflowTemplate = GetWorkflowTemplate(modelHost, definition);

                if (workflowTemplate == null)
                {
                    throw new SPMeta2Exception(
                              string.Format("Cannot find workflow template by definition:[{0}]", definition));
                }

                existingWorkflowAssotiation = SPWorkflowAssociation.CreateListAssociation(workflowTemplate,
                                                                                          definition.Name,
                                                                                          list.ParentWeb.Lists[definition.TaskListTitle],
                                                                                          list.ParentWeb.Lists[definition.HistoryListTitle]);

                isNew = true;
            }

            MapProperties(definition, existingWorkflowAssotiation);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioned,
                Object           = existingWorkflowAssotiation,
                ObjectType       = typeof(SPWorkflowAssociation),
                ObjectDefinition = definition,
                ModelHost        = modelHost
            });

            if (isNew)
            {
                list.WorkflowAssociations.Add(existingWorkflowAssotiation);
                list.Update();
            }
            else
            {
                list.WorkflowAssociations.Update(existingWorkflowAssotiation);
            }
        }
        private void DeployListWorkflowAssociationDefinition(ListModelHost modelHost, SPList list, WorkflowAssociationDefinition definition)
        {
            var existingWorkflowAssotiation = FindExistringWorkflowAssotiation(modelHost, definition);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioning,
                Object           = existingWorkflowAssotiation,
                ObjectType       = typeof(SPWorkflowAssociation),
                ObjectDefinition = definition,
                ModelHost        = modelHost
            });

            bool isNew = false;

            if (existingWorkflowAssotiation == null)
            {
                var workflowTemplate = GetWorkflowTemplate(modelHost, definition);

                existingWorkflowAssotiation = SPWorkflowAssociation.CreateListAssociation(workflowTemplate,
                                                                                          definition.Name,
                                                                                          list.Lists[definition.TaskListTitle],
                                                                                          list.Lists[definition.HistoryListTitle]);

                isNew = true;
            }

            MapProperties(definition, existingWorkflowAssotiation);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioned,
                Object           = existingWorkflowAssotiation,
                ObjectType       = typeof(SPWorkflowAssociation),
                ObjectDefinition = definition,
                ModelHost        = modelHost
            });

            if (isNew)
            {
                list.WorkflowAssociations.Add(existingWorkflowAssotiation);
            }
            else
            {
                // ??
            }
        }
Beispiel #8
0
        public static SPWorkflowAssociation CreateWorkflowAssociation(this SPList list, SPWorkflowTemplate workflowTemplate, string assocName, SPList workflowTasksList, SPList workflowHistoryList, bool overwrite = false, Action <SPWorkflowAssociation> action = null)
        {
            if (workflowTemplate == null)
            {
                throw new ArgumentNullException("workflowTemplate");
            }

            SPWorkflowAssociation workflowAssociation = null;
            // create the association
            SPWorkflowAssociation assoc =
                SPWorkflowAssociation.CreateListAssociation(workflowTemplate, assocName, workflowTasksList,
                                                            workflowHistoryList);

            if (action != null)
            {
                action(assoc);
            }

            //apply the association to the list
            if (list != null)
            {
                if (overwrite)
                {
                    SPWorkflowAssociation dupWfAssoc =
                        list.WorkflowAssociations.GetAssociationByName(assocName, list.ParentWeb.Locale);

                    if (dupWfAssoc != null)
                    {
                        if (dupWfAssoc.BaseId == workflowTemplate.Id)
                        {
                            list.WorkflowAssociations.Remove(dupWfAssoc);
                        }
                        else
                        {
                            throw new SPException(string.Format("Duplicate workflow name \"{0}\" is detected.", assocName));
                        }
                    }
                }

                workflowAssociation = list.WorkflowAssociations.Add(assoc);
            }

            return(workflowAssociation);
        }
        private static void NewCreatePOAssociation(WorkflowDescription _dsc, SPWeb _web, SPList _taskList, SPList _historyList, bool _autoStartCreate)
        {
            SPWorkflowTemplate _workflowTemplate = _web.WorkflowTemplates[_dsc.WorkflowId];
            // create workflow association
            SPWorkflowAssociation _freightPOLibraryWorkflowAssociation =
                SPWorkflowAssociation.CreateListAssociation(
                    _workflowTemplate,
                    _dsc.Name,
                    _taskList,
                    _historyList);

            // configure workflow association and add to WorkflowAssociations collection
            _freightPOLibraryWorkflowAssociation.Description     = _dsc.Description;
            _freightPOLibraryWorkflowAssociation.AllowManual     = true;
            _freightPOLibraryWorkflowAssociation.AutoStartCreate = _autoStartCreate;
            _freightPOLibraryWorkflowAssociation.AutoStartChange = false;
            _freightPOLibraryWorkflowAssociation.AssociationData = _dsc.Name;
            _web.Lists[CommonDefinition.ShippingListTitle].WorkflowAssociations.Add(_freightPOLibraryWorkflowAssociation);
        }
        private static void NewSendEmailAssociation(string _targetList, SPWeb _web, SPList _taskList, SPList _historyList, POLibraryWorkflowAssociationData _wfData)
        {
            SPWorkflowTemplate _workflowTemplate = _web.WorkflowTemplates[SendEmail.SendEmail.WorkflowId];
            // create workflow association
            SPWorkflowAssociation _wa = SPWorkflowAssociation.CreateListAssociation(
                _workflowTemplate,
                _wfData.Name,
                _taskList,
                _historyList);

            // configure workflow association and add to WorkflowAssociations collection
            _wa.Description     = "Send PO by email";
            _wa.AllowManual     = true;
            _wa.AutoStartCreate = true;
            _wa.AutoStartChange = false;
            // add workflow association data
            _wa.AssociationData = _wfData.Serialize(_wa);
            _web.Lists[_targetList].WorkflowAssociations.Add(_wa);
        }
Beispiel #11
0
        public static void EnsureWorkflowAssociation(SPList list, string workflowTemplateName, string associationName, bool allowManual, bool startCreate, bool startUpdate)
        {
            var web            = list.ParentWeb;
            var lcid           = (int)web.Language;
            var defaultCulture = new CultureInfo(lcid);

            // Create the workflow association
            SPList taskList    = EnsureListExist(web, workflowTaskListName);
            SPList historyList = EnsureListExist(web, workflowHistoryListName);

            var workflowAssociation =
                list.WorkflowAssociations.Cast <SPWorkflowAssociation>().FirstOrDefault(i => i.Name == associationName);

            if (workflowAssociation != null)
            {
                list.WorkflowAssociations.Remove(workflowAssociation);
                list.Update();
            }

            var template    = web.WorkflowTemplates.GetTemplateByName(workflowTemplateName, defaultCulture);
            var association = SPWorkflowAssociation.CreateListAssociation(template, associationName, taskList, historyList);

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

            Debug.WriteLine("Ensure.List.Workflow: " + associationName + " associated");
        }
        private static void NewWorkflowAssociation(string _targetList, WorkflowDescription _dsc, SPWeb _web, SPList _taskList, SPList _historyList)
        {
            if (string.IsNullOrEmpty(_targetList))
            {
                throw new ApplicationException("The parameter _targetList of the NewWorkflowAssociation cannot be null or empty");
            }
            SPWorkflowTemplate _workflowTemplate = _web.WorkflowTemplates[_dsc.WorkflowId];
            // create workflow association
            SPWorkflowAssociation _workflowAssociation =
                SPWorkflowAssociation.CreateListAssociation(
                    _workflowTemplate,
                    _dsc.Name,
                    _taskList,
                    _historyList);

            // configure workflow association and add to WorkflowAssociations collection
            _workflowAssociation.Description     = _dsc.Description;
            _workflowAssociation.AllowManual     = true;
            _workflowAssociation.AutoStartCreate = false;
            _workflowAssociation.AutoStartChange = false;
            _workflowAssociation.AssociationData = _dsc.Name;
            _web.Lists[_targetList].WorkflowAssociations.Add(_workflowAssociation);
        }
        public SPWorkflowAssociationInstance CreateListAssociation(SPWorkflowTemplateInstance template, string name, SPListInstance taskList, SPListInstance historyList)
        {
            if (template == null)
            {
                throw new JavaScriptException(this.Engine, "Error", "A workflow template must be supplied as the first argument.");
            }

            if (taskList == null)
            {
                throw new JavaScriptException(this.Engine, "Error", "A task list must be supplied as the third argument.");
            }

            if (historyList == null)
            {
                throw new JavaScriptException(this.Engine, "Error", "A history list must be supplied as the fourth argument.");
            }

            var result = SPWorkflowAssociation.CreateListAssociation(template.SPWorkflowTemplate, name, taskList.List,
                                                                     historyList.List);

            return(result == null
                ? null
                : new SPWorkflowAssociationInstance(this.Engine.Object.InstancePrototype, result));
        }
Beispiel #14
0
        public void BtnOK_Click(object sender, EventArgs e)
        {
            SPList taskList    = null;
            SPList historyList = null;

            if (!IsValid)
            {
                return;
            }
            if (!m_bContentTypeTemplate)
            {
                // If the user requested a new task list, create it.
                if (m_taskListId == Guid.Empty)
                {
                    string description = string.Format("Task list for the {0} workflow.", m_workflowName);
                    m_taskListId = Web.Lists.Add(m_taskListName, description, SPListTemplateType.Tasks);
                }

                // If the user requested a new history list, create it.
                if (m_historyListId == Guid.Empty)
                {
                    string description = string.Format("History list for the {0} workflow.", m_workflowName);
                    m_historyListId = Web.Lists.Add(m_historyListName, description, SPListTemplateType.WorkflowHistory);
                }
                taskList    = Web.Lists[m_taskListId];
                historyList = Web.Lists[m_historyListId];
            }

            // perform association (if it doesn't already exist)
            bool isNewAssociation;

            if (m_assocTemplate == null)
            {
                isNewAssociation = true;
                if (!m_bContentTypeTemplate)
                {
                    m_assocTemplate = SPWorkflowAssociation.CreateListAssociation(m_baseTemplate,
                                                                                  m_workflowName,
                                                                                  taskList,
                                                                                  historyList);
                }
                else
                {
                    m_assocTemplate = SPWorkflowAssociation.CreateSiteContentTypeAssociation(m_baseTemplate,
                                                                                             m_workflowName,
                                                                                             taskList.Title,
                                                                                             historyList.Title);
                }
            }
            else // modify existing template
            {
                isNewAssociation     = false;
                m_assocTemplate.Name = m_workflowName;
                m_assocTemplate.SetTaskList(taskList);
                m_assocTemplate.SetHistoryList(historyList);
            }

            // set up starup parameters in the template
            m_assocTemplate.Name            = m_workflowName;
            m_assocTemplate.LockItem        = m_bLockItem;
            m_assocTemplate.AutoStartCreate = m_autoStartCreate;
            m_assocTemplate.AutoStartChange = m_autoStartChange;
            m_assocTemplate.AllowManual     = m_allowManual;
            if (m_assocTemplate.AllowManual)
            {
                SPBasePermissions newPerms = SPBasePermissions.EmptyMask;

                if (Request.Params["ManualPermEditItemRequired"] == "ON")
                {
                    newPerms |= SPBasePermissions.EditListItems;
                }
                if (Request.Params["ManualPermManageListRequired"] == "ON")
                {
                    newPerms |= SPBasePermissions.ManageLists;
                }

                m_assocTemplate.PermissionsManual = newPerms;
            }

            // place data from form into the association template
            m_assocTemplate.AssociationData = SerializeFormToString(FormType.Association);// SerializeFormToString(m_assocTemplate.AssociationData, FormType.Association);

            // if its a content type association, add the template to the content type
            if (m_ct != null)
            {
                if (isNewAssociation)
                {
                    m_ct.AddWorkflowAssociation(m_assocTemplate);
                }
                else
                {
                    m_ct.UpdateWorkflowAssociation(m_assocTemplate);
                }
                if (m_updateLists)
                {
                    m_ct.UpdateWorkflowAssociationsOnChildren(false);
                }
            }
            else// Else, if this is a list association,
            {
                if (isNewAssociation) //if new association
                {
                    List.AddWorkflowAssociation(m_assocTemplate);
                }
                else
                {
                    List.UpdateWorkflowAssociation(m_assocTemplate);
                }

                if (m_assocTemplate.AllowManual && List.EnableMinorVersions)
                {
                    //In case you selected this WF to be the content approval WF (m_setDefault = true see association page)
                    if (List.DefaultContentApprovalWorkflowId != m_assocTemplate.Id && m_setDefault)
                    {
                        if (!List.EnableModeration)
                        {
                            List.EnableModeration = true;
                        }
                        List.DefaultContentApprovalWorkflowId = m_assocTemplate.Id;
                        List.Update();
                    }
                    else if (List.DefaultContentApprovalWorkflowId == m_assocTemplate.Id && !m_setDefault)
                    {
                        // Reset the DefaultContentApprovalWorkflowId
                        List.DefaultContentApprovalWorkflowId = Guid.Empty;
                        List.Update();
                    }
                }
            }
            string strUrl = StrGetRelativeUrl(this, "WrkSetng.aspx", null)
                            + m_strQueryParams;

            Response.Redirect(strUrl);
        }
Beispiel #15
0
        public static void AssociateWorkflow(this SPList list, string workflowTemplateName, string workflowAssocName, string association, string wfTaskList)
        {
            SPWeb                 web                  = list.ParentWeb;
            SPList                historyList          = null;  // Workflow history list
            SPList                taskList             = null;  // Workflow tasks list
            string                workflowTemplateGuid = null;  // Workflow template Guid
            SPWorkflowTemplate    workflowTemplate     = null;  // Workflow template
            SPWorkflowAssociation workflowAssociation  = null;  // Workflow association

            try
            {
                // Workflow association name

                // Allow unsafe updates on web
                web.AllowUnsafeUpdates = true;

                //workflowTemplateGuid = "BAD855B1-32CE-4bf1-A29E-463678304E1A";
                //workflowTemplate = web.WorkflowTemplates[workflowTemplateName];

                workflowTemplate = web.WorkflowTemplates.GetTemplateByName(workflowTemplateName, System.Globalization.CultureInfo.CurrentCulture);
                if (workflowTemplate == null)
                {
                    return;
                }
                try
                {
                    historyList = web.Lists["Workflow History"];
                }
                catch (ArgumentException exc)
                {
                    // Create workflow history list
                    Guid listGuid = web.Lists.Add("Workflow History", "", SPListTemplateType.WorkflowHistory);
                    historyList        = web.Lists[listGuid];
                    historyList.Hidden = true;
                    historyList.Update();
                }

                try
                {
                    if (string.IsNullOrEmpty(wfTaskList))
                    {
                        wfTaskList = "Workflow Tasks";
                    }
                    taskList = web.Lists[wfTaskList];
                }
                catch (ArgumentException exc)
                {
                    // Create workflow tasks list
                    Guid listGuid = web.Lists.Add(wfTaskList, "", SPListTemplateType.Tasks);
                    taskList        = web.Lists[listGuid];
                    taskList.Hidden = true;
                    taskList.Update();
                }



                // Create workflow association
                workflowAssociation = SPWorkflowAssociation.CreateListAssociation(
                    workflowTemplate,
                    workflowAssocName, taskList, historyList);

                // Set workflow parameters
                workflowAssociation.AllowManual     = true;
                workflowAssociation.AutoStartCreate = false;
                workflowAssociation.AutoStartChange = false;
                workflowAssociation.AssociationData = association.ToString();

                // Add workflow association to my list
                list.WorkflowAssociations.Add(workflowAssociation);

                // Enable workflow
                workflowAssociation.Enabled = true;
            }
            catch (Exception ex)
            {
                Utility.LogError(ex);
            }
            finally
            {
                web.AllowUnsafeUpdates = false;
            }
        }
Beispiel #16
0
        protected void associateCustomWorkflows()
        {
            AddWorklfowTemplate();

            try
            {
                foreach (SiteCollInfo siteCollInfo in Owner.WorkingSiteCollections)
                {
                    using (SPSite siteColl = new SPSite(siteCollInfo.URL))
                    {
                        foreach (SiteInfo siteInfo in siteCollInfo.Sites)
                        {
                            using (SPWeb web = siteColl.OpenWeb(siteInfo.ID))
                            {
                                if (web.Features[new Guid("00bfea71-f600-43f6-a895-40c0de7b0117")] == null)
                                {
                                    web.Features.Add(new Guid("00bfea71-f600-43f6-a895-40c0de7b0117"));
                                }
                                else
                                {
                                    Owner.IncrementCurrentTaskProgress("", siteInfo.Lists.Count);
                                }

                                SPWorkflowTemplateCollection wfTemplates = web.WorkflowTemplates;
                                int templateIndex = -1;

                                for (int i = 0; i < wfTemplates.Count; i++)
                                {
                                    SPWorkflowTemplate wfTemplate = wfTemplates[i];
                                    if (wfTemplate.Name == "SPDG Workflow")
                                    {
                                        templateIndex = i;
                                    }
                                }

                                if (templateIndex == -1)
                                {
                                    Log.Write("Declarative Workflow 'SPDG Workflow' not found.");
                                    return;
                                }


                                foreach (ListInfo listInfo in siteInfo.Lists)
                                {
                                    try
                                    {
                                        SPList             list             = web.Lists[listInfo.Name];
                                        SPWorkflowTemplate workflowTemplate = web.WorkflowTemplates[templateIndex];

                                        SPList wftasks = web.Lists.TryGetList("Workflow Tasks");
                                        if (wftasks == null)
                                        {
                                            //Check if Workflow List exists
                                            Guid listGuid = web.Lists.Add(
                                                "Workflow Tasks",
                                                string.Empty,
                                                SPListTemplateType.Tasks);
                                            wftasks = web.Lists.GetList(listGuid, false);
                                        }

                                        SPList wfhistory = web.Lists.TryGetList("Workflow History");
                                        if (wfhistory == null)
                                        {
                                            //Check if Workflow List exists
                                            Guid listGuid = web.Lists.Add(
                                                "Workflow History",
                                                string.Empty,
                                                SPListTemplateType.WorkflowHistory);
                                            wfhistory        = web.Lists.GetList(listGuid, false);
                                            wfhistory.Hidden = true;
                                            wfhistory.Update();
                                        }


                                        // create the association
                                        SPWorkflowAssociation assoc =
                                            SPWorkflowAssociation.CreateListAssociation(
                                                workflowTemplate, workflowTemplate.Name,
                                                wftasks, wfhistory
                                                );
                                        assoc.AllowManual = true;

                                        //apply the association to the list
                                        list.WorkflowAssociations.Add(assoc);
                                        Owner.IncrementCurrentTaskProgress(string.Format("Added declarative workflow to {0} on subsite{1}", listInfo.Name, web.Url));
                                    }
                                    catch (Exception ex)
                                    {
                                        Errors.Log(ex);
                                        Owner.IncrementCurrentTaskProgress(string.Format("Error while adding declarative workflow to {0} on subsite{1}", listInfo.Name, web.Url));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Errors.Log(ex);
            }
        }
Beispiel #17
0
        void associateWorkflows()
        {
            try
            {
                foreach (SiteCollInfo siteCollInfo in Owner.WorkingSiteCollections)
                {
                    using (SPSite siteColl = new SPSite(siteCollInfo.URL))
                    {
                        // Activating out-of-the-box workflows
                        if (siteColl.Features[new Guid("0af5989a-3aea-4519-8ab0-85d91abe39ff")] == null)
                        {
                            siteColl.Features.Add(new Guid("0af5989a-3aea-4519-8ab0-85d91abe39ff"));
                        }

                        foreach (SiteInfo siteInfo in siteCollInfo.Sites)
                        {
                            using (SPWeb web = siteColl.OpenWeb(siteInfo.ID))
                            {
                                int workflowCount = web.WorkflowTemplates.Count;

                                foreach (ListInfo listInfo in siteInfo.Lists)
                                {
                                    try
                                    {
                                        SPList     list             = web.Lists[listInfo.Name];
                                        int        randomNumberOfWF = SampleData.GetRandomNumber(1, 2);
                                        List <int> addedTemplates   = new List <int>();

                                        for (int i = 0; i < randomNumberOfWF; i++)
                                        {
                                            Owner.IncrementCurrentTaskProgress("Adding Workflow Association to list '" + web.Url + "/" + list.RootFolder.Url + "'");
                                            int templateIndex = SampleData.GetRandomNumber(0, 5);

                                            if (addedTemplates.Any(x => x == templateIndex))
                                            {
                                                continue;
                                            }

                                            SPWorkflowTemplate workflowTemplate = web.WorkflowTemplates[templateIndex];

                                            SPList wftasks = web.Lists.TryGetList("Workflow Tasks");
                                            if (wftasks == null)
                                            {
                                                //Check if Workflow List exists
                                                Guid listGuid = web.Lists.Add(
                                                    "Workflow Tasks",
                                                    string.Empty,
                                                    SPListTemplateType.Tasks);
                                                wftasks = web.Lists.GetList(listGuid, false);
                                            }

                                            SPList wfhistory = web.Lists.TryGetList("Workflow History");
                                            if (wfhistory == null)
                                            {
                                                //Check if Workflow List exists
                                                Guid listGuid = web.Lists.Add(
                                                    "Workflow History",
                                                    string.Empty,
                                                    SPListTemplateType.WorkflowHistory);
                                                wfhistory        = web.Lists.GetList(listGuid, false);
                                                wfhistory.Hidden = true;
                                                wfhistory.Update();
                                            }


                                            // create the association
                                            SPWorkflowAssociation assoc =
                                                SPWorkflowAssociation.CreateListAssociation(
                                                    workflowTemplate, workflowTemplate.Name,
                                                    wftasks, wfhistory
                                                    );
                                            assoc.AllowManual = true;

                                            //apply the association to the list
                                            list.WorkflowAssociations.Add(assoc);

                                            addedTemplates.Add(templateIndex);
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        Errors.Log(ex);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Errors.Log(ex);
            }
        }