1

I am facing this duplicate item issue while creating the item in Sitecore programmatically. I have exposed one custom API end point which is being called by another application. In this API, based on request received, I am creating the items in Sitecore.

In normal way everything works file, Sitecore does not allow to create the duplicate items. But issue comes when API is called twice with same payload at same time. In this Scenario Sitecore is not able to figure out if its a duplicate item or not and that where it end up creating duplicate items in content tree. Below setting is already set to false

<setting name="AllowDuplicateItemNamesOnSameLevel" value="false" />

Note : This issue does not occur if I call my Sitecore API end point at different time with same payload.

Sample Code

using (new Sitecore.SecurityModel.SecurityDisabler())
{
 
  Sitecore.Data.Database master = Sitecore.Data.Database.GetDatabase("master");
  Items.TemplateItem template = master.GetItem("/sitecore/templates/Sample/Sample Item");
  Item parentItem = master.GetItem("/sitecore/content/home");
  if(!CheckItemExists("/sitecore/content/home/NameOfNewItem")){
      Item newItem = parentItem.Add("NameOfNewItem", template);
      newItem.Editing.BeginEdit();
      newItem.Fields["Title"].Value = "NewValue1";
      newItem.Fields["Text"].Value = "NewValue2";
      newItem.Editing.EndEdit();
  }
}

Thanks in advance!

5
  • Can you provide sample code as to how the new item is being created? Do you have a check in place to see if an item of the same name already exists? Apr 24 at 15:48
  • Yes I do have a check in place, but problem is with concurrent requests. Two concurrent requests are coming to create same item. This check will return that item does not exists in content tree, so process will go ahead and create the item. Added the sample code in question
    – Neha Gupta
    Apr 24 at 16:03
  • @NehaGupta Did you tried adding lock on your method so that call to your function runs only after the previous lock is released?
    – ckhanna
    Apr 24 at 16:28
  • Is this a likely scenario? What is calling the API? Seems like a problem you need to code for using a lock as suggested.
    – Richard Seal
    Apr 24 at 16:44
  • Maybe you should look into your flow. Why would multiple calls cause duplicate items. You could also work with a buffer. Store your data in a queue and handle that queue 1 by 1.
    – Danny Paul
    Apr 24 at 18:47

1 Answer 1

3

You can implement createItem function using lock as below. This will ensure thread safety of the item creation code.

 private static readonly object padlock = new object();
 public void CreateItem()
 {
   lock (padlock)
   {
     using (new Sitecore.SecurityModel.SecurityDisabler())
     {
      // code to create item goes here.
      }
    }
  }

I have tested the flow on JMeter by running concurrent threads, duplicate items are not getting created after integrating lock for thread safety as above.

Hope this helps!!!

2
  • It did not help, I was able to create duplicate items
    – Neha Gupta
    Apr 30 at 14:39
  • @NehaGupta can you please share the code in case possible..I tested a my end through Jmeter running concurrent threads. its working fine..
    – ckhanna
    Apr 30 at 15:19

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.