Sitecore 发布限制覆盖

Sitecore Publishing Restriction Override

我有两个发布目标 - 一个是阶段,一个是 production.The 在 Production 上发布应该遵守发布限制,但在 Stage 上发布不应该查看或丢弃有效日期和有效起始日期在任何情况下发布。

我写了一个发布管道(PipelinePublishProvider)。我不确定如何设法临时覆盖字段值,以便它每次都发布到舞台上。

public class StagePublishOverride : PipelinePublishProvider
{

    public override PublishHelper CreatePublishHelper(PublishOptions options)
    {

        Assert.ArgumentNotNull(options, "options");

        if (options.TargetDatabase.Name.ToLower() == "stage")
        {
            Item itemToBePublished = new Item(options.RootItem.ID, options.RootItem.InnerData, new Database("web"));
            itemToBePublished.Editing.BeginEdit();                
            itemToBePublished.Publishing.ValidTo = DateTime.MaxValue;
            itemToBePublished.Publishing.ValidFrom = DateTime.MinValue;
            itemToBePublished.Editing.EndEdit();
            options.RootItem = itemToBePublished;
        }

        if (options is ExtendedPublishOptions)

            return new ExtendedPublishHelper(options as ExtendedPublishOptions);

        return new PublishHelper(options);

    }

}

public class ExtendedPublishHelper : PublishHelper
{

    private readonly ExtendedPublishOptions _options;

    public ExtendedPublishHelper(ExtendedPublishOptions options)

        : base(options)
    {

        _options = options;

    }

    public override Item GetVersionToPublish(Item sourceItem)
    {

        Assert.ArgumentNotNull(sourceItem, "sourceItem");

        if (Options is ExtendedPublishOptions)
        {

            return sourceItem.Publishing.GetValidVersion(Options.PublishDate, _options.RequireApproval);

        }

        return sourceItem.Publishing.GetValidVersion(Options.PublishDate, true);

    }

}

public class ExtendedPublishOptions : PublishOptions
{

    public ExtendedPublishOptions(Database sourceDatabase, Database targetDatabase, PublishMode mode, Language language, DateTime publishDate, bool requireApproval)

        : base(sourceDatabase, targetDatabase, mode, language, publishDate)
    {

        RequireApproval = requireApproval;

    }

    public bool RequireApproval { get; set; }

}

}

我相信你最好在 publishItem 管道中添加一个处理器。这是一些未经测试的代码,我认为它们可以满足您的目的:

public class PublishOverride : PublishItemProcessor
{
    public override void Process(PublishItemContext context)
    {
        Assert.ArgumentNotNull((object)context, "context");
        if (context.Action != PublishAction.None)
            return;
        Item sourceItem = this.GetSourceItem(context);
        if (sourceItem == null)
            return;

        var stagingDB = Factory.GetDatabase("Stage");

        if (stagingDB != null && !sourceItem.Publishing.NeverPublish && context.PublishContext.PublishOptions.TargetDatabase == stagingDB)
        {
            context.Action = PublishAction.PublishVersion;
            context.VersionToPublish = sourceItem;
        }

    }
    private Item GetSourceItem(PublishItemContext context)
    {
        Assert.ArgumentNotNull((object)context, "context");
        return context.PublishHelper.GetSourceItem(context.ItemId);
    }
}

确保在默认配置中的 DetermineAction 处理器之前修补它。所以你的配置补丁看起来像这样:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:x="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <publishItem>
        <processor patch:before="*[@type='Sitecore.Publishing.Pipelines.PublishItem.DetermineAction, Sitecore.Kernel']"
                   type="YourNamespace.PublishOverride, YourAssembly"  />
      </publishItem>
    </pipelines>
  </sitecore>
</configuration>