Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

sitecore9 - Sitecore Cancel Publish Based On Conditions

I have a requirement like when user clicks on the Publish Item button, I wanted to execute some custom code where I will run some validations against the current item and if it's all good, then the item will be published otherwise an alert will be displayed and the publish pipeline should be disabled.

So for this I have created custom processor under publishItem pipeline and it all works good but when I am showing the alert it's giving me null reference exception.

Sitecore.Context.ClientPage.ClientResponse.Alert(message);

I am not sure what wrong I am doing here and is there any other way to achieve this ...Please suggest Below is the sample code which I wrote The below class is inherited from PublishItemProcessor.

public override void Process(PublishItemContext context)
        {
            Assert.ArgumentNotNull(context, "context");

            // code goes here

            if (condition)
            {
                string message = "Required fields are missing";
                context.AbortPipeline();                
                Sitecore.Context.ClientPage.ClientResponse.Alert(message);
            }
        }

<publishItem>
      <processor type="Namespace.CheckRequiredChildItems, Namespace" />
</publishItem>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should avoid changing the publishing pipeline, as it may prevent you from upgrading to Sitecore Publish Service in the future. As a general principle, any user should be able to publish any item at any time. The Sitecore publish restrictions and workflows will prevent unfinished/invalid items from being published. An item may also be published in many different ways, such as item publish (as in your described scenario), as a related item, site publish etc. Publishing may also run in the background, so UI interaction isn't suitable.

You should use workflows for this instead, as one of its purposes is to prevent items from reaching its publishable state without fulfilling validation rules. If your editors don't want to use workflows, you can still use it almost seamlessly. You can have a simple workflow with just two states (Draft and Ready for Publish). You can then either let authors transition the item through the workflow or use automatic workflow actions to transition it to the next step. Failing validation rules will prevent it from being transitioned.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...