2012-07-19 33 views
2

在一个页面时,我们将点击组件外观选项卡我们可以看到仅低于插入按钮组件和模板中列出there.On点击,它会打开另一窗口“插入组件演示”还有,我们将有插入,靠近button.So现在我需要做的虽然插入我需要检查选择的组件和模板的组合是否已经存在有页面,或在不。如果是,那么它应该防止插入相同的弹出窗口,如“这个组合已经存在,选择其他组件”。 任何想法我可以如何继续。如何在插入按钮上触发Javascript?防止插入组件的相同组合模板:

编辑:

时,我就subscrbing页我得到erro.My代码:

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

    } 
} 
+0

为什么你签的页面?当你在启动阶段运行这个处理程序时,抛出异常将取消该操作。 除此之外,您通过捕获异常并且不做任何事情来取消异常。 – 2012-07-25 12:37:09

+0

我删除了签入并使用保存。和它的工作。谢谢你arjen – SDLBeginner 2012-07-26 06:51:33

回答

2

你为什么订阅组件?我认为它应该是页面。然后你可以步行通过ComponentPresentations属性。

代码要经过组件演示,并抛出一个异常时,重复演示文稿发现:

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

,功能包括CPB在列表中时,它等于CPA:

function ComponentPresentationsAreEqual(ComponentPresentation cpA, ComponentPresentation cpB) 
{ 
    return cpA.Component.Id == cpB.Component.Id && cpA.ComponentTemplate.Id == cpB.ComponentTemplate.Id; 
} 
+0

谢谢arjen ...我在订阅页面时出错。 – SDLBeginner 2012-07-25 12:08:42

+0

什么是错误? (页面主题,SaveEventArgs eventArgs,EventPhases阶段)' – 2012-07-25 12:34:00

+0

'其实我使用的是“使用System.Web.UI”,这是“使用System.Web.UI”为什么它显示那个错误。 – SDLBeginner 2012-07-25 12:48:44

4

您可以在订阅的页面保存事件的事件处理函数中实现此和初始阶段。当有重复的组件演示时,您可以通过抛出异常来取消保存。该消息将显示在TCM Explorer的消息中心中。

+0

谢谢Arjen对你的回应...你能解释我怎么可以订阅事件处理程序到页面保存事件。 – SDLBeginner 2012-07-19 07:26:15

+1

有一篇关于SDL Tridion World的2011年事件系统的文章:http://sdltridionworld.com/articles/sdltridion2011/sdltridion2011eventsystem.aspx,还有一个有很多有用信息的示例事件系统也可以在那里找到http:// sdltridionworld.com/community/2011_extensions/rapideditorialinterface.aspx – 2012-07-19 07:49:37

+0

嗨巴特,感谢您的时间。但概率是我无法访问下面的链接中提供的PDF。请清除我的疑问,我们是否可以在插入按钮上触发一个JavaScript,它将在插入时检查duplicateat组件。因为我需要检查用户是否选择相同的组合“**仅插入**时” – SDLBeginner 2012-07-19 09:17:31

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) 
{ 
} 
+1

也许问这是一个单独的问题 - 在评论中很难回应,也因为原来的问题已经有了答案。 :-) – 2012-08-02 00:34:15