如何创建没有工作流程的sitecore项目版本?

时间:2017-10-11 10:18:32

标签: sitecore sitecore-workflow

我在Sitecore遇到了业务需求,我们不需要附加工作流程,但我们想要在锁定编辑项目时创建新版本。

我无法应用以下设置,因为它会影响所有项目,而我的工作流程也无用。

<setting name="RequireLockBeforeEditing" value="true"/>

我也经历了stackoverflow question并且它已经给出了以下解决方案,但不确定它应该是哪个事件。

public Item StartEditing(Item item)
{
  Error.AssertObject((object) item, "item");
  if (!Settings.RequireLockBeforeEditing || Context.User.IsAdministrator)
    return item;
  if (this._context.IsAdministrator || StandardValuesManager.IsStandardValuesHolder(item) || !this.HasWorkflow(item) && !this.HasDefaultWorkflow(item) || !this.IsApproved(item))
    return this.Lock(item);
  Item obj = item.Versions.AddVersion();
  if (obj != null)
    return this.Lock(obj);
  else
    return (Item) null;
}

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

补丁项目:已保存的事件:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <events>
      <event name="item:saved">
        <handler type="PersonalizationDemo.Data.Items.ItemEventHandler, PersonalizationDemo" method="OnItemSaved"/>
      </event>
    </events>
  </sitecore>
</configuration>

实现自定义事件处理程序的类:

namespace PersonalizationDemo.Data.Items
{
    using Sitecore.Data.Items;
    using Sitecore.Events;
    using System;

    public class ItemEventHandler
    {
        protected void OnItemSaved(object sender, EventArgs args)
        {
            if (args == null) { return; }

            var sitecoreEventArgs = args as SitecoreEventArgs;
            if (sitecoreEventArgs == null) { return; }

            if (sitecoreEventArgs.Parameters.Length < 2) { return; }

            var item = sitecoreEventArgs.Parameters[0] as Item;
            if (item == null) { return; }

            if (!this.ShouldItemBeProcessed(item)) { return; }

            var itemChanges = sitecoreEventArgs.Parameters[1] as ItemChanges;
            if (itemChanges == null) { return; }

            if (itemChanges.FieldChanges.Contains(Sitecore.FieldIDs.Lock))
            {
                var fieldChange = itemChanges.FieldChanges[Sitecore.FieldIDs.Lock];
                if (fieldChange.Value == "<r />") { return; }

                this.CreateNewVersion(item);
            }
        }

        protected bool ShouldItemBeProcessed(Item item)
        {
            // TODO implement necessary check
            return true;
        }

        protected void CreateNewVersion(Item item)
        {
            var newItem = item.Versions.AddVersion();

            newItem.Editing.BeginEdit();
            // update necessary field if you need that
            newItem.Editing.EndEdit();
        }
    }
}