Try our conversational search powered by Generative AI!

Srinivasan M
Mar 20, 2017
  22841
(14 votes)

Get CMS content using content reference ID

To get CMS content from Episerver using content reference ID anywhere in the MVC application using below code.

var contentReference = new ContentReference(163);
var pageRouteHelper = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>().Get<RegistrationPage>(contentReference);

var model = new CMSPageModel<RegistrationPage, RegisterViewModel>(pageRouteHelper);
model.ViewModel = new RegisterViewModel();

Now you can use this model.ViewModel object to render CMS content in MVC view

Mar 20, 2017

Comments

Henrik Fransas
Henrik Fransas Mar 21, 2017 10:26 AM

There are some of stuff you need to fix for this code to be usefull.

First:

If you are just getting content from the database you should use IContentLoader instead of IContentRepository

Second:

You should never!!! write this as a one line of code:

var pageRouteHelper = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance().Get(contentReference);

You should if you can get the instance of IContentLoader (thats the one you should use) with dependency injection and then you should use TryGet insted of get since Get will throw an exception if there are no page.

Please login to comment.
Latest blogs
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

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