Problems experienced with Sitecore’s PublishManager.PublishItem

A project that I was working on recently, I had to import items into a Sitecore 8 solution. After the data was imported, then the content needed to be published.

The data was nested up to 5 levels deep. However this was where I was continually experiencing an issue after the publish, in that the Application Pool of the site would have to be recycled again before anything would run.

After debugging it was discovered that the problem was with the PublishingManage.PublishItem command.

Initially this command seemed perfect as you could use it to publish an item, and all its children in every language that the site was using, especially as the site where this command was being used was in twelve languages.

However this was not the case.

This is the code that was originally being used.

public void Publish(Guid itemId)
{
    var item = GetItem(MasterDatabase, itemId, 
        Settings.DefaultLanguage);

    Database[] targetDatabases = { WebDataBase };
    var languages = MasterDatabase.Languages;

    const bool publishChildren = true;
    const bool smartPublish = true;
    const bool publishRelatedItems = true;

    PublishManager.PublishItem(item, targetDatabases, languages, 
        publishChildren, 
        smartPublish,
        publishRelatedItems);
}

After discovering that the problem was with the PublishManger.PublishItem command, I had to update the Publish funtion to publish each language individually.

This is the final code that was used.

public void Publish(Guid itemId)
{
    var item = GetItem(MasterDatabase, itemId, 
        Settings.DefaultLanguage);

    Database[] targetDatabases = { WebDataBase };
    var languages = MasterDatabase.Languages;

    foreach (var language in languages)
    {
        foreach (var targetDatabase in targetDatabases)
        {
            RunPublishJob(item, targetDatabase, language);
        }
}

private void RunPublishJob(Item item, Database targetDatabase, 
    sc.Globalization.Language language)
{
    var options = new PublishOptions(MasterDatabase, targetDatabase, 
        PublishMode.Smart, language, DateTime.Now)
    {
        Deep = true,
        PublishRelatedItems = false,
        RootItem = item
    };

    try
    {
        var publisher = new Publisher(options);
        publisher.Publish();
    }
    catch (Exception ex)
    {
        /// Logger code removed for simplicity
    }
}

Once this change was made, then no longer needed to recycle the App Pool, but I am disappointed that the PublishManger does not do the job.

Advertisements

My musing about anything and everything

Tagged with: , , ,
Posted in PublishManager, Sitecore

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

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 9 other followers

%d bloggers like this: