Thursday, November 23, 2017

Integrating an isolated MVC app with a Sitecore site.

I was working on an MVC app that was later intended to be integrated into a sitecore site. The latter step kept being postponed partially because it seemed to have many unknowns. The advantage I had was using Team Development for Sitecore which allows leveraging source control and significantly simplifies deployment. Once I started I realized it was less complicated than I expected. Here are the steps I followed for this proof of concept setup:

1. Set up a blank Sitecore instance (in my case it was 8.1 rev. 160302 ).

2. Create an item in sitecore tree that you intend to be navigable.

3. This step is optional. In Visual Studio remove Debug and Release config files from your project.

4. Set the Web.config file's Build Action property to 'None' and Copy to Output Directory to 'Do not copy'.

5. Add a TDS project to your solution and set up it's properties to build out to your output directory (webroot).

6. Disable default route in RouteConfig

7. Add references to Sitecore.Kernel and Sitecore.MVC to your project

8. Create an MVC layout (.cshtml file). In this layout add a placeholder. 

9. Make a controller rendering that points to a controller and action method

10. In presentation details add this new rendering you just created to the placeholder in your MVC layout. In my case it was 'body'.

11. Publish your site and navigate to your 'page' (navigable item you made in step 2). 

There are a couple of ways to check that your setup is truly working and you have the ability to use sitecore fully.

1. Check that you have access to Sitecore context by accessing for instance an item name. In any of your models add a member and then assign a sitecore items' name to it.

MyAccountModel model = new MyAccountModel()
    ItemName = Sitecore.Context.Item.Name

Then in your view display that information:

<h1>You navigated to page: @Model.ItemName</h1>

2. Make your rendering cacheable in sitecore and add a time counter to your view:


As you refresh your page the count should not change. Then set the rendering's Vary By Query String to true and navigate to your item with any query string parameter. Your counter should update.

You are done! Your MVC application is fully integrated into sitecore. 

No comments:

Post a Comment