2

I need to take the Component and Component Template tcm ids from the Component Presentation tab of any Tridion Page.

Scenario:

  • On the exisiting page in Tridion.
  • Go to "Component Presentation" tab.
  • When I click the "Insert" button, I want to iterate the Component and Component Template tcm id and display that in JavaScript alert box.

I am trying to find the exact javascript file/function to accomplish this, I couldn't find the exact one.

I am using SDL Tridion 2011 SP1.

Any help/suggestion would be highly appreciated.

EDIT

I am having this script which will fire when we wil click the Insert button in the "Insert component presentation" window.

Tridion.Cme.Views.InsertCpDialog.prototype.onInsertClick = 
                    function InsertCpDialog$onInsertClick()
{
  var p = this.properties;
  var c = p.controls;
  var templateId = c.templateSelect.getValue();
  var components = c.list.getSelection().getItems();

  p.componentPresentationsAdded += components.length;

  var compId = components[components.length - 1];
  var component = $models.getItem(compId);
  if (component)
  {
    var userSettings = Tridion.UI.UserSettings.getInstance();
    if (userSettings && userSettings.isLoaded())
    {
      Tridion.UI.UserSettings.setLastSelectedLocation(
          component.getPublicationId(), $const.ItemType.COMPONENT, p.contextUri);
    }
  }

  this.fireEvent("insert", {
    components: components,
    template: templateId
  });
};

So here I will get the tcm id of selected Component and template "compId" & "templateId", which is to be inserted. Now my question is: how can I check whether this component and template is already present on the Component Presentation Tab?

If somehow I will be able to get the id of all component and template already present on the page (from my "Insert" button) then I can compare them. But I am not getting any function which will give me those id. Here I got stuck.

EDIT

I am trying to go inside the page with these codes but i am not able to get the correct function which is getting fired on click of Insert, or which will return me the list of component and template id listed there.

Extensions.Test.prototype.isEnabled = function Test$isEnabled(selection) {
  try
  {
    console.log($controls);
    var masterTabControl = $controls.getControl($("#MasterTabControl"), 
                                            "Tridion.Controls.TabControl");

    console.log(masterTabControl);
    alert("Mastercontrol - ComponentpresentationsTab");
    console.log(masterTabControl.getPage("ComponentPresentationsTab"));
    console.log("list of component presentations");
    console.log(masterTabControl.getPage("ComponentPresentationsTab").
                             getListComponentPresentations().getItems());
    console.log("list of get xml");
    console.log(masterTabControl.getPage ("ComponentPresentationsTab").
                             getListComponentPresentations().getItems().getXml);
  }
  (catch exc)
  {
  }
  return true;
}

EDIT I got all the CP Id listed under CP TAB with this code. Thanks to @Frank van Puffelen

var p = this.properties;
var tgp = this.properties;
var c = p.controls;

var pageId = selection.getItem(0);
var masterTabControl = $controls.getControl($("#MasterTabControl"), 
                                            "Tridion.Controls.TabControl");
var compPresTab = masterTabControl.getPage("ComponentPresentationsTab");
var comPresList = p.compPresTab.getListComponentPresentations(); 
for (var i = 0; i <= myStringArray.length; i++) 
{     
  console.log(comPresList.getItems()[i].getComponentId());
  console.log(comPresList.getItems()[i].getComponentTemplateId());     
}
SDLBeginner
  • 829
  • 1
  • 7
  • 19
  • How about you show us the code that you already wrote and where you are stuck? – Frank van Puffelen Jul 20 '12 at 14:18
  • possible duplicate of [how to write a c program to add a button ](http://stackoverflow.com/questions/11446313/how-to-write-a-c-program-to-add-a-button) – Frank van Puffelen Jul 20 '12 at 14:18
  • If you don't provide more information about what ***you*** have already done, you run a good chance that this user account will get locked out of asking questions similar to what happened to @user1518281. Stack Overflow is not meant to turn your list of requirements into a working piece of code that you can copy/paste into a solution. We can ***help*** you do your work, but only if you show us what you've already done. – Frank van Puffelen Jul 20 '12 at 14:26
  • Did you write this code? It looks you copied standard `InsertCpDialog.onInsertClick` method. Do you understand what this code does? If not: did you set a breakpoint in it and step through it? You'll see that it gets access to a `controls` array that contains all the controls in the view (in this case the `InsertCpDialog` popup). This logic is applicable in any view, so you can just follow the trail. – Frank van Puffelen Jul 20 '12 at 21:31
  • No i didn't tell like i write this code.I got this while debugging the site and searching for the function which is getting triggered on click of "Insert"(on the InsertCpDialog Popup) button. So i am trying to do the same for the Insert button which is Present under CP Tab on page. So i am trying to dig inside – SDLBeginner Jul 21 '12 at 04:21
  • +1 for revising question and including code, though it might be hard to follow which snippets come from where. The first is from Anguilla, the second is your first attempt, and the third is what worked? – Alvin Reyes Aug 02 '12 at 00:26
  • Thanks Alvin for +1 :) , yes you are right . the codes which i pasted here are in same manner. – SDLBeginner Aug 02 '12 at 04:17

2 Answers2

3

Once you have the Component Presentations tab of the Page dialog, you can get the list of Component Presentations like this

comPresList = compPresTab.getListComponentPresentations();

You can inspect the list in XML format like this:

comPresList.getXml()

Or, as you seemingly are trying in your code snippet, you can get the items themselves using:

comPresList.getItems()

This will give you a list of Tridion.ContentManager.ComponentPresentation objects, each of which you can call getComponentId and getComponentTemplateId on.

comPresList.getItems()[0].getComponentId()
comPresList.getItems()[0].getComponentTemplateId()

I got this information by taking the snippet that John Winter provided you earlier, pasting it into a Chrome JavaScript console and then doing a few text searches on the source tree (on disc) for key phrases such as "insert" (to find all handlers for the insert event) and ComponentPresentation = (to find the ComponentPresentation class).

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
2

I answered this one here: restrict user to insert same component and template

It seems the question has been changed since so the answer now doesn't seem related, but i'm sure this will help you figure out what you need to do.

Community
  • 1
  • 1
johnwinter
  • 3,624
  • 15
  • 24
  • Thanks john, i can see tht script there, that will be helpfull to me.one thing how can i loop it so that i can get ids of more than one component present on the page under the component presentation tab,and the second important thing i need to bring in your attension that, **i want to fire this javascript on the click of "Insert" button on the page under component Presentation tab**. – SDLBeginner Jul 20 '12 at 13:42
  • 2
    Are you asking us to do you job? Most of us will gladly work for you or your organization, and help you with this. So far I think you asked this question about 4 times, and you don't seem to show real progress. Have you - for instance - tried to figure out which events are fired when? Which views or controls have you looked at? What have you found so far? – Nuno Linhares Jul 20 '12 at 14:37
  • Sorry Nuno and frank. plz dont get me wrong. I edited my question and i shared my js. – SDLBeginner Jul 20 '12 at 17:30