首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何停止Sitecore中引用项的存档(计划)?

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

Stack Overflow用户
提问于 2013-01-26 06:23:31
回答 1查看 552关注 0票数 1

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

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

如何/在何处连接才能停止其他项目引用的项目的归档(计划归档)?我想停止归档,并创建一些关系,表明归档没有成功。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-26 19:50:23

为了防止Sitecore存档链接的项目,你需要重写2个类。第一个是ArchiveItem,因此它会在存档之前检查项目是否已链接:

代码语言:javascript
复制
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任务,而不是原始的Sitecore ArchiveItem

代码语言:javascript
复制
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);
            }
        }
    }
}

您需要做的最后一件事是更新Sitecore配置以指向MySqlServerTaskDatabase

代码语言:javascript
复制
<TaskDatabase type="My.Assembly.And.Namespace.MySqlServerTaskDatabase, My.Assembly">
    <param connectionStringName="core"/>
</TaskDatabase>

有关失败的存档尝试的信息将存储在日志文件中。您可能需要更新此部件以将其存储在您的自定义报告中。

下面是一些附加信息,这些信息对于您的原始问题不是必需的。

您还可以在如下所述设置时间表之前挂钩,以通知用户该项目将不会被存档。

首先创建将覆盖ArchiveDateForm类的类:

代码语言:javascript
复制
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 date.xml文件。将第6行更改为指向新类:

代码语言:javascript
复制
<CodeBeside Type="My.Assembly.And.Namespace.MyArchiveDateForm,My.Assembly" />

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14531308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档