How to publish related items of sitecore programmatically

571 Views Asked by At

Please suggest any way to publish related items in sitecore programmatically.

P.S: Deep = true did not work.

2

There are 2 best solutions below

0
On BEST ANSWER

I did it by getting all references of the item and published it using publish manager. I used GetReferences() to get the references.

Thanks.

1
On

PublishOptions class has "PublishRelatedItems" property.

You can use it e.g. with this extension method written by Brian Pedersen ( https://briancaos.wordpress.com/2019/09/13/sitecore-publish-items-using-the-publishmanager/ )

public static void PublishItem(
    this Item item, 
    PublishMode publishMode, 
    bool publishAsync = false, 
    bool deepPublish = false, 
    bool publishRelatedItems = false, 
    bool compareRevisions = false)
{
   
  if (item == null)
    return;

  PublishOptions publishOptions = new PublishOptions(item.Database, Database.GetDatabase("web"), publishMode, item.Language, DateTime.Now);
  publishOptions.RootItem = item;
  publishOptions.Deep = deepPublish;
  publishOptions.PublishRelatedItems = publishRelatedItems;
  publishOptions.CompareRevisions = compareRevisions;

  var handle = PublishManager.Publish(new PublishOptions[] { publishOptions });
  if (publishAsync)
    return;
  PublishManager.WaitFor(handle);
}