Retrieving content on MVC sites

The following section describes how you can retrieve Kentico data in your MVC application. Generally, we recommend using generated code when retrieving pages, custom tables, and form tables from Kentico.

Retrieving pages

We recommend using generated code when working with pages.

The following example uses a generated DancingGoat.Article page type. We recommend using the generated providers when retrieving individual page types.




// Gets articles using the generated provider
IEnumerable<Article> articles = ArticleProvider.GetArticles()
    .OnSite("MySite")
    .Culture("en-US")
    .Path("/Articles/", PathTypeEnum.Children);

// Gets the published version of a single article using the generated provider
Article article = ArticleProvider.GetArticle(nodeGuid, "en-US", "MySite");


By default, the generated code returns published version of pages. If you need to retrieve the latest edited version of a page, use the following approach:




// Gets the latest version of a single article using the generated provider
Article article = ArticleProvider.GetArticle(nodeGuid, "en-US", "MySite")
    .LatestVersion()
    .Published(false);


Working with retrieved page data

You can access the data of a retrieved page using the TreeNode.GetValue() method. There are two types of page data that you can access:

  • Form data - the data that is editable on a page’s Form tab
  • Metadata - data like the page title and page keywords

Accessing page form data

The fields available on a page’s Form tab are specific to its Page type. You can see the fields that each page type uses:

  • In Pages types (application) -> edit page type -> Fields tab.
  • In the CONTENT_<document_type> database table.

We recommend accessing the specific fields of a specific page in the following way as code completion will provide you the relevant fields that you can use:




// Retrieves the page
News news = NewsProvider.GetNews(nodeGuid, "en-US", "MySite");

// Accesses the 'Title' field
string title = news.Fields.Title;

// Accesses the 'Text' field
string text = news.Fields.Text;


Resolving HTML tags and relative URLs

Fields which are populated by the Rich text editor form control may contain HTML tags and relative links. To ensure that the content is displayed correctly, use one of the following methods:

  • Html.Raw method disables the HTML encoding for the values.
  • Html.Kentico().ResolveUrls extension method disables the HTML encoding for the values and resolves relative URLs to their absolute form.



@Html.Raw(Model.<Rich text field content>)
@Html.Kentico().ResolveUrls(Model.<Rich text field content>)


Accessing page metadata

Page metadata are stored the same way as Form data (in the TreeNode object), but aren’t provided in the generated classes. This means that you need to use the GetValue method to access page metadata.

All the metadata fields you can access are defined in the CMS_Document database table.




// Retrieves the page
News page = NewsProvider.GetNews(nodeGuid, "en-US", "MySite");

// Accesses the 'DocumentPageTitle' metadata field
page.GetValue("DocumentPageTitle");

// Accesses the 'DocumentPageKeywords' metadata field
page.GetValue("DocumentPageKeywords");


Retrieving page attachments

For information on retrieving page attachments, see Working with page attachments in MVC applications.

Retrieving custom table data

The following example requires the class generated for the SampleTable custom table included in the solution.




// Gets all custom table items
IEnumerable<SampleTableItem> items = CustomTableItemProvider.GetItems<SampleTableItem>();

// Gets a single custom table item for a given custom table item ID
SampleTableItem item = CustomTableItemProvider.GetItem<SampleTableItem>(1);


Retrieving form data

The following example requires the class generated for the ContactUs form included in the solution.




// Gets all form items
IEnumerable<ContactUsItem> items = BizFormItemProvider.GetItems<ContactUsItem>();

// Gets a single form item for a given form item ID
ContactUsItem item = BizFormItemProvider.GetItem<ContactUsItem>(1);