November Happy Hour will be moved to Thursday December 5th.

Eric
Feb 1, 2013
  7910
(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 SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog

I'm running Optimizely CMS on .NET 9!

It works 🎉

Tomas Hensrud Gulla | Nov 12, 2024 | Syndicated blog

Recraft's image generation with AI-Assistant for Optimizely

Recraft V3 model is outperforming all other models in the image generation space and we are happy to share: Recraft's new model is now available fo...

Luc Gosso (MVP) | Nov 8, 2024 | Syndicated blog