2

I have a requirement to show the validation message for language checkbox if no language is selected. I have used below code. It shows the alert message box but closes the workflow window. Is there a way to show alert message on top of the Worflow window?

 class ValidateItem
    {
        public void Process(WorkflowPipelineArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
            if (args.DataItem == null)
                return;
            using (new SecurityDisabler())
            {
                //check if a valid option is selected in the dropdown
                if (args.CommentFields["Select Items"] != null && args.CommentFields["Select Items"].Equals(""))
                {
                    SheerResponse.Alert("Please select a valid option in the 'Select Items' field");
                    args.AbortPipeline();
                    return;
                }

                if (args.CommentFields["Languages"] != null && args.CommentFields["Languages"].Equals(""))
                {
                    SheerResponse.Confirm("Please select at least one language");
                    SheerResponse.SetReturnValue("failed");
                    args.AbortPipeline();
                    return;
                }
                //Don't do validation for child items in case of branch and artilce approval
                foreach (var obj in args.Parameters)
                {
                    var type = obj.GetType();
                    if (type == typeof(Dictionary<string, string>))
                    {
                        var dictionary = (Dictionary<string, string>)obj;
                        string doValidation;
                        dictionary.TryGetValue("DoValidation", out doValidation);
                        if (doValidation == "false") return;
                    }
                }
                //check if item is not already locked by someone other than the current user.
                //ID for list item 'Single Item' in the dropdown
                //added null check for CommentFields in case workflow is executed through SRM
                if (args.CommentFields["Select Items"] != null && !args.CommentFields["Select Items"].Equals(ItemIds.MaginetPublishingWF.ApprovalOption.SingleItemApproval)
                    && args.DataItem.Locking.IsLocked() && args.DataItem.Locking.GetOwner().ToLowerInvariant() != Sitecore.Context.User.DisplayName.ToLowerInvariant())
                {
                    SheerResponse.Alert("Cannot fast approve '" + args.DataItem.DisplayName + "' as it is locked by another user(" + args.DataItem.Locking.GetOwnerWithoutDomain() + ").", new string[0]);
                    args.AbortPipeline();
                    return;
                }

                //Read item presentation details, validation for related data source content item with at least one version in approve state
                if (!args.CommentFields["Select Items"].Equals(ItemIds.MaginetPublishingWF.ApprovalOption.BranchApproval))
                {
                    var renderings = ItemUtils.GetItemRenderings(args.DataItem);
                    bool relatedDSInDraft = true;
                    foreach (var rd in renderings)
                    {
                        Item dataSource = ItemUtils.GetDataSourceItem(rd.Datasource, args.DataItem);
                        if (dataSource != null && dataSource.IsBasedOnTemplate(TemplateIds.PageTemplate))
                        {
                            foreach (var versionItem in dataSource.Versions.GetVersions())
                            {
                                IWorkflow versionItemWorkflow = versionItem.Database.WorkflowProvider.GetWorkflow(versionItem);
                                if (versionItemWorkflow != null)
                                {
                                    WorkflowState parentWorkflowState = versionItemWorkflow.GetState(versionItem);
                                    if (parentWorkflowState.StateID == ItemIds.MaginetPublishingWF.State.ApprovedState)
                                    {
                                        relatedDSInDraft = false;
                                        break;
                                    }
                                }
                                else
                                {
                                    relatedDSInDraft = false;
                                }
                            }
                            if (relatedDSInDraft)
                            {
                                SheerResponse.Alert("Cannot fast approve '" + args.DataItem.DisplayName + "' becasue for '" + dataSource.Paths.FullPath + "' data source item there is no approved item version'", new string[0]);
                                args.AbortPipeline();
                                return;
                            }
                        }
                    }
                }
                if (args.DataItem.Paths.IsContentItem)
                {
                    //validation for parent item
                    Item parentItem = args.DataItem.Parent;
                    if (parentItem != null)
                    {
                        IWorkflow argsWorkflow = args.DataItem.Database.WorkflowProvider.GetWorkflow(args.DataItem);
                        if (argsWorkflow != null)
                        {
                            WorkflowState argsWorkflowState = argsWorkflow.GetState(args.DataItem);

                            while (parentItem.TemplateID != TemplateIds.Site)
                            {
                                IWorkflow parentWorkflow = parentItem.Database.WorkflowProvider.GetWorkflow(parentItem);
                                if (parentWorkflow != null && parentItem.Fields[FieldIDs.DefaultWorkflow].Value == ItemIds.MaginetPublishingWF.WorkflowID)
                                {
                                    WorkflowState parentWorkflowState = parentWorkflow.GetState(parentItem);
                                    if (parentItem.Versions.Count == 1 && parentWorkflowState.StateID == argsWorkflowState.StateID)
                                    {
                                        SheerResponse.Alert("Cannot fast approve '" + args.DataItem.DisplayName + "' as '" + parentItem.DisplayName + "' is not approved", new string[0]);
                                        args.AbortPipeline();
                                        return;
                                    }
                                }
                                parentItem = parentItem.Parent;
                            }
                        }
                    }
                }

            }
        }

    }
}

enter image description here

1 Answer 1

1

You can add your custom field validation rule in Sitecore. And you just need to apply this rule to the Workflow validation rules of the item. That way it'll show the validation dialog when the workflow state is being changed to final.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.