如何以编程方式发布 sitecore 的相关项目

How to publish related items of sitecore programmatically

请建议任何以编程方式在 sitecore 中发布相关项目的方法。

P.S:Deep = true 无效。

PublishOptions class 有“PublishRelatedItems”属性。

您可以使用它,例如使用由 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);
}

我通过获取项目的所有引用并使用发布管理器发布它来做到这一点。我使用 GetReferences() 来获取引用。

谢谢。