0

I'm using Sitecore 8.2 and trying to publish an item to 'web' like this:

public static PublishResult PublishItem(string sourceItemPath, Language targetItemLanguage, 
string targetDatabaseName,  bool publishSubItems)
        {
          PublishResult publishResult = null;
          Item sourceItem = Databases.masterDb.GetItem(sourceItemPath, targetItemLanguage);

            if (sourceItem != null)
            {
                Database targetDatabase = GetDatabase(targetDatabaseName);

                if (targetDatabase != null)
                {
                    using (new SecurityDisabler())
                    {
                      Sitecore.Publishing.PublishOptions publishOptions =
                      new Sitecore.Publishing.PublishOptions(Databases.masterDb, 
targetDatabase, Sitecore.Publishing.PublishMode.SingleItem,    
sourceItem.Language, System.DateTime.Now);

                        if (publishOptions != null)
                        {
                            publishOptions.UserName = Sitecore.Context.User.Name; ;
                      Sitecore.Publishing.Publisher publisher = 
              new Sitecore.Publishing.Publisher(publishOptions);

                            if (publisher != null)
                            {
                                publisher.Options.RootItem = sourceItem;

                                publisher.Options.Deep = publishSubItems;

                       publishResult = publisher.PublishWithResult(); ///exception here

                                sourceItem.Publishing.ClearPublishingCache();
                            }
                        }
                    }  
                }
            }

            return publishResult;
        }

The error is:

One or more exceptions occurred while processing the subscribers to the 'publish:end' event.

When published manually, the item gets published.
Do we need to modify any config setting, when publishing items programmatically.

UPDATE: I changed the publishing statement to this & it worked:

Sitecore.Handle handle = PublishManager.PublishItem(sourceItem, new[] { targetDatabase }, 
new[] { targetItemLanguage }, publishSubItems, true);

But I would like to get the published statistics like items created/updated/skipped.

4
  • Any custom pipelines on the publish:end event , Please check from showconfig.aspx and search for the publish:end section – Abhay Dhar Dec 17 '19 at 13:50
  • @AbhayDhar No custom pipelines as I see. This is a fresh instance. – Qwerty Dec 17 '19 at 13:52
  • Does it have SXA installed ? – Abhay Dhar Dec 17 '19 at 14:52
  • No. Just Vanilla Sitecore. Please also check my update in the post – Qwerty Dec 17 '19 at 15:04
1

Your code works for me (not much help, I know :) ) but if there is something in the publish:end event listener that is throwing the error then you could try using an EventDisabler in your code as well as the SecurityDisabler you have;

using (new SecurityDisabler())
using (new EventDisabler())
{
    // your code
}

Not sure that will stop that event happening though. There is a method in the Publisher class called PerformPublishWithResult that doesn't instigate the publish events however it is protected so you'll need your own publisher class to use it.

public class MyPublisher : Sitecore.Publishing.Publisher
{
    public MyPublisher(PublishOptions options)
        : base(options)
    {

    }

    public MyPublisher(PublishOptions options, IEnumerable<Language> languages)
        : base(options, languages)
    {

    }

    public override PublishResult PublishWithResult()
    {
        object publishLock = GetPublishLock();
        lock (publishLock)
        {
            try
            {
                using (new SecurityDisabler())
                {
                    AssertState();
                    PublishResult result = PerformPublishWithResult();
                    UpdateLastPublish();
                    return result;
                }
            }
            catch (Exception ex)
            {
                NotifyFailure(ex);
                throw;
            }
        }
    }
}

Now use MyPublisher instead;

MyPublisher publisher = new MyPublisher(publishOptions);

if (publisher != null)
{
    publisher.Options.RootItem = sourceItem;
    publisher.Options.Deep = publishSubItems;
    publishResult = publisher.PublishWithResult();
    sourceItem.Publishing.ClearPublishingCache();
}

The PublishWithResult method on MyPublisher does what the built-in version does only without raising the publish events.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.