Web developers wiki ASP.NET Sitecore Sharepoint Kentico by Evident Interactive

Publish items from code behind

Modified: 2009/03/31 12:39 by jim - Categorized as: ASP.NET, Csharp, Sitecore
When you create and edit items from code behind pages in a non-Sitecore workflow, you will need to publish the items from the code behind page. Please note the PublishOptions constructor differs between sitecore versions. The example below is Sitecore 5.3.1.

        public static void PublishItem(ID targetItemID)
        {
            SecurityDisabler securitydisabler = new SecurityDisabler();
            DateTime publishDate = DateTime.Now;
            Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");
            Sitecore.Data.Items.Item home = master.GetItem(targetItemID);
            string targetDBName = "web";
            Sitecore.Data.Database targetDB = Sitecore.Configuration.Factory.GetDatabase(targetDBName);
            foreach (Sitecore.Globalization.Language language in master.Languages)
            {
                Sitecore.Publishing.PublishOptions publishOptions = new Sitecore.Publishing.PublishOptions(Sitecore.Publishing.PublishMode.SingleItem, language, publishDate);
                publishOptions.RootItem = home;
                publishOptions.Deep = false;
                publishOptions.SourceDatabase = master;
                publishOptions.TargetDatabase = targetDB;
                Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions); 
                publisher.Publish();
            }
            SecurityEnabler securityenabler = new SecurityEnabler();
        }

 © Evident Interactive BV