i am wondering why did i could create the same Item repeatedly and remove the same item repeatedly on the same requests. Does Sitecore automatically commits database changes on the end of requests?

        Database master = Database.GetDatabase("master");
        Item folder = master.GetItem(new ID("{098D8157-4B17-4061-A392-12EABFDFA13F}"));

        TemplateID templateId = new TemplateID(new ID("{A87A00B1-E6DB-45AB-8B54-636FEC3B5523}"));
        ID id = new ID("ec02041d-a4d0-478e-920e-4a2a0e48d51c");
        Item i = folder.Add("testItem", templateId, id);
        folder.Database.Engines.DataEngine.CreateItem("testItem", folder, templateId.ID, id);
        i = folder.Add("testItem", templateId, id);
        i = folder.Add("testItem", templateId, id);
        i = folder.Add("testItem", templateId, id);
        i = folder.Add("testItem", templateId, id);
        i = folder.Add("testItem", templateId, id);
        i = folder.Add("testItem", new TemplateID(new ID("{BA3DAAD7-CB67-4BDC-B623-F25C96E785B7}")), id);

The code above runs smoothly and no exception - I was expecting an exception since I am adding the same Item with the same ID more than once.

  • Does Sitecore automatically commit the above code at end of request or some 'context'?
  • Is it perhaps the DataEngine is designed that way so that user wont accidentally delete non-existent item and caught an exception?
  • How could i 'commit' those changes manually?

Fiddling the api a bit do no luck..

  • 1
    This does not answer your specific question regarding Item API behaviour - however you are going about this the wrong way. Take a look at this post. sitecore.stackexchange.com/questions/10344/… – Mark Cassidy May 14 '18 at 9:20
  • actually, i'm thinking of doing this just like git index.lock file. that suggested answer did not prevent the scheduled task ran by different application pool somewhere in the cluster.. i do wonder why that important part was edited out.. perhaps i was unclear.. – Bagus Tesa May 14 '18 at 9:50
  • No, it was clear. But it's not relevant to your question - you're asking about Item API behaviour; the reasons WHY you need it and how else to maybe solve that, would be a second separate question. – Mark Cassidy May 14 '18 at 11:16
  • alright, pardon me for messing the entire question. i do wonder if the api is designed to be always fail safe or is there any way to alter the current behavior. – Bagus Tesa May 14 '18 at 14:36
  • I don't think it cares if I'm honest. Adding an item with a specific ID is - behind the scenes really "Add an item with this ID if it doesn't exist" and "Add a version to the aforementioned item". So from the Item API perspective, you can add an "item" more than once, it's perfectly legal. And will be ignored. If no one else jumps at it, I should be able to come up with a more clarifying answer this evening. – Mark Cassidy May 14 '18 at 14:39

Your Answer

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

Browse other questions tagged or ask your own question.