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).
Lastly, for Glass Mapper, you may need to add a line to the compilation nodes in the Web.Config.
<compilation defaultLanguage="c#" debug="false" targetFramework="4.5.2" numRecompilesBeforeAppRestart="2000"> <assemblies> <add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /> |
Great post 🙂
Just a note that setting CopyToLocal=True is a bad idea if you work in Website folder because cleanup or rebuild action will remove all the assemblies from bin folder.
Yep, good point Alen.
And then, if you work inside your website folder you won’t need to do a lot of the things mentioned here (like the App_Config files) as the project will already see them in place.
Thanks so much for this — I spent a day and a half fighting with this problem before I found this post!
In my solution setting CopyToLocal only for Sitecore.Mvc did the trick.
Great instructions. Thanks for posting them out!
This is a cool solution however, if you have a multi project solution like I do 🙂 and you have a TDS project per normal project then you need to do a lot of manual configuration in each TDS project. I am no TDS expert but would be really cool if there could be some general settings for all the TDS projects in a solution.
Thanks Alin.
With TDS versions 5+ there is a way to do this once for all TDS projects in your solution.
Right click on the solution, and select Add TdsGlobal.config file.
You can then set your settings in that file and they will be used by all of your TDS projects.
Better yet, if you don’t want to commit your own settings to source control, you can duplicate that TdsGlobal.config file under the name TdsGlobal.config.user, in the same file system location, and place your settings in there.
That way each developer can have their own settings, and only have to configure them once for the entire solution.
Let me know if this helps.
Thanks
Sean
Nice post 🙂
Nice post Sean!!! Most of my searches in google are landing up on your site or posts. Hereafter for all my queries, I am gonna ping you directly 🙂
Finally, something that I believe helps with Glass Mapper’s intellisense…
You need to add a line to the default Sitecore Web.Config for the System.Core assembly.