2

在一个页面中,当我们单击组件演示选项卡时,我们可以看到那里列出的组件和模板。单击其下方的插入按钮,它将打开另一个窗口“插入组件演示”,我们还将有插入和关闭按钮。所以现在我需要做什么在插入时我需要检查所选组件和模板的组合是否已经存在于页面上。如果是,那么它应该防止插入相同的弹出窗口,如“此组合已存在,选择其他组件”。知道我该如何继续。如何在插入按钮上触发 Javascript?

编辑:

当我订阅页面时,我得到了错误。我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text;
using Tridion.ContentManager.Extensibility.Events;
using Tridion.ContentManager.Extensibility;
using Tridion.ContentManager.ContentManagement;
using System.IO;
using System.Windows.Forms;




namespace MyEventHandlers
{
[TcmExtension("MyEventHandlerExtension")]

public class MyEventHandler : TcmExtension 
{
    public MyEventHandler()
{
  Subscribe();
}

public void Subscribe()
{
    EventSystem.Subscribe<Page, SaveEventArgs>(SaveBtnInitiated, EventPhases.Initiated);

}

private void SaveBtnInitiated(Page subject, SaveEventArgs args, EventPhases phase)
{

    try
    {
        List<string> allcplist = new List<string>();
        List<string> allcplist = new List<string>();
        foreach (ComponentPresentation cp in subject.ComponentPresentations)
        {
            allcplist.Add(cp.Component.Id + "," + cp.ComponentTemplate.Id);   
        }
        List<string> uniquecplist = allcplist.Distinct().ToList();
        if (allcplist.Count != uniquecplist.Count)
        {
            subject.Checkin(false);
            throw new Exception("Page has duplicate component presentation");

    }
    catch(Exception)
    {

    }
} 
4

3 回答 3

4

您可以在订阅 Page Save 事件和 Initiated 阶段的事件处理程序中实现这一点。当存在重复的组件表示时,您可以通过抛出异常来取消保存。该消息将显示在 TCM Explorer 的消息中心。

于 2012-07-19T07:17:03.150 回答
2

为什么要订阅组件?我认为它应该是页面。然后,您可以步行穿过该ComponentPresentations物业。

当发现重复演示时,遍历组件演示并抛出异常的代码:

foreach (var cpA in subject.ComponentPresentations)
{
    if (subject.ComponentPresentations.Where(cpB => ComponentPresentationsAreEqual(cpA, cpB)).ToList().Count() > 2)
    {
        throw new DuplicateComponentPresentationsEmbeddedOnPageException();
    }
}

当 cpB 等于 cpA 时,将 cpB 包含在列表中的函数:

function ComponentPresentationsAreEqual(ComponentPresentation cpA, ComponentPresentation cpB)
{
    return cpA.Component.Id == cpB.Component.Id && cpA.ComponentTemplate.Id == cpB.ComponentTemplate.Id;
}
于 2012-07-25T11:29:35.580 回答
0

感谢@Arjen Stobbe,我得到了这段代码的结果

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text;
using Tridion.ContentManager.Extensibility.Events;
using Tridion.ContentManager.Extensibility;
using Tridion.ContentManager.ContentManagement;
using System.IO;
using System.Windows.Forms;




namespace MyEventHandlers
{
[TcmExtension("MyEventHandlerExtension")]

public class MyEventHandler : TcmExtension 
{
    public MyEventHandler()
{
  Subscribe();
}

public void Subscribe()
{
    EventSystem.Subscribe<Page, SaveEventArgs>(SaveBtnInitiated, EventPhases.Initiated);

}

private void SaveBtnInitiated(Page subject, SaveEventArgs args, EventPhases phase)
{

    try
    {
        List<string> allcplist = new List<string>();
        List<string> allcplist = new List<string>();
        foreach (ComponentPresentation cp in subject.ComponentPresentations)
        {
            allcplist.Add(cp.Component.Id + "," + cp.ComponentTemplate.Id);   
        }
        List<string> uniquecplist = allcplist.Distinct().ToList();
        if (allcplist.Count != uniquecplist.Count)
        {
            subject.Save(false);
            throw new Exception("Page has duplicate component presentation");

    }
    catch(Exception)
    {

    }
} 

但我不会删除页面上存在的重复 CP。需要补充吗,

for each()

里面

if (allcplist.Count != uniquecplist.Count)
{
}
于 2012-07-26T06:50:39.953 回答