<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1639164799743833&amp;ev=PageView&amp;noscript=1">
Diagram Views

Tips for New Episerver Developers

Brad McDavid
#Episerver-Ektron Merger, #Episerver, #Code
Published on February 17, 2015
warren-wong-323107-unsplash-1

If you are new to developing with Episerver, learn how to get the most out of your Episerver platform with these developer tips.

Following the recent merger between EPiServer and Ektron, many website owners are now considering migrating from Ektron to EPiServer. But in addition to the site owners, web developers who are well-versed in Ektron are also finding that they will need to learn how to translate their expertise into a new platform. We’ve worked with both platforms extensively, so we wanted to share some tips that can help developers that are new to EPiServer understand how to get the most out of the platform. This is the first in a series of blogs in which we'll share tips for EPiServer developers, so please stay tuned as we continue to look at EPiServer development. Here are some basics to get us started:

Content References

In Ektron, the only known content reference for a new site is the root content folder which has the identifier of zero. There is no way out of the box to specify a homepage, but having a reference from a settings smart form with a content selector quickly resolves the issue. In EPiServer, there are some references that are almost always available from anywhere in the code from the start which are:

  • EPiServer.Core.ContentReference.RootPage
  • EPiServer.Core.ContentReference.StartPage (only available within an HTTP context)

Navigation Selected States

Many web sites these days include a visual element in the site navigation to indicate the current or active section of the web site for the visitor. In Ektron, determining the current section is typically done by checking the ID of the current page to a content ID in the site navigation item. In EPiServer, ContentReference identifiers are slightly more complex in that they contain a WorkID for versioning. To account for this, the EPiServer API has a method on a ContentReference of CompareToIgnoreWorkID, which does as it says, compares the content references ignoring the version.

Creating Structured Content (Page Types and Block Types)

My previous article compares how Ektron and EPiServer differ in creating structured content and dynamic pages. For EPiServer, when creating structured content types, knowing what properties are available is very handy, and EPiServer provides many out of the box. Here is a list of built-in types (also note that any block type can be used as a PageData property):

Default Property Values

In Ektron, default values are specified when creating smart forms within the work area. In EPiServer, page types are typically created by classes, so to set the default values, the SetDefaultValues method is available to override for your page type to set the defaults. The following snippet shows how to set a DateTime property to the current date and time:

public virtual DateTime ArticleDate { get; set; }

public override void SetDefaultValues(EPiServer.DataAbstraction.ContentType contentType)
{

base.SetDefaultValues(contentType);
this.ArticleDate =
DateTime.Now;
}

Property Attributes

Utilizing property attributes are a great way to simplify content creation, and EPiServer utilizes many built-in .NET attributes, as well as providing several of its own.

Namespace: System.ComponentModel.DataAnnotations

  • Display – sets name, sort order, group name (tab) in the UI.
  • Required – requires a value before the content can be published.
  • Editable(true or false) – makes field read-only in the UI if false is passed.
  • UIHint(hint name) EPiServer.Web.UIHint.LongString makes a string property appear as text area. These hints can also be used for ContentReference properties to open an image selector using EPiServer.Web.UIHint.Image or choose block folder using EPiServer.Web.UIHint.BlockFolder. If there isn't an input type or style that you prefer, the UIHint attribute can also be used with your own custom-developed input types and associated UI.

Namespace: EPiServer.DataAnnotations

  • CultureSpecific – makes property unique per language.
  • Searchable – includes the property in the search index.

Namespace: EPiServer.Shell.ObjectEditing

  • AllowedTypes – used on ContentArea and ContentReference properties to restrict the types of content allowed to be referenced within them.

Custom Attributes

Developers can also create custom attributes; here are some great resources for doing so:

Code Extensions

Using extension methods can quickly speed up coding by simplifying the syntax to operate on data. The following code example provides an extension to quickly get a page type’s content from a given content reference:

// code sample

namespace Web.Extensions
{

// extension classes must be static
public static class ContentExtensions
{

// Get a reference to the defined IContentLoader service to share with extension methods
private static EPiServer.ServiceLocation.Injected<EPiServer.IContentLoader> ContentLoader { get; set; }

// Takes a content reference and populates the type T with data
public static T GetContent<T>(this EPiServer.Core.ContentReference Content) where T :EPiServer.Core.IContent
{

try
{
return ContentLoader.Service.Get<T>(Content);
}

catch (Exception ex)
{
// Handle exception here
}

return default(T);
}
}
}

// usage sample
// add a reference to the extensions: using Web.Extensions;

StartPageData start = EPiServer.Core.ContentReference.StartPage.GetContent<StartPageData>();

Global Settings

Creating global settings like a web site logo, footer text, social media links, and company information is always useful to have and should never be hard coded. In EPiServer, these settings are best stored in a settings block that is assigned to the startpage type. Accessing them then simply becomes:

// code sample also utilizing code extension from above

StartPageData start = EPiServer.Core.ContentReference.StartPage.GetContent<StartPageData>();
var value = start.NameofSettingsBlock.NameofProperty;

Content Events

For developers who are familiar with Ektron content strategies, EPiServer has similar content events, and adding Alf Nilsson’s EPiEventHelper to your web site code will greatly simplify implementing events, as the functions can be stored within the class that makes the page type by implementing specified interfaces such as IPublishedPage.

Quick Tips

To determine if content is being edited, the following property can be checked: EPiServer.Editor.PageEditing.PageIsInEditMode.

To get content that has not been published use language overload and pass: LanguageSelector.AutoDetect(true) when calling GetChildren<T>.

To personalize a list of content based on visitor groups, use: EPiServer.Filters.FilterForVisitor.Filter.

Final Thoughts

New developers will want to utilize both the Visual Studio Extensions, which simplifies creating page types, block types, and templates, and the EPiServer NuGet Feed which helps you keep EPiServer up to date with the latest releases which come every two weeks thanks to their continuous release process.

In addition to these tips, more resources can be found in EPiServer's developers guide, their developer forum, and their blogging community. If you have any questions for us about how to use the methods we've discussed, or if you want to know more about EPiServer development, please contact us to speak to a Solutions Engineer. Do you have any questions about EPiServer development that we can discuss in future blogs in this series? Please share them in the comments below.