防止插入相同的组件和模板组合:

时间:2012-07-19 06:05:09

标签: tridion tridion-2011

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

修改

当我将它解压到Page时,我得到了错误。我的代码:

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)
    {

    }
} 

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:2)

您为什么订阅组件?我认为它应该是Page。然后,您可以浏览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;
}

答案 2 :(得分: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)
{
}