/// <summary> /// Changes the workflow state for the given item /// </summary> /// <param name="request">The request.</param> /// <returns>ChangeWorkflowResponse.</returns> public ChangeWorkflowResponse ChangeWorkflow(ChangeWorkflowRequest request) { var response = new ChangeWorkflowResponse { IsSuccess = false }; var item = this._workflowRepository.GetItem(request.ItemId, request.Language); Sitecore.Workflows.IWorkflow wf = this._workflowRepository.GetWorkflow(item); if (wf != null) { try { var result = wf.Execute(request.CommandId, item, request.Comment, false, new object[] { string.Empty }); response.IsSuccess = result.Succeeded; response.Message = result.Message; } catch (Exception ex) { response.Message = ex.Message; this._logger.LogError(ex.Message, ex); } } return(response); }
/// <summary> /// /// </summary> /// <param name="i"></param> /// <param name="commandID">Optional: if included, the command will be executed</param> /// <returns>The workflow state of the item (after workflow command has executed, if given one)</returns> public ArticleWorkflowState ExecuteCommandAndGetWorkflowState(Item i, string commandID) { using (new Sitecore.SecurityModel.SecurityDisabler()) { IWorkflow workflow = i.State.GetWorkflow(); if (workflow == null) { //uh oh return(new ArticleWorkflowState()); } if (commandID != null) { //var oldWorkflow = workflow.WorkflowID; // This line will cause the workflow field to be set to null... sometimes workflow.Execute(commandID, i, "comments", false); //var info = new WorkflowInfo(oldWorkflow, i.Fields[Sitecore.FieldIDs.WorkflowState].Value); //i.Database.DataManager.SetWorkflowInfo(i, info); //i.Fields[Sitecore.FieldIDs.Workflow].Value = oldWorkflow; } var state = new ArticleWorkflowState(); var currentState = workflow.GetState(i); state.DisplayName = currentState.DisplayName; state.IsFinal = currentState.FinalState; var commands = new List <ArticleWorkflowCommand>(); foreach (Sitecore.Workflows.WorkflowCommand command in workflow.GetCommands(i)) { var wfCommand = new PluginModels.ArticleWorkflowCommand(); wfCommand.DisplayName = command.DisplayName; wfCommand.StringID = command.CommandID; ICommand commandItem = _sitecoreMasterService.GetItem <ICommand>(new Guid(command.CommandID)); IState stateItem = _sitecoreMasterService.GetItem <IState>(commandItem.Next_State); if (stateItem == null) { //_logger.Warn("WorkflowController.ExecuteCommandAndGetWorkflowState: Next state for command [" + command.CommandID + "] is null!"); } else { wfCommand.SendsToFinal = stateItem.Final; wfCommand.GlobalNotifyList = new List <StaffStruct>(); foreach (var x in stateItem.Staffs) { var staffItem = _sitecoreMasterService.GetItem <IStaff_Item>(x._Id); if (staffItem.Inactive) { continue; } var staffMember = new StaffStruct { ID = staffItem._Id }; //staffMember.Name = x.GetFullName(); //staffMember.Publications = x.Publications.ListItems.Select(p => p.ID.ToGuid()).ToArray(); wfCommand.GlobalNotifyList.Add(staffMember); } commands.Add(wfCommand); } } state.Commands = commands; return(state); } }
public override void Apply(T ruleContext) { SC.Diagnostics.Assert.ArgumentNotNull(ruleContext, "ruleContext"); SC.Diagnostics.Assert.ArgumentNotNull( ruleContext.Item, "ruleContext.Item"); if (ruleContext.Item.IsStandardValuesOrBranchTemplate()) { return; } SC.Diagnostics.Assert.ArgumentNotNullOrEmpty( this.WorkflowCommandID, "WorkflowCommandID"); SC.Diagnostics.Assert.ArgumentNotNullOrEmpty( this.Comment, "Comment"); SC.Data.Items.Item workflowCommand = ruleContext.Item.Database.GetItem(this.WorkflowCommandID); SC.Diagnostics.Assert.IsNotNull(workflowCommand, "workflowCommand"); SC.Data.Templates.Template checkTemplate = SC.Data.Managers.TemplateManager.GetTemplate(workflowCommand); SC.Diagnostics.Assert.IsTrue( checkTemplate.InheritsFrom(SC.TemplateIDs.WorkflowCommand), "workflow command template"); SC.Data.Items.Item commandWorkflow = workflowCommand.GetAncestorWithTemplateThatIsOrInheritsFrom( SC.TemplateIDs.Workflow); // before invoking a workflow command, // put the item in the appropriate workflow and workflow state if (commandWorkflow == null) { throw new Exception("Unable to determine workflow from workflow command"); } SC.Data.Items.Item commandState = workflowCommand.GetAncestorWithTemplateThatIsOrInheritsFrom( SC.TemplateIDs.WorkflowState); if (commandState == null) { throw new Exception("Unable to determine workflow state from workflow command"); } if (ruleContext.Item[SC.FieldIDs.Workflow] != commandWorkflow.ID.ToString()) { using (new SC.Data.Items.EditContext(ruleContext.Item)) { ruleContext.Item[SC.FieldIDs.Workflow] = commandWorkflow.ID.ToString(); } } if (ruleContext.Item[SC.FieldIDs.WorkflowState] != commandState.ID.ToString()) { using (new SC.Data.Items.EditContext(ruleContext.Item)) { ruleContext.Item[SC.FieldIDs.WorkflowState] = commandState.ID.ToString(); } } SC.Workflows.IWorkflow workflow = ruleContext.Item.State.GetWorkflow(); SC.Diagnostics.Assert.IsNotNull(workflow, "workflow"); workflow.Execute( this.WorkflowCommandID, ruleContext.Item, this.Comment, false, new object[] {}); }