如何停止Sitecore中引用项的存档(计划)?

如何停止Sitecore中引用项的存档(计划)?,sitecore,sitecore6,Sitecore,Sitecore6,当我手动归档一个被其他项目引用的项目时,Sitecore弹出对话框,其中包含操作–如何处理链接 如果将项目配置为使用“设置存档日期”自动存档,并且该项目已存档,则Sitecore似乎在默认情况下选择“保留链接”操作,因此指向存档项目的所有链接都将断开 如何/在何处连接以停止归档其他项目引用的项目(计划归档)?我希望停止归档,并与归档不成功的人建立融洽的关系。为了防止Sitecore归档链接的项目,您需要覆盖2个类。 它们中的第一个是ArchiveItem,因此它在归档之前检查项目是否链接: na

当我手动归档一个被其他项目引用的项目时,Sitecore弹出对话框,其中包含操作–如何处理链接

如果将项目配置为使用“设置存档日期”自动存档,并且该项目已存档,则Sitecore似乎在默认情况下选择“保留链接”操作,因此指向存档项目的所有链接都将断开


如何/在何处连接以停止归档其他项目引用的项目(计划归档)?我希望停止归档,并与归档不成功的人建立融洽的关系。

为了防止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;
        }
    }
}
您需要覆盖的第二个类是SqlServerTaskDatabase,因此它调度被覆盖的MyArchiveItem任务,而不是原始SitecoreArchiveItem

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);
            }
        }
    }
}
<TaskDatabase type="My.Assembly.And.Namespace.MySqlServerTaskDatabase, My.Assembly">
    <param connectionStringName="core"/>
</TaskDatabase>
您需要做的最后一件事是更新Sitecore配置,使其指向MySqlServerTaskDatabase:

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);
            }
        }
    }
}
<TaskDatabase type="My.Assembly.And.Namespace.MySqlServerTaskDatabase, My.Assembly">
    <param connectionStringName="core"/>
</TaskDatabase>
然后查找文件/sitecore/shell/applications/dialogs/archive item/archive date.xml。将第6行更改为指向新类:

<CodeBeside Type="My.Assembly.And.Namespace.MyArchiveDateForm,My.Assembly" />


就这样。无论何时尝试安排对链接项目的存档,Sitecore都会显示无法存档该项目的信息。

感谢Maras,这是一个很好的解决方案,但“在设置时间表后链接项目”的情况仍然存在问题。我找到了解决此问题的解决方案。见上面更新的答案。