Skip to main content
2 of 2
Added code sample
UI Beardcore
  • 2.3k
  • 12
  • 13

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);
item.setPublication(publicationId);
item.setOrganizationalItem(orgItemId);
item.setDefaultSubType($const.ComponentSubType.COMPONENT);

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

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

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

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

    schema.loadInstanceData(orgItemId);
};

$evt.addEventHandler(item, "load", onLoad);
item.load(true);
UI Beardcore
  • 2.3k
  • 12
  • 13