Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.

 

Eric
Feb 1, 2013
  7954
(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
How to: create Decimal metafield with custom precision

If you are using catalog system, the way of creating metafields are easy – in fact, you can forget about “metafields”, all you should be using is t...

Quan Mai | Jan 16, 2025 | Syndicated blog

Level Up with Optimizely's Newly Relaunched Certifications!

We're thrilled to announce the relaunch of our Optimizely Certifications—designed to help partners, customers, and developers redefine what it mean...

Satata Satez | Jan 14, 2025

Introducing AI Assistance for DBLocalizationProvider

The LocalizationProvider for Optimizely has long been a powerful tool for enhancing the localization capabilities of Optimizely CMS. Designed to ma...

Luc Gosso (MVP) | Jan 14, 2025 | Syndicated blog

Order tabs with drag and drop - Blazor

I have started to play around a little with Blazor and the best way to learn is to reimplement some old stuff for CMS12. So I took a look at my old...

Per Nergård | Jan 14, 2025

Product Recommendations - Common Pitfalls

With the added freedom and flexibility that the release of the self-service widgets feature for Product Recommendations provides you as...

Dylan Walker | Jan 14, 2025

My blog is now running using Optimizely CMS!

It's official! You are currently reading this post on my shiny new Optimizely CMS website.  In the past weeks, I have been quite busy crunching eve...

David Drouin-Prince | Jan 12, 2025 | Syndicated blog