Sitecore MVC

by Chris van de Steeg. 2 Comments

January 1st I started my new Job at eFocus, a full service internet company, at the department specialized in Sitecore development. I’d never worked with Sitecore before, so it was a complete new experience. Since I love the ASP.NET MVC way of development, I started looking if there was an option to use it for Sitecore development. Unfortunately I did not find any community effort to do so, except for blog posts on how to use Sitecore and an MVC project side-by-side. So I decided to try to create my own solution, since I would then have something fun to do while getting to know the ins and outs of Sitecore, something I felt obliged to do anyway.

In this blog post I’ll show you how to install and use my end-result of this proof of concept. In a follow-up, I’ll create a sample application with it. This first version is really just that: a proof of concept, just showing that it works. For example, this version includes a simple valueprovider for binding sitecore items to a model, but this is far from optimal. When creating a sample application, I’ll also be digging into modelbinding and other issues that’ll show up when creating an application. Perhaps I’ll conclude that its better to use something like CustomItem generator or the glass project and wrap one of those in a modelbinder, we’ll see (shoot your opinion about that in the comments!). I also only tested this on Sitecore 6.4, so it’ll probably only work on that version and up.

Now, let’s go to the code! To get started, open an existing Sitecore project (or create a new one) in Visual Studio. Now add the NuGet package BoC.Sitecore.MvcApp either through the package manager console or through tools->library package manager->Manage NuGet Packages for Solution…

VerySimpleWebsite - Manage NuGet Packages_2012-02-28_18-36-28

The Sitecore Mvc starter app is the version I’ll use for this blog-post: it will add a basic mvc project to your solution to get started with. The other one (BoC.Sitecore.Mvc) will just add the dll + a config file to your current project, use this one if you want to set up your own Mvc project without any starting point.

Once you’ve installed the package, a new project named ‘yourproject’.mvcapp is added to your solution:

emptysite - Microsoft Visual Studio_2012-02-28_18-40-50

This is an empty ASP.NET MVC3 Razor project, with some stuff removed. The default web.config is removed, since we’ll have to use a modified version of Sitecore’s web.config and the account-stuff has been removed. As you can see, a default HomeController has been added, a default view and a default model (ItemView). Now, to see if BoC.Sitecore.Mvc works, let’s build the solution and open up the sitecore shell. Once logged in to your Sitecore shell, expand the tree items /sitecore/Layout and /sitecore/Layout/Layouts. You’ll see that underneath both, a new folder is added named ‘MVC Actions’. It’s in two places since you can use your MVC Actions both as Layout or as a rendering, more on that later.

Sitecore - Windows Internet Explorer_2012-02-28_19-02-43

Let’s see what the action and view for Home/Index looks like, so we can now what to expect to see when we try to load them in sitecore. The action itself looks very simple:

emptysite - Microsoft Visual Studio_2012-02-28_19-09-36

It expects an item of type ItemView and just passes that on to the view. As mentioned earlier, I added a simple valueprovider that will try to resolve any parameter named “item” with field-values from the current sitecore item (the current sitecore item by default is the context.item, unless you provided a datasource for the rendering). The model by default only includes the property DisplayName of the type RenderingString:

emptysite - Microsoft Visual Studio_2012-02-28_19-14-05

RenderingString is a special type added by BoC.Sitecore.Mvc : it will call RenderField when outputted in html so you can use the page editor. You could also change this to ‘public string DisplayName {get; set; }’ to have it filled with the normal string value. Since DisplayName is not editable in the page editor, and thus we cannot see the difference between RenderingString and normal String, let’s change this property to Title like so:

emptysite - Microsoft Visual Studio_2012-02-28_19-19-04

In our case we’ll use the ‘Sample Item’ template from the default setup, which has a Title field. If your item does not have a Title field, you’ll have to pick some other field name of course.

Now, open up the view in Views/Home/Index.cshtml:

emptysite - Microsoft Visual Studio_2012-02-28_20-05-05

The view still uses DisplayName, so we have to change that as well:

emptysite - Microsoft Visual Studio_2012-02-28_20-08-02

When the view will be fully rendered (thus not through a RenderChildAction), the view uses the ‘masterpage’ Views/Shared/_Layout.cshtml:

emptysite - Microsoft Visual Studio_2012-02-28_20-14-09

As you can see there is a special HtmlHelper extension used here: @Html.RenderPlaceholder(“main”) which is the equivalent of <sc:placeholder Key=”main” runat=”server” /> for the aspx version.

Now, compile your changes, and go back to the sitecore shell. Underneath the Homepage, add a new Sample Item template called ‘My new sample item’ and click the ‘Layout Details’ button in the ‘Presentation’ Ribbon.  In the dialog that opens, click on ‘edit’.

Sitecore MVC - Windows Live Writer_2012-02-28_20-11-17

In the next dialog, click on the layout to pick the MVC Actions / Home / Index layout

Sitecore -- Dialoogvenster van webpagina_2012-02-28_20-20-37

Now, save all modifications by click ‘OK’ twice.

Now in the ribbon presentation, click on the ‘preview’ button to see our new page.

Sitecore - Windows Internet Explorer_2012-02-28_20-26-29

And there it is! Our MVC rendered sitecore item. The page still holds all the other renderings from the original Sample Item template including the nested Sample Sublayout.ascx the Sample Inner Sublayout.ascx and the sample rendering.xslt. So now we have an MVC rendered razor layout, holding asp.net usercontrols with an xslt rendering! You can mix-and-match those all together, how cool is that!

The item’s title is displayed twice, since our ‘Layout’ renders the title also, remember (the Home/Index.cshtml ouputs the title, and we’ve picked that as our Layout for the sitecore item). To see that the page editor also still works (even for the razor code), click on the ‘page editor’ button in the ‘Publish’ ribbon.

httplocalhost56325sc_mode=edit&sc_itemid=%7b46A6E270-FD57-4A02-9BBE-4B6AB2_2012-02-28_20-35-35

Modify the fields, add some extra renderings, it all still works. To show off an extra mix-and-match, now let’s also add our action as rendering to the existing asp.net based homepage.

Click on the homepage-treeitem and click on the ‘Layout details’ button on the ‘Presentation’ ribbon. In the popup dialog, click on ‘edit’ and then, in the next popup dialog, click on controls.

Sitecore -- Dialoogvenster van webpagina_2012-02-28_20-39-30

Now, click the add button. In the next popup dialog, select MVCActions/Home/Index and add it to placeholder ‘/main/centercolumn/content’

Sitecore -- Dialoogvenster van webpagina_2012-02-28_20-41-12

Click on select, and then twice on OK to save this new addition. Now, on our preview pane, we see an extra Title appear!

Sitecore - Windows Internet Explorer_2012-02-28_20-44-43

This is our Views/Home/Index.cshtml ! Since the action is now added to the page as a rendering instead of being used as a layout, the /Views/Shared/_Layout.cshtml is not used. This is because this action is now being rendered by calling MVC’s RenderChildAction. Child actions never have a layout.

Now just go ahead and do some mixing of different options for yourself, you could try a different viewengine (aspx, spark viewengine) for example, works just fine. A little extra nice-to-know is that you can add a DescriptionAttribute to your controllers and/or actions to have them show up differently in sitecore. ({0} is thereby replaced with the original name).

I hope you’ll all enjoy playing with MVC within Sitecore and get some great ideas about it while you’re at it. Please leave all your great ideas and suggestions in the comments.

If you want to checkout the source, you can find that at github: http://github.com/csteeg/BoC.Sitecore.MVC

Compile your asp.net mvc Razor views into a seperate dll

by Chris van de Steeg. 92 Comments

Inspired by David Ebbo’s blog post ‘Turn your Razor helpers into reusable libraries’ I wanted to be able to embed compiled Razor views in a dll. This would allow for easy distribution of asp.net mvc ‘modules’ that have their default views embedded, but allowing you to place files in your ‘views’ folder to override those default views.

To generate the c# code for the views, I just started out with David Ebbo’s single file generator and modified it to generate views instead of helpers only. To do this, there had to be a WebRazorHostFactory that knew about all the mvc stuff (what dll’s to reference, what namespaces to use, etc) .I could either choose to include all that information statically in the code, or I could look for a web.config in the same project and let the WebRazorHostFactory use that config. I chose the latter option, so that everyone can easily change the WebRazorHostFactory behavior  by adding a web.config file to their project.

Next, I started figuring out how the PageVirtualPathAttribute inside System.Web.Webpages.dll is used by Microsoft. I found out that it is used when you call ApplicationPart.Register and after that call, there’s not much you can use of this functionality without some heavy reflection. I know it’s the wrong path to choose, but did it anyway. I ended up creating a ViewEngine calling into ApplicationPart’s internal methods to output compiled views. It worked, but I didn’t like it much: there had to be a better way.

I then tried if I could hook into the BuildManager stuff that asp.net mvc uses to generate the views. It was actually much easier then expected, now why didn’t I go on that path the first time!

I ended up creating

  • a CompiledRazorBuildProvider, which inherits from the default RazorBuildProvider,
  • a CompiledVirtualPathProvider which returns a CompiledVirtualFile if it decides that a compiled view should be used
  • and a ApplicationPartRegistry, which registers which compiled views are available.

To view the source, head over to

To just get started without viewing the source, read on, I will explain step by step how to use this library with some screenshots.

If you have improvements, fork the project on GitHub and let me know! If you have suggestions for improvement, just drop it in the comments.

Step 1: Install the FileGenerator

Download and install the Visual Studio extension: http://visualstudiogallery.msdn.microsoft.com/en-us/f28290ce-d987-4f91-b034-707031e10ce6/file/39295/0/RazorSingleFileGenerator.vsix

Step 2: Create new mvc project

image

image

Step 3: Add a class library to hold the views

image

image

Step 4: Cut your Models & Views folders

image

Step 5: Paste them into the just created Class Library

image

Step 6: Copy the website’s web.config file

image

Step 7: Paste that web.config file into the class library

image

Step 8: Select all your .cshtml files in the class library and set the Custom Tool to ‘MvcRazorClassGenerator’

Does anyone have a suggestion on improving this step??

image

Step 9: Build your class library

Step 10: Add your class library to the references of the web application

image

image

Step 11: Add ‘Commons.web.mvc.precompiledviews.dll’ as a reference to your website

this dll is copied to the class library’s output folder (in this case ../EmbeddedViews/bin/debug’)

image

Step 12: Register your views by adding a line to the application_start in global.asax.cs

image

Step 13: Run your website!

Now, you will see the normal default website, even though there aren’t any views in your website path!

If you wish to override some views, just create the normal folders (eg /Views/Home) and add your views there, but don’t forget to copy back the deleted /Views/web.config back into your project then!

HappyBirthday app – Vodafone App Star Country winner

by Chris van de Steeg. 2 Comments

I’m very (very!) happy to inform you all that my mobile app HappyBirthday! has won the 1st price at the Vodafone AppStar competition for the Netherlands. This also means, the app is now 1 of the eight apps in the the grand final! The first price of the finals is another € 75.000 !!
So I kindly ask you to all go vote at the Vodafone App Star website and get me that first price! :-)

HappyBirthday is an app for the Vodafone 360 platform and helps you to never forget a birthday again.
You can read more about the app at the details page of HappyBirthday!

The Vodafone 360 platform is a new internet service for your mobile, PC and Mac. It brings your phone, email, chat and social network contacts together in one placeand runs on many many phones. You don’t need to have a vodafone account to join 360, anyone can join at http://360.com.