CRUD operations with Sitecore (Sitecore API, Glass and Fortis) – Part One

So in this article we start our series discussion of how you would work with Sitecore.Data.Items.Item and perform CRUD (Create, Read, Update and Delete) operations on those items.  I will also cover how you would do these same operations using Glass and Fortis which are both Object Relational Mappers (ORM).

CRUD (Create)

First we’ll start out by discussing how you would Create a new item.  The example below shows how you would create an item.  There isn’t much error checking in this example, but your code should check to see if the current template item is set and that the parent item isn’t null either.  Else your code with throw an unfriendly error to the user of the site, which isn’t ideal.  The second thing to notice, is that we are calling ItemUtil.ProposeValidItemName, this helper method will take a string that you pass in, and spit out a Item Name that will work with Sitecore (in other words, it’ll strip out special characters and more).

Creating a new item is a fairly straight forward approach, the same can be said for creating an item within Glass Mapper.  The example below is how you would do this.  Very similar to the above approach, however you do not need to specify the template, that is automatically done by Glass when you create an item, because it can determine this from the POCO model you are using.  Also if you are working with TDS and Glass, or just basic Glass without auto-generating your Glass models, then you will need to ensure the model has some sort of Item name that you can specify.  If you are auto-generating the glass models using TDS, you would want to extend the GlassBase, to include a property for ItemName.

Finally, if you haven’t even used Glass, there is luckily other options for working with Sitecore.  Another popular ORM is Fortis, which has alot of similarities to Glass.  Let’s quickly take a look at how you would achieve creating a new item within Fortis:

That’s it for this part, please feel free to share your thoughts on these methods for creating a new item.  I’m thinking I might write more on how to create items using other ORM’s or Object Mappers (like Synthesis) similar to Glass for Sitecore soon so this blog series will be your one stop shop for all references related to creating items.