Skip to main content
Added code sample
Source Link
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);

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.

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);
Source Link
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.