SaaS CMS has officially launched! Learn more now.

Srinivasan M
Mar 20, 2017
  23142
(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
Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024

Optimizely release SaaS CMS

Discover the future of content management with Optimizely SaaS CMS. Enjoy seamless updates, reduced costs, and enhanced flexibility for developers...

Andy Blyth | Jul 17, 2024 | Syndicated blog

A day in the life of an Optimizely Developer - London Meetup 2024

Hello and welcome to another instalment of A Day In The Life Of An Optimizely Developer. Last night (11th July 2024) I was excited to have attended...

Graham Carr | Jul 16, 2024

Creating Custom Actors for Optimizely Forms

Optimizely Forms is a powerful tool for creating web forms for various purposes such as registrations, job applications, surveys, etc. By default,...

Nahid | Jul 16, 2024

Optimizely SaaS CMS Concepts and Terminologies

Whether you're a new user of Optimizely CMS or a veteran who have been through the evolution of it, the SaaS CMS is bringing some new concepts and...

Patrick Lam | Jul 15, 2024