Sitecoreがリンクされたアイテムをアーカイブしないようにするには、2つのクラスをオーバーライドする必要があります。それらの最初はArchiveItemであるため、アーカイブする前にアイテムがリンクされているかどうかをチェックします。
namespace My.Assembly.And.Namespace
{
public class MyArchiveItem : Sitecore.Tasks.ArchiveItem
{
public MyArchiveItem(System.DateTime taskDate) : base(taskDate)
{
}
public override void Execute()
{
using (new Sitecore.SecurityModel.SecurityDisabler())
{
lock (SyncRoot)
{
Sitecore.Data.Items.Item item = GetItem();
if (item != null && HasLink(Sitecore.Globals.LinkDatabase, item))
{
Sitecore.Diagnostics.Log.Error(string.Format(
"Item {0} or one of its descendants are linked from other items. "
+ "Remove link before scheduling archive.", item.Paths.FullPath), this);
// uncomment next line if you don't want to retry archiving attempt
//Sitecore.Globals.TaskDatabase.Remove(this);
return;
}
}
}
base.Execute();
}
private static bool HasLink(Sitecore.Links.LinkDatabase linkDatabase, Sitecore.Data.Items.Item item)
{
Sitecore.Links.ItemLink[] referrers = linkDatabase.GetReferrers(item);
if (referrers.Length > 0)
{
if (referrers.Any(link => link.SourceFieldID != Sitecore.FieldIDs.Source))
{
return true;
}
}
foreach (Sitecore.Data.Items.Item item2 in item.Children)
{
if (HasLink(linkDatabase, item2))
{
return true;
}
}
return false;
}
}
}
オーバーライドする必要がある2番目のクラスはSqlServerTaskDatabaseであるため、元のSitecore ArchiveItemではなく、オーバーライドされたMyArchiveItemタスクをスケジュールします。
namespace My.Assembly.And.Namespace
{
public class MySqlServerTaskDatabase : Sitecore.Data.SqlServer.SqlServerTaskDatabase
{
public MySqlServerTaskDatabase(string connectionString) : base(connectionString)
{
}
public override void UpdateItemTask(Sitecore.Tasks.Task task, bool insertIfNotFound)
{
Sitecore.Data.Sql.SqlBatch batch = new Sitecore.Data.Sql.SqlBatch(true);
BindTaskData(task, batch);
string sql = GetUpdateSql() +
" WHERE [ItemID] = @itemID AND [Database] = @databaseName AND [taskType] = @taskType";
batch.AddSql(sql);
if (insertIfNotFound)
{
AddInsertTask(batch, true);
}
batch.Execute(ConnectionString);
}
protected new virtual void BindTaskData(Sitecore.Tasks.Task task,
Sitecore.Data.Sql.SqlBatch batch)
{
System.DateTime taskDate = task.TaskDate;
if (taskDate == System.DateTime.MinValue)
{
taskDate = (System.DateTime)System.Data.SqlTypes.SqlDateTime.MinValue;
}
batch.AddParameter("taskID", task.ID);
batch.AddParameter("nextRun", taskDate);
if (task is Sitecore.Tasks.ArchiveItem)
{
batch.AddParameter("taskType",
Sitecore.Reflection.ReflectionUtil.GetTypeString(typeof(MyArchiveItem)));
}
else
{
batch.AddParameter("taskType", ReflectionUtil.GetTypeString(task.GetType()));
}
batch.AddParameter("parameters", task.Parameters);
batch.AddParameter("recurrence", task.RecurrencePattern);
batch.AddParameter("itemID", task.ItemID);
batch.AddParameter("databaseName", task.DatabaseName);
if (string.IsNullOrEmpty(task.InstanceName))
{
batch.AddParameter("instanceName", System.DBNull.Value);
}
else
{
batch.AddParameter("instanceName", task.InstanceName);
}
}
}
}
最後に行う必要があるのは、MySqlServerTaskDatabaseを指すようにSitecore構成を更新することです。
<TaskDatabase type="My.Assembly.And.Namespace.MySqlServerTaskDatabase, My.Assembly">
<param connectionStringName="core"/>
</TaskDatabase>
失敗したアーカイブの試行に関する情報は、ログファイルに保存されます。この部分を更新して、カスタムレポートに保存することをお勧めします。
以下に、元の問題が機能するために必要ではない追加情報を示します。
以下に説明するようにスケジュールが設定される前にフックして、アイテムがアーカイブされないことをユーザーに通知することもできます。
まず、ArchiveDateFormクラスをオーバーライドするクラスを作成します。
namespace My.Assembly.And.Namespace
{
public class MyArchiveDateForm
: Sitecore.Shell.Applications.Dialogs.ArchiveDate.ArchiveDateForm
{
protected override bool SetItemArchiveDate
(Sitecore.Data.Items.Item item, string value)
{
if (HasLink(Sitecore.Globals.LinkDatabase, item))
{
Sitecore.Web.UI.Sheer.SheerResponse.Alert(
"Item or one of its descendants are linked from other items. "
+ "Remove link before scheduling archive.", new string[0]);
return false;
}
return base.SetItemArchiveDate(item, value);
}
private static bool HasLink(Sitecore.Links.LinkDatabase linkDatabase,
Sitecore.Data.Items.Item item)
{
Sitecore.Links.ItemLink[] referrers =
linkDatabase.GetReferrers(item);
if (referrers.Length > 0)
{
if (referrers.Any(
link => link.SourceFieldID != Sitecore.FieldIDs.Source))
{
return true;
}
}
foreach (Sitecore.Data.Items.Item item2 in item.Children)
{
if (HasLink(linkDatabase, item2))
{
return true;
}
}
return false;
}
}
}
次に、ファイル/ sitecore / shell / Applications / dialogs / archive item /archivedate.xmlを見つけます。6行目を新しいクラスを指すように変更します。
<CodeBeside Type="My.Assembly.And.Namespace.MyArchiveDateForm,My.Assembly" />
以上です。リンクされたアイテムのアーカイブをスケジュールしようとすると、Sitecoreはアイテムをアーカイブできないという情報を表示します。