Q:

Sitecore update field just before publish

I have a need to update a date time field in Sitecore just before the item is published.  This will act like a "publish date time" when the item is actually published.  I have successfully implemented this in the workflow and that works fine for items in the workflow by adding a custom action. 
For items not in workflow and that should be picked up by the Publish agent, I tapped into the  pipeline and added a processor just before the PerformAction processor.  The field gets updated fine in the master database but it's never published by the publish agent to the web database.  The item with all other values before the field update goes through fine. 
I have tried to debug the issue and feel like it's happening because the updated item is not reflected as part of the publishqueue.  Is there a way I can force the update of the date time field also published in the same process instead of having to force it to publish? 
Any suggestions are welcome. 

A:

You are right updated item is not part of the publish queue.  You need to put your code into publish:itemProcessing event.  
You need to follow next steps:

Add a handler class into 

<event name="publish:itemProcessing" help="Receives an argument of type ItemProcessingEventArgs (namespace: Sitecore. Publishing. Pipelines. PublishItem)"/>

Your publish:itemProcessing event will look like
    <event name="publish:itemProcessing" help="Receives an argument of type ItemProcessingEventArgs (namespace: Sitecore. Publishing. Pipelines. PublishItem)">
        <handler type="YourNameSpace. SetPublishDate, YourAssembly" method="UpdatePublishingDate"/>
    </event>

Create your own class for processing items on publish: 

    public class SetPublishDate
    {
    /// <summary>
    /// Gets from date. 
    /// </summary>
    /// <value>From date.</value>
    public DateTime FromDate { get; set; }

    /// <summary>
    /// Gets to date. 
    /// </summary>
    /// <value>To date.</value>
    public DateTime ToDate { get; set; }

    public void UpdatePublishingDate(object sender, EventArgs args)
    {
        var arguments = args as Sitecore. Publishing. Pipelines. PublishItem. ItemProcessingEventArgs;
        var db = Sitecore. Configuration. Factory. GetDatabase("master");
        Item item = db. GetItem(arguments. Context. ItemId);
        if (item ! = null)
        {
            using (new Sitecore. Data. Events. EventDisabler())
            {
                using (new EditContext(item))
                {
                    //PublishDateFieldName must be datetime field
                    item["PublishDateFieldName"] = DateTime. Now;
                }
            }
        }
    }

