Pages

15 March, 2012

How to edit an Item in Code Behind (Sitecore v6.5) – 5

Sitecore will throw exception when editing an item if the item is not in editing mode. 
There are two ways where you can put an item in editing mode.
1.       Sitecore.Data.Items.ItemEditing
2.       Sitecore.Data.Items.EditContext
Scenario
Sitecore.Data.Items.ItemEditing
Sitecore.Data.Items.EditContext
To begin editing
Item.Editing.BeginEdit()
Using(new Sitecore.Data.Items.EditContext(item))
To Commit/End Changes
Item.Editing.EndEdit()
If previous statement passes, automatically item changes will be committed.
To Cancel Edit
Item.Editing.CancelEdit()
If previous statement fails, automatically item changes will be rolled back.
 
1.       Sitecore.Data.Items.ItemEditing
Unless and until, we call EndEdit() or CancelEdit(), Sitecore does not commit the changes.
 
            //Getting Master Database
            Sitecore.Data.Database masterDB = Sitecore.Configuration.Factory.GetDatabase("master");
            //Getting a Sitecore Item
            Sitecore.Data.Items.Item home = masterDB.GetItem("/sitecore/content/Home");
            //Begin Editing Sitecore Item
            home.Editing.BeginEdit();
            try
            {
                home["Title"] = "Title from Code";
                //Commit the changes
                home.Editing.EndEdit();
            }
            catch (Exception)
            {
                //Revert the Changes
                home.Editing.CancelEdit();
            }
 
2.       Sitecore.Data.Items.EditContext
Closure of Using in EditContext will implicitly commit any changes within that segment of code and also calls dispose().
 
            //Getting Master Database
            Sitecore.Data.Database masterDB = Sitecore.Configuration.Factory.GetDatabase("master");
            //Getting a Sitecore Item
            Sitecore.Data.Items.Item home = masterDB.GetItem("/sitecore/content/Home");
            //Using EditContext to edit an Item
            using (new Sitecore.Data.Items.EditContext(home))
            {
                home["Text"] = "Modified Text from Code";
            }

13 March, 2012

How to access Sitecore Items in Code Behind (Sitecore v6.5) – 4

To get a Sitecore Content Item, use Sitecore.Data.Database.GetItem(Path)
­Sitecore Content Item Class: Sitecore.Data.Items.Item 
(Get Sitecore Item from “/sitecore/content/Home/myItem”)
Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");
Sitecore.Data.Items.Item myItem = master.GetItem("/sitecore/content/Home/myItem"); 
If item does not exist or current context user doesn’t have permission to access this item, Sitecore will return null or throw exception. 
Case is Insensitive while using path to get the items. 

To get a Sitecore Template Item, use Sitecore.Data.Database.GetTemplate(ID)
­Sitecore Template Item Class: Sitecore.Data.Items.TemplateItem 
(Get Template Item: Folder Template)
Sitecore.Data.Items.TemplateItem item = master.GetTemplate(Sitecore.TemplateIDs.Folder); 

To get a Sitecore Media Item, use Sitecore.Data.Database.GetItem(Path)
­Sitecore Media Item Class: Sitecore.Data.Items.MediaItem 
(Get Media Item from “/sitecore/content/Media Library/Images/Logo.png”)
Sitecore.Data.Items.Item myItem = master.GetItem("/sitecore/content/Media Library/Images/Logo.png");
 
To get a Sitecore System Item, use Sitecore.Data.Database.GetItem(ID)
­Sitecore Item Class: Sitecore.Data.Items.Item 
(Get System Item “Layouts”)
Sitecore.Data.Items.Item layoutsItem = master.GetItem(Sitecore.ItemIDs.Layouts);  

To get Sitecore Context Item, use Sitecore.Context.Item
­Sitecore Template Item Class: Sitecore.Data.Items.Item 
Sitecore.Data.Items.Item contextItem = Sitecore.Context.Item;
Note: Add Sitecore.Kernel.dll reference to project.

How to access Sitecore Content Database in Code Behind (Sitecore v6.5) - 3

Content Database:
Sitecore Content Editor primarily interact with Content database i.e. Master Database(by default).
To get the content database, use the following property.
Sitecore.Data.Database contentDB = Sitecore.Context.ContentDatabase;
Note: Add Sitecore.Kernel.dll reference to project.

blockquote { margin: 0; } blockquote p { padding: 15px; background: #eee; border-radius: 5px; } blockquote p::before { content: '\201C'; } blockquote p::after { content: '\201D'; }