如何在没有工作流程的情况下创建 sitecore 项目版本?
How to create sitecore item version without workflow?
我在 Sitecore 中遇到了业务需求,其中对于少数项目我们不需要附加工作流,但我们希望在锁定编辑项目时创建新版本。
我无法应用以下设置,因为它会影响所有项目并且我的工作流程将无用。
<setting name="RequireLockBeforeEditing" value="true"/>
我也经历了 Whosebug 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;
}
尝试这样的事情:
补丁 item:saved 事件:
<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>
实现自定义事件处理程序 class:
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();
}
}
}
我在 Sitecore 中遇到了业务需求,其中对于少数项目我们不需要附加工作流,但我们希望在锁定编辑项目时创建新版本。
我无法应用以下设置,因为它会影响所有项目并且我的工作流程将无用。
<setting name="RequireLockBeforeEditing" value="true"/>
我也经历了 Whosebug 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;
}
尝试这样的事情:
补丁 item:saved 事件:
<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>
实现自定义事件处理程序 class:
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();
}
}
}