Fixing Visual Studio IntelliSense in Sitecore MVC Views
IntelliSense in Visual Studio is a fantastic auto-complete feature that greatly improves your Sitecore development, however I’ve occasionally seen this not working in my MVC solutions.
In particular, I was seeing the following error highlighted around any calls to the @Html.Sitecore() helper:-
‘System.Web.Mvc.HtmlHelper<Model>’ does not contain a definition for ‘Sitecore’ and no extension method ‘Sitecore’ accepting first argument of type ‘System.Web.Mvc.HtmlHelper<Model> could be found.’
This would mean that I could not get auto-complete on the @Html.Sitecore() helper, nor would I get auto-complete for my properties on my Glass Models.
While this error had be seen in Visual Studio 2013, I was using the latest update of Visual Studio 2012 (Update-4), and shouldn’t have had this issue.
In order to get IntelliSense working in the Views:-
- Include the Sitecore Web.Config file in your project.
- Include the Sitecore /Views/Web.Config file in your Views folder. (it should have a reference to the Sitecore.Mvc namespace).
- Include some necessary App_Config files in your project’s directory. (You can still have these excluded from your project, or you can follow the ‘Build Action: None’ steps below)
- You will need the App_Config/Prefetch folder, and the configs in the root of App_Config (not the Include or Security folders).
- Ensure Sitecore.Kernel.DLL and Sitecore.Mvc.DLL are referenced in your project, and are set to CopyLocal=true.
- we need these DLLs to end up in the bin directory of the project.
Now, typically we’re told to have CopyLocal set to false for Sitecore DLL references, as it can slow down the build…but here we need it to enable IntelliSense.
What we can do is have the project use CopyLocal=true, then exclude the DLLs in the TDS project settings. TDS even has a default setting for this, where Sitecore.*.DLL is excluded.
This means that the DLLs are copied to the project’s bin directory, but they’re not copied across to my website’s bin directory. (Remember, you should be working outside of the webroot).
With the configs added to the project, I can also make TDS ignore the configs by ensuring that each config file’s Build Action property is set to None. This tells TDS not to copy that file during the build process. (if you don’t need the file, you could just not include it in the project…just have it sitting on the file system in the project’s directory).
Now, my project has everything it needs for IntelliSense, and I’m not pushing files to my website that I shouldn’t be. (they’re already there anyway).