Try our conversational search powered by Generative AI!

Eric
Feb 1, 2013
  7732
(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
From Procrastination to Proficiency: Navigating Your Journey to Web Experimentation Certification

Hey there, Optimizely enthusiasts!   Join me in celebrating a milestone – I'm officially a certified web experimentation expert! It's an exhilarati...

Silvio Pacitto | May 17, 2024

GPT-4o Now Available for Optimizely via the AI-Assistant plugin!

I am excited to announce that GPT-4o is now available for Optimizely users through the Epicweb AI-Assistant integration. This means you can leverag...

Luc Gosso (MVP) | May 17, 2024 | Syndicated blog

The downside of being too fast

Today when I was tracking down some changes, I came across this commit comment Who wrote this? Me, almost 5 years ago. I did have a chuckle in my...

Quan Mai | May 17, 2024 | Syndicated blog

Optimizely Forms: Safeguarding Your Data

With the rise of cyber threats and privacy concerns, safeguarding sensitive information has become a top priority for businesses across all...

K Khan | May 16, 2024