Return to doc.sitecore.com

Change items during the publishing process
Prev Next

Author: Alexander Tsvirchkov
Posted: 7/9/2007 3:34:32 PM

Question: is it possible to catch the publish item event so that I can to make some modifications in web item on the fly during the publishing process?
Version: 5.3.1
Answer: If you want to make some changes for published item in web database, you should hook up the AddedVersion event of Sitecore.Publishing.PublishLog. You can override appropriate methods:
AddedVersion
AddedItem
FailedItem
RemovedItem - should work from version 5.3.1 rev. 070628
RemovedVersions - should work from version 5.3.1 rev. 070628

Example code:

using Sitecore;
using Sitecore.Data.Items;
using Sitecore.Events;
using Sitecore.Publishing;
namespace Sitecore.layouts.Code
{
    public class CustomPublish
    {

        public void OnPublish(object sender, EventArgs args)
        {
            SitecoreEventArgs sArgs = args asSitecoreEventArgs;
            Sitecore.Publishing.Publisher pbl = sArgs.Parameters[0] as Sitecore.Publishing.Publisher;
            pbl.Log = newCustomLog(pbl);
        }
    }
 

    public class CustomLog : Sitecore.Publishing.PublishLog
    {
        public CustomLog(Publisher publisher): base(publisher) {}
        public override void AddedVersion(Item version)
        {
            base.AddedVersion(version);
            Item webItem = this.Publisher.Options.TargetDatabase.Items[version.ID, version.Language];
            if (webItem != null)
            {
                using (new Sitecore.Data.Items.EditContext(webItem))
                {
                   // This code makes changes for a web item.
                   webItem["Text"] = DateTime.Now.ToString(); 
                   webItem["__icon"] = "Network/16x16/home.png";
                }
            }
        }
    }
}
You should use publish:begin event e.g.:
<event name="publish:begin" >
   <handler type="Sitecore.layouts.Code.PublishBegin, CustomPublish" method="OnPublish"/>
</event>

 


Prev Next