Thursday, October 11, 2018

Setup Visual Studio 2017 project to work on Sitecore 9.x MVC

In sitecore documentation, we have the steps to create a local visual studio development environment from Visual Studio 2015 for a Sitecore 9 development.

However, when we switch to Visual Studio 2017, we would face lots of dependency mismatches, which has to be corrected for the project to work without any error.

I have pointed such issues and its resolutions in this blog.

Reference url: (For configuring using Visual Studio 2015) This is the formal sitecore documentation on this topic.

Creating VS Solution in Visual Studio 2017 to work on Sitecore 9 Projects - from scratch:

Prerequisite - .net framework 4.6.2

Note: According to the compatibility matrix  published by sitecore.
Sitecore 9 is compatible with .net framework version 4.6.2 or 4.7
Sitecore 8.2 is compatible with .net framework version 4.6.1 (However, in the sitecore documentation given in the starting of this blog, the .net framework version chosen is 4.6.1. Ideally we should choose 4.6.2)

Step 1: Create an Mvc project with Empty Template

Step 2: Configure the Nuget package manager settings with the source urls of nuget and sitecore respectively.

Nuget Package Manager Settings:

Sitecore Package Source  (Ref)
Nuget Package Source
             
Step 3: delete existing global.asax in the solution created and copy from the sitecore instance.

Step 4: go to root web.config > properties > Mark Build Action to None and Copy to Output Directory to Do Not Copy



From step 1 to step 4, the steps are same as that mentioned in the sitecore documentation url given above. However, when we start doing the same using VS-2017, we start to get various compatibility issues. i.e, version mismatches between certain libraries in sitecore with the libraries in the solution created from VS2017 using .net framework 4.6.2

Step 5: Add below Sitecore Packages to the project using the Sitecore Package Manager Source, given in the Step 2

Sitecore.Kernel
Sitecore.Mvc
Sitecore.Mvc.Analytics

NOTE: In the Solution Explorer in Visual Studio, set the Copy Local property of each dll reference the Nu-Get Package Manager added to False.

Sitecore.Kernel

When you run Sitecore.Kernel, we will get the following dlls as dependencies.


Please note here, we will get these dependencies only when we have created our project with .net framework 4.6.2. Instead if it is 4.6.1, we will get the below error, when we try to update Sitecore.Kernel package.


To solve this, switch the project to point to .net framework version 4.6.2



Sitecore.Mvc

With .net framework 4.6.2 when we try to install Sitecore.Mvc package, we will get the compatibility errors with couple of .net packages, viz.,

Microsoft.AspNet.Mvc
Microsoft.AspNet.WebPages

For a .net framework 4.6.2 project, the default version of Microsoft.AspNet.Mvc and Microsoft.AspNet.WebPages packages is 5.2.4. However, the one which is compatible with Sitecore is, 5.2.3. So we have to change the version of these packages.

Below is the error we would get,



We have to install the write version of these packages i.e., v3.2.3


Below are the dependencies for the Sitecore.Mvc package when we install,


Sitecore.Mvc.Analytics

Below are the dependencies for the Sitecore.Mvc.Analytics package when we install,



Step 7: After installing the packages, add the following configuration in Web.config under Views folder.

        <add namespace="Sitecore.Mvc" />
        <add namespace="Sitecore.Mvc.Presentation" />

After adding these namespaces, you can go ahead and publish the application in the sitecore instance.

How can we enable intellisense in the MVC project for our views?
Ref
http://blog.nikkipunjabi.com/2015/11/intellisense-not-working-for-sitecore.html
http://www.seanholmesby.com/fixing-visual-studio-intellisense-in-sitecore-mvc-views/

The Step 7 above helps us in getting the intellisense for the visual studio project. However, this has the dependency on Sitecore.Web dll and Sitecore.Mvc dll. Only when this dll is present in the application the intellisense would work.

If you follow the procedure given in this blog and install the packages from Nuget Sitecore Package, both Sitecore.Web and Sitecore.Mvc will automatically be installed in your project.

After the publish, the sitecore instance should load fine, which shows that the local environment is ready for our use.

In case, if any of your local instance setup issue is not covered in this blog, please feel free to share the same in your comments.

No comments: