I am working on an extension that must create a component. Until now, my code looks like this:

var item = $models.createNewItem($const.ItemType.COMPONENT);
item.setTitle("Test Title");

$evt.addEventHandler(item, "save", HandlerSave);
$evt.addEventHandler(item, "savefailed", HandlerSaveFailed);
$evt.addEventHandler(item, "validatefailed", HandlerValidateFailed);


The error I get is: Unable to load XML: XML document must have a top level element.. Unable to update Component (tcm:0-0-0).

I think the problem is creating the xml. I tried with setXml/setContent..., but I don't know how to build the required xml for the component.


The thing is that you should have new component Xml set to the component before you can save it. Imagine you have a schema with default values associated with folder where this component (will)resides.

You should create component (UI side, just like you did) then load it (it will load xml of created item) and only then do save.

increment = 0;

var publicationId = "tcm:0-xx-1";
var orgItemId = "tcm:xx-yy-2";
var schemaId = "tcm:xx-zz-8";

var item = $models.createNewItem($const.ItemType.COMPONENT);

var onLoad =  function() {
    $evt.removeEventHandler(item, "load", onLoad);
    var schema = $models.getItem(schemaId);

    var onInstanceDataLoad =  function()
        $evt.removeEventHandler(schema, "instancedataload", onInstanceDataLoad);

        item.setTitle("Test Title " + (increment++));
        item.setContent(schema.getInstanceData(orgItemId, true));

   $evt.addEventHandler(schema, "instancedataload", onInstanceDataLoad);


$evt.addEventHandler(item, "load", onLoad);
  • There is nothing created through UI, everything happens on a button click. I tried to use load method of the newly created component, it fires the loading event but it remains unloaded. The only things I modify through UI are some content fields, but I need the rest of the xml of the component which I cannot reach/generate. – Bogdan Spark Sep 25 '15 at 11:27
  • I`ve added a code sample. Please check. – UI Beardcore Sep 25 '15 at 15:03
  • Thank you very much. That helped a lot. Didn't knew the orgItem and schema must be in the same publication. Also, the instancedataload event was pure gold ^^. – Bogdan Spark Sep 29 '15 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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