Try our conversational search powered by Generative AI!

Eric
Feb 1, 2013
  7705
(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
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog