Developer Primer - Sitecore

Overview

Sitecore is a CMS (Content Management System) based on ASP.NET and typically a SQL Server database, although Oracle is also supported. It can be highly customized via Visual Studio. (Sitecore supports Visual Studio's Web Application model, not the Website model.) Content items are created within the content hierarchy and stored in the database. In certain cases, content items will point to a file on the web server. This is the case with sublayouts, which is the Sitecore term for a web user control (ASCX file). Content is stored in one of three database: Core, Master, or Web. The actual database name is prefixed with the name of your Sitecore instance, plus "Sitecore_". Thus, if the name of your Sitecore instance (specified during installation) is "MySite", the three databases would be called MySiteSitecore_Core, MySiteSitecore_Master, and MySiteSitecore_Web. The connection strings to these databases is held in the ConnectionStrings.config file in the /App_Config folder off the website root.


Terminology








Website Creation Roadmap

Website development typically proceeds as follows.

1. Data Templates are created for each content type. An icon for template may optionally be changed at this point to distinguish items based on this template from those based on others.

2. Template Standard Values are created for the Data Template.

3. The Template Standard Values are used to specify default field values and default insert options.

4. Components are created for the presentation layer. These take the form of either a Sublayout (which is stored internally as a .NET web user control – i.e., an ASCX file) or an Rendering (which is stored internally as an XSLT file). Note the Renderings (XSLT files) are typically used for static or less complex content, and Sublayouts (ASCX files) are used for more complex presentations.

5. Template Standard Values are bound to Presentation Layout Details. Binding can be done dynamically by the use of a Sitecore placeholder control, or statically through traditional ASP.NET markup.

Using Visual Studio


Key Classes and Members

General

SnippetUsage
Sitecore.Context.ItemThe item we're currently working with
LinkManager.GetItemUrl()Generates an SEO-friendly URL
LinkManager.GetDynamicUrl()Generates URLs for analytics
MediaManager.GetMediaUrl()for Media Items, where you'd have an item with a reference to another item

Sample Code to Get Items

using (new SecurityDisabler())
{
    //--- Option 1 ---
    var db = Factory.GetDatabase("master");
    var items = db.SelectItems("/sitecore/content/data[@name='yyz']");

    //--- Option 2 ---
    var item = Sitecore.Context.Item;
    var items = item.Axes.SelectItems("/data[@name='yyz']");

    if (items != null)
    {
        // do something with items
    }
}

Sample Code to Create Item

Item dataFolder = null;
TemplateItem templateItem = null;
string cUser = "";

try
{
    using (new SecurityDisabler())
    {
        var db = Factory.GetDatabase("master");
        dataFolder = db.GetItem("/sitecore/content/data");
        templateItem = db.Templates["MyTemplates/Template2"];
    }

    var newItem = dataFolder.Add("Item Name", templateItem);
    newItem.Editing.BeginEdit();
    newItem["FieldName"] = "xyz";
    newItem.Editing.EndEdit();
}
catch (Exception error)
{
    throw;
}

Getting Field Values

var item = Sitecore.Context.Item;
var fld = items.Fields["fieldName"];
if (fld != null && !string.IsNullOrEmpty(fld.Value)) 
{
    // Do something
}

Indexer returns an empty string if (1) fields value is null or (2) field doesn't exist.

Field TypeClass NameValue
(most)(varies)fld.Value
Rich Text fieldHtmlField fldfld.InnerField.Value
Checkbox fieldCheckboxField fldfld.Checked
Date fieldDateField fldfld.DateTime
HyperLink fieldLinkField fldfld.Url
Image fieldImageField fldstring imageURL = StringUtil.EnsurePrefix('/', MediaManager.GetMediaUrl(fld.MediaItem));
Name-Value ListNameValueList fld(use WebUtil for manipulation, esp. ParseUrlParameters method)
List Types: Droplink, Droptree, GroupedDroplinkReferenceField fldfld.ID is a GUID for another Sitecore item
List Types: Checklist, Multilist, Treelist, TreelistExMultilistField fldfld.GetItems() // returns array of Items

Sitecore Query and the DataSource Field Property


Page Editor Support