Eric
Feb 1, 2013
  7770
(5 votes)

Example of baseclass and access to StartPage in your CMS 7 projects

When you start developing your EPiServer CMS 7 website you will get the new features for building pagetypes directly in code. An important message at the developer course is to create your own base class for the templates and user controls that you develop in your project. When you start developing you will probably have to reference your StartPage from time to time, we often have global settings and other important properties on the startpage. For instance you might have properties on the startpage where you change information in the site footer or links to external sites. This is a great way of using the startpage since we have access to that page via the API in EPiServer:

   1: PageReference.StartPage;
   2:  
   3: ContentReference.StartPage;

 

When we get our properties from the startpage we can now use the ServiceLocater helper class in CMS 7 and ContentRepository() combined with the new and shiny Get<T>:

   1: var prop = 
   2: Locate.ContentRepository().Get<StartPageType>(ContentReference.StartPage).YourProperty;

Doing this all the time is not that funny when binding lists and other functions in for instance the site footer. So instead of doing this all the time we create a base class for both usercontrols and templates. In this base class we add our StartPage-object:

   1: public class SiteUserControlBase : EPiServer.UserControlBase
   2:     {
   3:         private StartPageType mStartPage;
   4:  
   5:         /// <summary>
   6:         /// Start page for the web site
   7:         /// </summary>
   8:         public StartPageType StartPage
   9:         {
  10:             get
  11:             {
  12:                 if (mStartPage == null)
  13:                 {
  14:                     mStartPage = Locate.ContentRepository().Get<StartPageType>(ContentReference.StartPage);
  15:                 }
  16:  
  17:                 return mStartPage;
  18:             }
  19:         }
  20:     }

Now you make sure you inherit the new class in your usercontrols. By doing this you can now start to use the StartPage as strongly typed directly when accessing propertys in your code:

   1: <asp:Literal runat="server" ID="test"></asp:Literal>
   2:  
   3: <EPiServer:Property runat="server" ID="EPiPropertyCtrl"></EPiServer:Property>
   1: test.Text = StartPage.SearchLegend;
   2:  
   3: EPiPropertyCtrl.PageLink = StartPage.PageLink;
   4:  
   5: EPiPropertyCtrl.PropertyName =  StartPage.GetPropertyName(p => p.SearchLegend);

Using the webcontrol will ensure you have the correct attributes when it comes to editing on page in CMS 7.

Might not be the best example but hey it works Ler

Have a nice weekend, I will!!

Feb 01, 2013

Comments

Eric
Eric Feb 1, 2013 04:29 PM

Sorry for the double post but I can not remove it :)

Trupti Lanke
Trupti Lanke Feb 1, 2013 06:13 PM

Thanks Nice article

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024