Sitecore Workflows: Scheduled Publishing

One of the common tasks required from Sitecore developers is to implement workflows that end up being published immediately or at a specific time in the future (Scheduled Publishing).

The first option is straightforward to do and doesn’t require any external coding or user interaction (other than changing workflow states). The second option can also be implemented without additional coding. However, it requires users to select ‘Standard Fields’ under the View tab in Sitecore ribbon, navigate to a ‘Standard’ section called ‘Publishing’ and set the ‘Publish’ date to a future date. Publishing the site will not affect this item now until its ‘Publish’ date is due.

Publishing.PNG

This works but I prefer not forcing the user into sections that should be hidden. In addition, the user editing the item has to set this ‘Publish’ date at the time of editing. What if we want a supervisor or auditor to set this ‘Publish’ date from the workbox once approved?

Workflow Comment Templates

Let’s start with the following workflow configuration for an ‘Awaiting Approval’ state:

schedulePublishConfig

One of the really useful features Sitecore has introduced lately is the Workflow Comment Template in the workflow command items. Originally, you can allow the workbox user to enter a comment on commands. But a string-based comment is not always enough. Now, you can create a new template with all the fields you need to be filled. Sitecore will prompt the user to fill the fields on commands.

CommentTemplate

fields

After setting the workflow command’s ‘Comment Template’ to the newly created template, you’ll be able to see the following when you click the command button:

dialog

Workbox users can now enter their comments, in addition to selecting the item’s publish date, which could be now or in the future. Of course, we haven’t implemented the scheduled functionality yet!

The final part of the comment template is to read the data entered in the above dialog. To do this, add a new workflow action under the Scheduled Publish Command (or the existing ‘Schedule’ action if you created the above configuration). Actions under the Command will be automatically triggered by Sitecore after the command is executed. You’ll have to enter the ‘Type string’ of your newly created class that will contain the action’s logic. The action class should contain a method named ‘Process’ with one parameter of type ‘WorkflowPipelineArgs’.

In our case, the ‘Process’ method should do a couple of things:

  1. Read the scheduled date from the comment template and set it to the standard field ‘Publish’ date
  2. Add the current item into the publish queue
public void Process(WorkflowPipelineArgs args)
{
      var item = args.DataItem;

      if (item == null)
        return;

      //Read the scheduled date from the commentFields
      var scheduledDateTime = args.CommentFields[this.Scheduled_Publish_DateTimeFieldName];
      item.Editing.BeginEdit();
      //Set the date into the Publish date standard field
      item.Fields["__Publish"].Value = string.IsNullOrEmpty(scheduledDateTime) == false ? scheduledDateTime : DateUtil.ToIsoDate(DateTime.Now);
      item.Editing.EndEdit();

      //Add the item into the publish queue
      Sitecore.Publishing.PublishManager.AddToPublishQueue(item, ItemUpdateType.Created, DateUtil.IsoDateToDateTime(item.Fields["__Publish"].Value));
}

At this point, executing the ‘Scheduled Publish’ command will prompt the user to enter the ‘Scheduled Publish’ date and then trigger the above ‘Process’ method which will set the future Publish date using Sitecore’s standard Publish field and add the current item to the publish queue.

Setting up the Sitecore PublishAgent

The above implementation will simply allow the item to be published after the specified date. But it does not do the publishing. To fix this, we need to create a job that tries to publish changes every while. Luckily Sitecore provides out of the box a PublishAgent that does exactly this.

This agent runs a publish operation using predefined publish options on predefined intervals. Open your Sitecore.config and search for ‘Sitecore.Tasks.PublishAgent’ and edit its publish options:

<agent type="Sitecore.Tasks.PublishAgent" method="Run" interval="00:05:00">
  <param desc="source database">master</param>
  <param desc="target database">web</param>
  <param desc="mode (full or smart or incremental)">incremental</param>
  <param desc="languages">en,ar</param>
</agent>

Source database should be the content database which is ‘master’ and target database should be the ‘web’ database. The mode should be ‘incremental’ since we are using the publish queue. Then you should set the language versions to be published. Finally, edit the interval attribute to something that makes sense to your business needs. I have set the interval to 5 minutes: ’00:05:00′.

At this point, if you followed the steps, you should be able to create a new item using our workflow, select the ‘Scheduled Publish’ command, enter the scheduled publish date and see that the value of the ‘Publish’ field has become the value entered in the dialog. In around 5 minutes (in case your interval is set to 5 minutes), you should be able to see the item in your web database. Otherwise, please see the log to diagnose any other issues.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s