1

i'm trying to create a new component using the TOM.NET API. i managed to find a block of code which seems to fit with my expectations but i have some questions:

        TDSE tdse = new TDSE();
        tdse.Impersonate(@"tridion\aramet");
        tdse.Initialize();

        Component myNewComp = new Component();
        myNewComp.Title = "hello";
        myNewComp.Schema = 
        myNewComp.Content = 
        myNewComp.OrganizationalItem = 
        myNewComp.Save(true);

  1. What is the right syntax to set the Schema property (the tcm of my schema )
  2. Problem with the Content property (is this supposed to be a XDocument ? what about the structure?)

    Error 1 'Tridion.ContentManager.Interop.TDS.Component' does not contain a definition for 'Content' and no extension method 'Content' accepting a first argument of type 'Tridion.ContentManager.Interop.TDS.Component' could be found (are you missing a using directive or an assembly reference?) C:\Users\aramet\Documents\Visual Studio 2008\Projects\APITOMNET\APITOMNET\WebForm1.aspx.cs 23 23 APITOMNET

  3. OrganizationalItem property seems to be used to provide the folder in which i want my component to be saved, what is the right syntax ?
Thanks in advance for your help.

3

1 Answer 1

2

In SDL Tridion 2009, you need to use the TOM (Interop) API to create new items, this differs from the TOM.NET API (since the TOM.NET API in 2009 was read-only).

So you have to work from the TDSE like this:

using System.Runtime.InteropServices;
using Tridion.ContentManager.Interop.TDS;  
using Tridion.ContentManager.Interop.TDSDefines;
using ItemType = Tridion.ContentManager.Interop.TDSDefines.ItemType;

{
  TDSE tdse = new TDSE();
  tdse.Impersonate(@"domain\user");
  tdse.Initialize();

  // read Schema tcm:1-3-8
  Schema schema = (Schema)tdse.GetObject("tcm:1-3-8", EnumOpenMode.OpenModeView, Constants.URINULL, XMLReadFilter.XMLReadData);

 // create new Component, in Folder tcm:1-2-2
  Component comp = (Component)tdse.GetNewObject(ItemType.ItemTypeComponent, "tcm:1-2-2", Constants.URINULL);

  comp.Title = "new title";
  comp.Schema = schema;
  // etc. follow the examples in the TOM API guide, don't mix this with the TOM.NET API

  comp.Save(true);
  if (comp.Info.IsCheckedOut)
  {
     comp.CheckIn();
  }

  // release objects
  ReleaseCom(schema);
  ReleaseCom(comp);
  ReleaseCom(tdse);
}

private static void ReleaseCom(object instance)
{
  if (instance != null)
  {
    try
    {
      Marshal.ReleaseComObject(instance);
    }
    catch
    {
      /* log potential memory leak */
    }
  }
}

You really need to only look at the TOM API guide, and don't even include the TOM.NET DLLs as references in our project, since that will just confuse you.

Note the Component class used in your example and mine are different, I'm using a Tridion.ContentManager.Interop.TDS.Component, and that is also what you need to use. Don't use a Tridion.ContentManager.CommunicationManagement.Component, since that will be a read-only object.

For more examples on how to work with the TOM API, you could take a look at the old powertools, these are written in ASP VBScript, and you will need to translate that to TOM Interop (C#), but it should already give you a general idea.

4
  • Unfortunately, i got this COMException : <?xml version="1.0"?> <tcm:Error xmlns:tcm="tridion.com/ContentManager/5.0" ErrorCode="80040325" Category="3" Source="Kernel" Severity="2"><tcm:Line ErrorCode="80040325" Cause="false" MessageID="16137"><![CDATA[Unable to save Component (tcm:0-0-0).]]><tcm:Token>RESID_4574</tcm:Token><tcm:Token>RESID_4418</tcm:Token><tcm:Token>tcm:0-0-0</tcm:Token></tcm:Line><tcm:Line ErrorCode="80040325" Cause="true" Moreover my schema and components objects seems to be empty
    – Arn a
    Commented Feb 4, 2015 at 16:56
  • More info on those type of exceptions can be found in the Tridion event log on the server. If you just used my code and nothing more, chances are your Schema has mandatory fields (which have not been set). Like I mentioned in the comment, you will need to set the fields values etc. But please don't put all your errors in a comment, just edit your question with these kind of updates so I can update my answer. Else this question and its answer will get very messy with unreadable comments. Commented Feb 5, 2015 at 9:03
  • Note that using COM+ interop requires you to always release your COM handles so I recommend using blocks around them to guarantee the handles are released: using (ComCleanup cc = new ComCleanup()) { TDSE tdse = new TDSE(); cc.Register(tdse); //other code here } Commented Feb 5, 2015 at 13:12
  • I solved the problem. The folder in which i want to save my component and the schema had not the same publication ID. Thanks for your advices
    – Arn a
    Commented Feb 6, 2015 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.