Building Your First Module
Welcome back to Sitecore Bootcamp for Developers! In this part we’ll build our first custom Sitecore 8.x content module.
If you are just joining us, please go back and read the introduction where you’ll also find links to each other part of the bootcamp.
Download the Sitecore Bootcamp For Developers Part 4 Files |
- Log into Sitecore
- http://sitecorebootcamp.localhost/sitecore
- Default username:password is admin:b
- http://sitecorebootcamp.localhost/sitecore
- Click Desktop -> Sitecore Start Menu –> Content Editor
- Create “User Defined” folders to store your content (Sitecore)
- Right click on /sitecore/Layout/Layouts -> Insert -> Layout Folder
- Right click on /sitecore/Layout/Renderings -> Insert -> Rendering Folder
- Create a Layout (Sitecore)
In real projects, you will organize items in sub-folders. The structure will be dictated by the solution architect, client, or framework. - Right click on /sitecore/Layout/Layouts/User Defined -> Insert -> Layout
- Create layout “Main Layout”
- Location = Layouts/User Defined
- File Location = /Website/Views
- Change the Path field to /Views/MainLayout.cshtml
- Click Save
- Create a Page Template (Sitecore)
- Right click on /sitecore/Templates/User Defined -> Insert -> New Template
- Name = “Page”
- Base Template = “Templates/System/Templates/Standard template” (default)
- Location = Templates/User Defined
- On the builder tab
- Section = Meta
- Fields
- Title = Single-Line Text (default)
- Keywords = Single-Line Text (default)
- Description = Multi-Line Text
- Click Save
-
Icons help content editors easily see content type while looking at the content tree. Choosing a custom icon is not required. Set the Page Template Icon (Sitecore)
- Click /sitecore/Templates/User Defined/Page
- Click Configure in the Sitecore navigation bar -> Icon -> More Icons
- Click the Icons tab
- Icons are sorted by categories in a drop down at the top of the dialog
- Find an icon that represents your template
- The icon will appear next to each instance of this template
- I used Applications/Document_Plain
- Click Ok on the Icon dialog
- Click Save
- Set the Page Template Standard Values (Sitecore)
- Click /sitecore/Templates/User Defined/Page
- Click Options in the Sitecore navigation bar -> Standard Values
- Standard values apply to all instances of this template, but can be overwritten by an individual instance as needed.
- They are a good place to provide default values for fields, default layouts, modules (ie navigation, headers, footers)
- Click /sitecore/Templates/User Defined/Page/__Standard Values
- Click Presentation in the Sitecore navigation bar – >Details
- Click Edit on the Shared Layout tab Next to Default
- On the Layout tab, select Layouts/User Defined/Main Layout
- This will make sure all new pages that use this “Page” template all use the “MainLayout” by default.
- Click Ok on the Device Editor dialog
- Click Ok on the Layout Details dialog
- Click Save
- Create an Item Template (Sitecore)
- Right click on /sitecore/Templates/User Defined -> Insert -> New Template
- Name = “Content Blurb”
- Base Template = “Templates/System/Templates/Standard template” (default)
- Location = Templates/User Defined
- On the builder tab
- Section = Content
- Fields
- Heading = Single-Line Text (default)
- Subtitle = Single-Line Text (default)
- Content = Rich Text
- Click Save
- Set the Item Template Icon (Sitecore)
- Click /sitecore/Templates/User Defined/Content Blurb
- Click Configure in the Sitecore navigation bar -> Icon -> More Icons
- Click the Icons tab
- Icons are sorted by categories in a drop down at the top of the dialog
- Find an icon that represents your template
- The icon will appear next to each instance of this template
- I used Applications/text_align_justified
- Click Ok on the Icon dialog
- Click Save
- Create a Rendering (Sitecore)
- Right click on /sitecore/Layout/Renderings/User Defined -> Insert -> Controller Rendering
- View renderings use Sitecore as the “Controller” and call a cshtml directly. This can be useful for simple modules.
- It is a good practice to use controller renderings instead of view renderings. It will let your code be more flexible in future with minimal impact to your code and templates.
- Create rendering “Content Blurb”
- Fields
- Controller = ContentBlurb
- Controller Action = ContentBlurb
- Datasource Template = Insert Link to /sitecore/Templates/User Defined/Content Blurb
- This creates the link between the template in Sitecore and your MVC Controller.
- Click Save
- Right click on /sitecore/Layout/Renderings/User Defined -> Insert -> Controller Rendering
- Create an Error View (Visual Studio)
- Right click on Views folder -> Add -> MVC 5 View Page (Razor)
- Name = Error
- Add html/razor (see attached)
- Create a Page Model (Visual Studio)
- Right click on Models folder -> Add -> New Item
- Choose Visual C#/Code/Interface
- Name = IPage.cs
- Add the SitecoreType attribute decorator to the interface
- [SitecoreType(TemplateId = “{GUID}”, AutoMap = true)]
- This helps sitecore automatically populate properties
- Update the GUID from the Page Template
- Click /sitecore/Templates/User Defined/Page (sitecore)
- Click on the Content tab
- Click Quick Info to expand
- Click on the Item ID and copy to the clipboard
- [SitecoreType(TemplateId = “{GUID}”, AutoMap = true)]
- Add string properties for the fields in the Sitecore template
- Remove any unnecessary using statements
- Create a Layout View (Visual Studio)
- Right click on Views folder -> Add -> MVC 5 View Page (Razor)
- Name = MainLayout
- Add html/razor (see attached)
-
This guide uses interfaces for models. You may also see concrete classes used for models. Create an Item Template Model (Visual Studio)
- Right click on Models folder -> Add -> New Item
- Choose Visual C#/Code/Interface
- Name = IContentBlurb.cs
- Add the SitecoreType attribute decorator to the interface
- [SitecoreType(TemplateId = “{GUID}”, AutoMap = true)]
- Update the GUID from the Item Template
- Click /sitecore/Templates/User Defined/Content Blurb (Sitecore)
- Click on the Content tab
- Click Quick Info to expand
- Click on the Item ID and copy to the clipboard
- Add string properties for the fields in the Sitecore template
- Remove any unnecessary using statements
- Create an Item View (Visual Studio)
- Right click on Views folder -> Add -> New Folder
- Name = ContentBlurb
- Right click on Views/ContentBlurb folder -> Add -> MVC 5 View Page (Razor)
- Name = ContentBlurb.cshtml
- Add html/razor (see attached)
- Create an Item Controller (Visual Studio)
- Right click on Controllers folder -> Add -> Controller
- Empty MVC 5 Controller
- Name = ContentBlurbController
- Extend GlassController instead of Controller
- Add ContentBlurb action method (see attached)
- Build and deploy the solution (Visual Studio)
- Build -> Build SitecoreBootcamp (or Build Solution)
- Build -> Publish SitecoreBootcamp
- Refresh home page in browser
- This allows Sitecore to rebuild its caches
- Create a new Page (Sitecore)
- Right click on /sitecore/Content/Home -> Insert -> Insert from template
- Select Templates/User Defined/Page
- Item Name = page1 (no spaces)
- Set title, keywords, description
- Click Save
- Create a new Content Blurb (Sitecore)
- Right click on /sitecore/Content/Home/page1 -> Insert -> Insert from template
- Select Templates/User Defined/Content Blurb
- Item Name = CB1
- Set heading, subtitle, content
- Click Save
- Add Content Blurb to a Page (Sitecore)
- Click on /sitecore/Content/Home/Page1
- Click Presentation -> Details in the Sitecore navigation bar
- Click Final Layout -> Default -> Edit in the Layout Details dialog
- On the Controls tab -> Click Add
- Select Renderings/User Defined/Content Blurb
- Placeholder = content
- This is defined in the MainLayout.cshtml
- Click Select to close the rendering dialog
-
Datasources allow you to reuse the same content in multiple places on the site. Click the Content Blurb rendering and click Edit
- On the Control Properties dialog -> Data Source -> Browse and choose /sitecore/Content/Home/Page1/CB1
- Notice you can only select items of type “Content Blurb” because Sitecore knows the item type for this control.
- Click Ok on each dialog to close
- Click Save
- Publish Site (Sitecore)
- Click Publish in the Sitecore navigation bar -> Publish dropdown -> Publish Site
- Select Republish
- This will publish EVERYTHING
- A good idea since this is the first publish
- It ensures that all of your templates, layouts, renderings, and items get published.
- Subsequent edits to items can be published using Item or Smart Publish
- This will publish EVERYTHING
- Click select all for languages (if you have multiple languages)
- Click Publish
- Confirm republish
- Load Page (Browser)
- http://sitecorebootcamp.localhost/page1
- The url is case insensitive
- Content blurb item appears on the page
- View source to verify keywords and description are present
- http://sitecorebootcamp.localhost/page1
- Load Item (Browser)
- http://sitecorebootcamp.localhost/page1/cb1
- Notice that you get the Sitecore error page
- It says it cannot find the requested layout with GUID {000-00-00-00-000}
- The CB1 content item does not have a layout defined in its presentation details like the page item does. This means it can go on a page, but isn’t a page itself.
- http://sitecorebootcamp.localhost/page1/cb1
Be sure to follow me through the entire month of November 2017 and follow along with each part as they become available. Thanks for reading, and be sure to leave me comments or questions, I would love to chat with you about Sitecore.
Created by: Eric Sanner, Brandon Bruno, Alan Tibbs
You have inherited controller from GlassController but so far I haven\’t seen any Glass Mapper usage or I missed something?
I use Glass Mapper here to get us ready for part 5 where we enable the experience editor. It also allows the model to easily automap the items from the Content Blurb template.