Book Image

Sitecore Cookbook for Developers

By : Yogesh Patel
Book Image

Sitecore Cookbook for Developers

By: Yogesh Patel

Overview of this book

This book will get you started on building rich websites, and customizing user interfaces by creating content management applications quickly. It will give you an insight into web designs and how to customize the Sitecore architecture as per your website's requirements using best practices. Packed with over 70 recipes to help you achieve and solve real-world common tasks, requirements, and the problems of content management, content delivery, and publishing instance environments. It also presents recipes on Sitecore’s backend processes of customizing pipelines, creating custom event handler and media handler, setting hooks and more. Other topics covered include creating a workflow action, publishing sublayouts and media files, securing your environment by customizing user profiles and access rights, boosting search capabilities, optimising performance, scalability and high-availability of Sitecore instances and much more. By the end of this book, you will have be able to add virtually limitless features to your websites by developing and deploying Sitecore efficiently.
Table of Contents (20 chapters)
Sitecore Cookbook for Developers
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Creating a simple content page using template and layout


As a developer, you will first have to understand the architecture of how a web page works. To create web pages, you first need to create a data structure using data templates, based on which you can create content items. Layout is used to place content with some presentation logic on it. In this recipe, we will take a quick look at creating a simple content page showing the title, body, logo, and other details.

How to do it…

We will first create and define a data template for the Home item (that is, home page):

  1. Log in to Sitecore. Open the Content Editor from Launch Pad. You will find a default /sitecore/Content/Home item with the Title and Text fields. Delete this item as we will create our own custom template for the Home item.

  2. In the /sitecore/Templates item, create a new Template Folder item, Cookbook. In this, create a Site Root template, leaving the base template set to the default Standard template, as shown in the following image:

  3. In template builder, create different fields, Title and Body, as shown in the following image, and save the details:

  4. We will now create a content page using this data template. Create a new content item, Home, in the /sitecore/Content path using the Site Root template that we created and fill in the appropriate information in these fields:

  5. To show common details of websites, such as logo, copyright text, and some other information, create another Site Configuration template with fields such as Company Name, Logo, Copyright Text, and so on.

  6. The Site Configuration item will be a non-visual item, so it should be created outside the Home item. Create a folder item Global in /sitecore/Content, and in this, create a content item, Configurations, using the preceding template, and fill in the appropriate details.

  7. We will now create a layout from Visual Studio. From the SitecoreCookbook project, create an MVC main.cshtml view in the /Views folder to render field values from the previously created items. For this, put the following code in the view file inside the <h tml/body> tag:

    <div id="header"> <a href="/">
      @Html.Sitecore().Field("Logo", Sitecore.Context.Database.GetItem("/sitecore/Content/Global/Configurations"))
    </a> </div>
    
    <div id="contentarea">
      <h1>@Html.Sitecore().Field("Title")</h1>
      @Html.Sitecore().Field("Body")
    </div>
    
    <div id="footer">
      @Html.Sitecore().Field("Copyright Text", Sitecore.Context.Database.GetItem("/sitecore/Content/Global/Configurations"))
    </div>
  8. We will now register this view file as a layout in Sitecore. In the /sitecore/layout/Layouts item, create a layout folder, Cookbook. In this, create a layout named Main Layout. Set the path field of this layout to /Views/main.cshtml.

  9. Now we will assign the layout to the content items. Select the Site Root data template. From the ribbon, select Standard Values in the Builder Options tab. This will create the __Standard Values item for the template.

  10. Select the __Standard Values item. From the ribbon, select the Details button in the Layout group from the Presentation tab. It will open a Layout Details dialog. For a Default layout, click on the Edit button, which will open the Device Editor dialog. Here, in the Layout section, select Main Layout and select OK. See the following image, which represents both the dialogs:

    Tip

    Instead of standard values, you can also assign a layout directly to the Home item. However, it's a recommended practice to apply presentation components to a template's standard values so that all items created from the template will inherit the field values from standard values.

  11. From the Content Editor, select the Home item. From the ribbon, click on the Preview button in the Publish group from the Publish tab. This will open your home page in preview mode, as shown in the following image, where you will find Logo, Title, Body, and Copyright Text.

    Note

    You can get the layout and style sheet files from the code bundle provided with this book to make it work, as shown in the following image:

Tip

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  • Log in or register to our website using your e-mail address and password.

  • Hover the mouse pointer on the SUPPORT tab at the top.

  • Click on Code Downloads & Errata.

  • Enter the name of the book in the Search box.

  • Select the book for which you're looking to download the code files.

  • Choose from the drop-down menu where you purchased this book from.

  • Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

How it works…

Sitecore layouts are reusable ASP.NET Web Forms (.aspx) or MVC views (.cshtml) that you register with Sitecore. ASP.NET uses Web Forms or views to serve HTTP requests. Here, on requesting the Home item, Sitecore first reads the item and renders the physical file of the layout associated with the item.

In the view file, we used the Sitecore field helper, @Html.Sitecore().Field(<field name>), to render the Title and Body field values from the context item (in our case, Home). This helper method can also render a field of items other than the context item, which we used in order to render the Logo and Copyright Text fields of the Configurations item, @Html.Sitecore().Field(<field name>, Sitecore.Context.Database.GetItem(<ItemId or path of item>)).

Here, the Sitecore.Context.Database.GetItem() method provides the Sitecore.Data.Items.Item object, which has a collection of all the field values associated with the item.

Note

To learn more APIs, download Sitecore Presentation Component API Cookbook (https://goo.gl/fu99Vh). It provides APIs from Sitecore 6.4 or later with examples of Web Forms or Web controls, but they are still valid for Sitecore 8 or later with MVC as well.

Apart from the Content Editor, Sitecore also provides another tool, Experience Editor, to view pages in editing mode. From the ribbon, click on the Experience Editor button in the Publish tab to open an item in the Experience Editor. You can open it from Launch Pad as well. Here, you can change field values (for example, Body in the following image) rendered on the layout or view:

To disable content editing for an item field, you can pass additional parameters to the field helper, as shown in the following code:

@Html.Sitecore().Field("<field name>", new {DisableWebEdit=true})

You can also customize its rendering behavior by extending the Sitecore helper class. You can learn this from https://goo.gl/ZHruKe and http://goo.gl/Kx8MQl.