4
votes

When you insert an item in Sitecore you use a code like this:

Item newItem = parentItem.Add("NameOfNewItem", template);

Then you edit the item.

Then you save the item.

If an error happens in the middle, AFTER the parentItem.Add instruction, the fields on your new item are not saved, but the child item is created and placed in the content tree anyway, so we have an incomplete item in the content tree (really wrong).

Is there anyway to insert an item on this lines?

  1. create an item
  2. populate the item
  3. finally, add the populated item to the content tree.

In other words is there any way to put the parentItem.Add instruction at the end of the whole process?

That would really help to improve data integrity.

2

2 Answers

1
votes

You are basically asking for a way to use transactions for the creation of the item.

This isn't supported with Sitecore 6. It was mentioned (a couple years ago) that Sitecore 7 will support transactions.

2
votes

Out of the box I don't believe transactions are supported. There's a good article on Sitecore and Item transactions here which implements a Unit of Work like approach to give an approximation of atomic actions on items.

The Sitecore kernel assembly internally provides transactional support to operations that require it. The API doesn't appear to expose this capability or allow access to the database IDbConnection.

public virtual DataProviderTransaction CreateTransaction();

Declaring Type: Sitecore.Data.DataProviders.Sql.SqlDataApi 
Assembly: Sitecore.Kernel, Version=6.0.0.0