Search code examples
c#asp.netvisual-studio-2012sitecoresitecore7

C# Method - Publish Entire Sitecore Site


I have an odd request. I am wondering if it is possible to have your C# solution file publish the entire Site from the master database to the web database. I am in my development environment and the amount of items in Sitecore is changing daily as I am working with multiple people. This is not something that is going to be used to Production Content Management or Content Delivery. Purely development.

Is it possible to trigger a full Site publish in C# just like pressing the publish button in the content editor? And what would the code look like?

/sitecore/shell/Applications/Publish.aspx

I assume this C# method would work with Sitecore.Publishing.PublishManager?

Thanks!


Solution

  • We also use that on our projects... We have that placed in a regular .aspx page. I hope that helps:

    protected void Page_Load(object sender, EventArgs e)
    {
        PublishMode publishMode = PublishMode.Full;
    
        using (new Sitecore.SecurityModel.SecurityDisabler())
        {
            var webDb = Sitecore.Configuration.Factory.GetDatabase("web");
            var masterDb = Sitecore.Configuration.Factory.GetDatabase("master");
    
            try
            {
                foreach (Language language in masterDb.Languages)
                {
                    //loops on the languages and do a full republish on the whole sitecore content tree
                    var options = new PublishOptions(masterDb, webDb, publishMode, language, DateTime.Now) { RootItem = masterDb.Items["/sitecore"], RepublishAll = true, Deep = true };
    
                    var myPublisher = new Publisher(options);
                    myPublisher.Publish();
                }
            }
            catch (Exception ex)
            {
                Sitecore.Diagnostics.Log.Error("Could not publish", ex);
            }
        }
    }