Monday, October 27, 2014

Learn EPiServer - Day 2

Creating a Start Page in EPiServer

Today we will create a page type and template for start page based on EPiServer Content model. And configure this page as start page for the site. 

1. Right click on Models -> Pages and select Add -> New Item -> EPiServer -> Page Type named it as HomePage.cs. And uncomment the code-block. We will discuss the details of code block later in this sequence.





2.Right click on Controller and select Add -> New Item -> EPiServer -> Page Controller(MVC) named it as HomeController.cs



There is a little bit changes in code. You need to add a namespace. And an attribute.

using EPiServerTest.Models.Pages;                                    
[TemplateDescriptor(Path="~/Views/Home/Index.cshtml")]

Here TempleteDescriptor attribute is used to tell controller which view to render using Path. You controller will look like this.



using EPiServer;
using EPiServer.Core;
using EPiServer.Framework.DataAnnotations;
using EPiServer.Web.Mvc;
using EPiServerTest.Models.Pages;

namespace EPiServerTest.Controllers
{
    [TemplateDescriptor(Path="~/Views/Home/Index.cshtml")]
    public class HomeController : PageController
    {
        public ActionResult Index(HomePage currentPage)
        {
            /* Implementation of action. You can create your own view model class that you pass to the view or
             * you can pass the page type for simpler templates */

            return View(currentPage);
        }
    }
}    
   




3. Create a folder named Home under Views. Now right click on View -> Home and select Add -> New Item -> EPiServer -> Page Partial View(MVC Razor) and named it as 

Index.cshtml. The all path and name needs to be exactly same as given in TemplateDescriptor Path.



You need to modify the model on view to @model EPiServerTest.Models.Pages.HomePage. Your view will like this.

@using EPiServer.Core
@using EPiServer.Web.Mvc.Html

@model EPiServerTest.Models.Pages.HomePage

<div>
  @Html.PropertyFor(m => m.MainBody)
</div>


5. Build the project and hit F5. Visual Studio will start IIS Express. Now you would be able to login using your windows credential in this url http://localhost:XXXX/episerver where XXXX is the IIS Express port number for this site.

6. Select Pages -> Root and then Add a new page and named as Start.




7. Most important thing you need to create a developer licence for you site. Go to EPiServer Licence Center and create a developer licence and past it in root of your project folder.

8. Now set Start page as site home page. Go to Admin -> Config -> Manage Sites -> Add Site. Fill the relevant information Name, URL and select the start page as Start.



9. You are all done. Now you can browse your site in using the url provided in URL field at step 7. In my case it is http://localhost:2242/



Happy Codding .... :)