1

When I create a new item in the Thai version & publish it by code, the item gets created in the web database, but the fields are empty. There is no error & also no issue when publishing the English version.
During debug, the sourceItem is from Thai version & I can also see the field values.
What am i doing wrong here.
Using Sitecore 8.2

public static PublishResult PublishItem(Item sourceItem, string targetDatabaseName, 
                                         bool publishSubItems)
{
   PublishResult publishResult = null;

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

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

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

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

             using (new SecurityDisabler())
             {
               using (new EventDisabler())
               {
                  publishResult = publisher.PublishWithResult();                                    
               }
             }

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

   return publishResult;
}

Mypublisher.cs

public class MyPublisher : 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;
                }
            }
        }
    }

Also, strangely there is no user name mentioned after publish. If I publish the English item, I can see the logged in username (sitecore\admin).

enter image description here

  • 1
    just a thought, could you please remove using (new EventDisabler()) and try your same code.? I guess this is stopping/silencing post publish events. – Gobinath Saminathan Dec 20 '19 at 21:01
0

The solution suggested by @Gobinath Saminathan worked for me, which is to remove EventDisabler()

Now my code is

using (new SecurityDisabler())
{
  publishResult = publisher.PublishWithResult(); 
}

and it works

| improve this answer | |

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.