-
Notifications
You must be signed in to change notification settings - Fork 2
Sharepoint project setup
Download the SharePoint binaries
A console application, GenerateViewCode.exe is used to convert the .cshtml files to .cshtml.cs files that can be compiled into a .NET 3.5 assembly. It can be set up as an external tool in visual studio with the following settings:
- Command: GenerateViewCode.exe
- Arguments: CompiledViews.TemplateBase
- Initial Directory: $(ProjectDir)
- Use Output Window: Yes
Running the tool will regenerate the code for all changed views in the current project.
The project structure follows the structure of a conventional ASP.NET MVC project.
- A views folder contains all the .cshtml files and their associated .cshtml.cs files. Subfolders can be used, but don't mean anything as no routing is available.
- A models folder contains model classes
The project needs a reference to CompiledViews.SharePoint, which contains the necessary view and webpart base classes.
If using VS2010 with ASP.NET 2010, you can get full syntax highlighting and intellisense on the views using the standard editor. To enable this you will need to add a web.config file to the root of the project with appropriate buildprovider settings so that the editor recognizes the project as an MVC web site.
Web parts can be set up in much the same way as standard web parts. The code just needs to inherit from CompiledViews.SharePoint.MvcWebPart.
A basic display web part only needs one method in the web part class - an override of Get that instantiates the model and view classes and returns a ViewResult. Optionally, the Post method can be overridden to handle writes, and any custom properties can be added as in a standard web part.
public class ExampleWebPart : MvcWebPart
{
protected override ActionResult Get()
{
var m = new ExampleModel()
{
Property1="Example Value"
};
return View(new ExampleView(), m);
}
}
In most ways the views are the same as standard ASP.NET MVC 3 Razor views. However, only a subset of the functionality is supported within SharePoint.
- All views are partial views - no support for layouts or nested partial views
- Inline HTML helpers may work - not yet tested
- Dynamic types are not available under .NET 3.5
Views should use the @model syntax rather than @inherits, as the view base class is determined by the code generator rather than by the view itself, keeping the views fully compatible with standard ASP.NET MVC3.