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>
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.