BLOG: Web Content Management

Welcome to Oshyn’s Web Content Management Blog where our experts discuss the latest developments and best practices in the Content Management industry with a focus on several leading platforms: Drupal, EPiServer, Jahia, Open Text and Sitecore.

Creating Templates and Items in Sitecore by coding

Creating Templates and Items in Sitecore by coding

Marcelo Davalos... - Friday, December 04, 2009

If we have looked around in Sitecore it is clear the idea of templates and items.  As it says on the application manual, Sitecore treats everything as a template which is equals to a class in an object oriented language, and the items which are an instance of the class.  This concept helps programmers to build organized components.  But, this does not end here, programmatically Sitecore also maintains this concept and offers the chance to create and modify templates and items just by using its libraries in .Net.

This ability to create the site components and its instances by scripts in c# brings us the possibility to extend the functionalities of the site in different manners, like creating migration tools or custom tools on the Sitecore CMS.  Knowing about these benefits in the following blog we present a brief detail on how to create this two major Sitecore components, templates and items.



In the picture above the home item is selected, on the right side description we can see the details of this item.  First, we see the item has a path “/sitecore/content/Home”, and then we can see the template (class) of the item which is “/sitecore/templates/Sample/Sample Item”.  This is the format how Sitecore works.  Programmatically, we can see this structure as follows:

The first step is to use the Sitecore.Data.Items library.

Then we will look for the item and template on the database:

//To be able to access the information without restrictions we use the Sitecore.SecurityModel.SecurityDisabler

using (new Sitecore.SecurityModel.SecurityDisabler())

            {

                //Set the master database we will be working with.

  Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

               //Look for the item in the database

                Sitecore.Data.Items.Item item = master.Items.GetItem(“/sitecore/content/Home”);

                //Get the template of the current item

                Sitecore.Data.Items.TemplateItem template = item.Template;                              

            }

With these steps it is possible to look for items and its properties. 

However, programmatically the main process and functionality people use in Sitecore is to create templates and items, this is the base work a back end user will do to personalize or create tools.

To create a temple we will do the following: look if the template exists, if not create a template.

In the following example we will create a template under the User Defined folder

using (new Sitecore.SecurityModel.SecurityDisabler())

            {

               //Set the master database we will be working with.

                Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

                //Look for the template on the database, if not found a null value will be set

                Sitecore.Data.Items.TemplateItem templateItem = master.Templates[“/sitecore/Templates/User Defined/MyTemplate”];

                //Check if template Item does not exists              

                if (templateItem == null)

                {

                       //Get the parent Item of the template (folder)

                       Sitecore.Data.Items.Item parentItem  = master.Items.GetItem(“/sitecore/Templates/User Defined”);

                     //Create the template under the given parent Item

                     templateItem = p_Database.Templates.CreateTemplate(“/sitecore/Templates/User Defined/MyTemplate”, parentItem);

                }               

            }

On this code sample we can see that we look for the template we want to create first.  Then if not found we look for the parent item.  In this example the parent item is an item whose template is a folder.  Then we create the template giving the template path and the parent item (folder).

Once we have created the template we can instantiate it as an item.

using (new Sitecore.SecurityModel.SecurityDisabler())

            {

               //Set the master database we will be working with.

                Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

               //Look for the Item to check if exists

                Sitecore.Data.Items.Item item = master.Items.GetItem(“/sitecore/content/Home/MyItem”);

                if (item == null)

                {

                         //Look for the TemplateItem we will be instantiating

                         Sitecore.Data.Items.TemplateItem templateItem = master.Templates[“/sitecore/Templates/User Defined/MyTemplate”];

                         //Create the Item based on the given template item

                        Item = p_Database.CreateItemPath(“/sitecore/content/Home/MyItem”, templateItem);

                }

            }

As a result of all this code, we have gotten the basic steps to look for a template and item and create each one of them. 


ajax rotator
Recent Posts

RSS feeds
Tag cloud
VS2010 frontend optimization scalability VB content mangement Drupal SMM AS3 CRM Maven liferay reddot whitepaper web design los angeles ipc JSR-168 reddot web development los angeles Visual Studio E-commerce IT Investment sitecore oms google analytics higher education ajax push patterns google search appliance portlets release management iphone cms los angeles reddot higher education RFP Navigation facebook developers plugin jquery industry challenges LiveServer jahia cms CTA open text web solutions cms white paper html reddot cms CMS Training campaigns deployment drools redundant database structure dynament social 2.0 tools EPiServer Active Directory web design SEO MVC open text cms consulting cloud VPP Live Server url Business Users Marketing Automation oshyn project management open text management server Acquia rdbs Active Directory content authoring keyword selection facebook open-source sitecore meetup postgis management opentext web2.0 web content management licencing theming profile tab design patterns geographical ubuntu oms APC design modules Jahia web marketing Velocity Flash webcomponent google maps Visual Basic sitecore online marketing suite, online marketing suite LDAP sitecore ECM ui Web development facebook app, OS 3.0, three20 templating jsp open text css multisite web services concrete5 EPiServer google google appEngine GSA Ubercart software design Sitecore foursquare editing content drush Database sales 2.0 data access IIS content management white paper enterprise web3.0 .net search suggest jahia wcm open text delivery server jquery, jquery plugin open source target dynament marketing development java LS print inbound marketing search engine GIS lead generation presentation layer portal content management systems configuration management twitter cookie-free domain missing images web marketing for dummies cms los angeles, content management los angeles, web development los angeles, website design los angeles, web design los angeles, sitecore meetup, sitecore user group sitecore devices CMS usability mobile communicating new sites Ubuntu Server friendly url keywords lucene reddot white paper profile box website design los angeles location Dynamic Data fbml command line geo sitecore layout ajax template design mysql Sharepoint sitecore email campaign manager wcm mashups mashup mashware sitecore user group cms whitepaper ASP.NET javascript WCM Design Patterns integration optimization OO Development online marketing tuning Solr boost web evolution content management whitepaper architecture thoughts Sitecore Active Directory multilingual spatial code design HubSpot icefaces performance php5 linux asp social network cluster Delivery Server content management los angeles social media tuckey content authors call to action ASP.NET MVC reddot cms higher education Actionscript 3 lead management memcache CMS open text liveserver cloud computing php
2010 Copyright Oshyn. All rights reserved.