Using the razor engine in SharePoint 2013 Visual Web Part

anpatel picture anpatel · May 6, 2013 · Viewed 7.9k times · Source

I'm trying to make use of the razor engine in a SharePoint 2013 Web Part. This, I read, shouldn't be too difficult since SharePoint 2013 Visual Web Parts can be built using .NET Framework 4.0

I'm simply not sure of where to begin to do this. I've seen an article on uploading the .cshtml file onto a document library and referencing the document library in the Web Part, but it is a bit unclear.

Does anyone have an idea of how I can start? Or point me to a step-by-step guide to achieve what I am after.

What my end goal is: Take a piece of an MVC application, customize it and make it a Web Part. I need to be able to modify or access SharePoint content on that site which is why I cannot use an App Part.

Answer

Rich Bryant picture Rich Bryant · Aug 1, 2013

Okay.

First install the Office Development Templates from

http://www.microsoft.com/visualstudio/eng/office-dev-tools-for-visual-studio

This will give you the following template selection on reopening Studio

VS Office and Sharepoint Templates

Then navigate to wherever EwsManagedApi32.msi is and enter the following command in an elevated prompt -

EwsManagedApi32.msi addlocal="ExchangeWebServicesApi_Feature,ExchangeWebServicesApi_Gac"

Now your templates are usable and the one you want is "VS2012 Web Part".

Now, by default this will add an ASP.NET project, which is not what you want, so manually delete the ASP.NET project, add an MVC one, highlight the SharePoint project in Solution Explorer, hit F4, and select the MVC project in the "Web Project" dropdown at the bottom of the properties.

And there you go. One controller per web part, sorted.