SP2010 and MVC 3

With this post I am looking into which methods are being used for integration SP2010 and MVC 3.

A couple of years ago

I started looking at MVC 2 integration into Sharepoint https://rasor.wordpress.com/2010/03/23/mvc-incorporate-into-sharepoint/ but I never did any PoC.

Learning MVC2:


Last year

I watched a demo by Mikko Salmi integrating SP2010 with MVC 3.
Slides: http://www.sharepointsaturday.org/copenhagen/Shared%20Documents/Speaker%20Slides.zip

MVC 3 has the problem that MVC 3 uses .NET 4.0 – Sharepoint 2010 runs under .NET 3.5.
Solution was to convert some code to .NET 3.5. It was already done by Tom Clarkson

 From readme.txt:

Precompiled Razor Views

This project contains tools and base classes for using Razor views in scenarios where standard ASP.NET MVC 3 is not an option.

 * Use cshtml to generate view code that can run without .NET 4.0
 * Use standard Razor views without a dependency on the server having a working Razor BuildProvider
 * Compile shared views into a seperate assembly
 * Use Razor views and MVC concepts to build SharePoint web parts

Here is a small usage sample.

It integrates fine with http://jqtouch.com/ for mobile web apps hosted in Sharepoint. More recent library is http://jquerymobile.com/blog/2011/11/16/announcing-jquery-mobile-1-0/.

This is a quite neat solution. It is possible to use Razor Views. (Intro, Sample,  Vid)
I didn’t do any PoC there either.


Get up to date with Microsoft

On top of SP2010 / Windows server 2008 R8 / IIS 7.5 / .NET 4.0 / Visual Studio 2010 / MVC 2 / install in following order:

Optionally get up to latest with:

Following the Tom Clarkson track from last year

To start with here are some kind of roadmap:

  1. We want to achieve: https://github.com/tqc/RazorEngine/wiki
  2. HowTo achieve: https://github.com/tqc/RazorEngine/wiki/Sharepoint-project-setup
  3. Maybe instead this VS extension RazorGenerator can be used: http://visualstudiogallery.msdn.microsoft.com/1f6ec6ff-e89b-4c47-8e79-d2d68df894ec?SRC=VSIDE
  4. Tom’s code is based on Matt’s code: http://razorengine.codeplex.com/
  5. Matt has updated his base: http://www.fidelitydesign.net/?p=473 , so maybe Tom’s code needs to be updated likewise.
  6. Tom’s code build View code from templates. Rick teaches how to host your own RazorEngine, so you can use Razor syntax outside MVC WebApps: http://www.west-wind.com/weblog/posts/2010/Dec/27/Hosting-the-Razor-Engine-for-Templating-in-NonWeb-Applications 
  7. NerdDinner is a school MVC sample from Hanselman and Co.: http://www.hanselman.com/blog/NerdDinnerBeingUpdatedToMVC3WithRazorHTML5GeoLocationEFCodeFirstJQueryMobileYepNopeAndModernizrAndAFixedMobileDeviceCapabilitiesViewEngine.aspx

So trying a PoC this way…

  • Build a master-detail model – use Linq to Objects
  • Build a dynamic data project around
  • Build a MVC3 project around
  • Use RazorGenerator to create cs
  • Change .NET from 4.0 to 3.5
  • Build (SP) Webparts around

Integrate MVC into ASP.NET links:


Slam CMS

Update: After testing it appears to me that Slam CMS provides a connection from ContentTypes and Lists to tables in the MVC sites database – Like Enterprise Contenttypes connected to LoB databases.

It also provides some homemade publishing features on the SP Foundation license, so here could be a business case to use it in.

It does not look as if it provides a View for MVC models.


It has become time to look at it again. Allan Wellenstein suggests to use http://www.slamcms.com . See what it provides:

The Technology

SlamCMS utilizes a collection of development frameworks (Autofac, Razor, Dapper, Entity Framework, the MVC Mini Profiler, Elmah) to provide a first class application development experience.
You write no SharePoint object model calls, CAML queries or stored procedures.
Instead, you use the SlamCMS Query Engine: a high-level, fluent API query builder capable of querying content across sites and site collections that can also query users, sites and SharePoint groups.
An image server that lets you request (and cache) images at any size, either with a locked aspect ratio or automatically cropped to your specified dimensions.

SlamCMS features a set of SlamFIELDs for:

  • Managing HTML
    (using the popular CK editor)
  • Uploading large files
    (including videos) to Amazon’s S3
  • Uploading images as fields
    in a SharePoint list.
  • Building your own application-specific SlamFIELDs.

That looks as a promissing integration.

Opposed to Tom’s solution SlamCMS uses both a SharePoint sitecollection and a ASP.NET site. This allows for using different .NET versions: Sharepoint 2010 uses 3.5.1, MVC 3 uses 4.0 and MVC 4 uses 4.5.


Installing SlamCMS:

  • Update: The installation did not fully succeed in my envir, so following is only a procedure proposal.
  • Download from http://slamcms.codeplex.com/ (using [v.18 – Jun 15 2012 – beta] in this test)
  • Copy the sample to a setup folder e.g. C:\Data\CodeSamples\SPSlamCms\SlamCMSDeployment\ (hereafter called \<inst>\)
  • Installing Demo Site
    • The demo uses a Slam sitecollection running in SP2010 (below running in http://slamspdemo.example.xx/) and a Slam MVC 3 site (below called SlamFeDemo) running on .NET4.0. The MVC 3 site connects to its own database.
    • Following \<inst>\readme.txt – PRE-REQUISITES:
      1. In SSMS Create a Database with the name of your choice e.g. SlamFeDemo
      2. Create and assign a user that has db_owner and db_securityadmin roles on the SlamFeDemo database. In the following an exisiting SP AppPool account is reused. You could create a new one, that should only be used by the SlamFeDemo site.
      3. Script for above bullets:
        USE [master]
        ( NAME = N'SlamFeDemo', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\SlamFeDemo.mdf' , SIZE = 2048KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
         LOG ON
        ( NAME = N'SlamFeDemo_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\SlamFeDemo_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
        USE [SlamFeDemo]
      4. Add http://slamspdemo.example.xx to your hosts file or DNS.
      5. From CentralAdmin Create a Sharepoint Webapplication (if you are not reusing one) e.g. http://slamspdemo.example.xx
        • Port: 80
        • Host Header: slamspdemo.example.xx
        • Create new Application Pool: SharePoint - slamspdemo.example.xx80 (uses .NET2.0 opposed to .NET4.0 in slamfedemo)
        • Database: WSS_Content_slamspdemo80
      6. Don't create a sitecollection - that will be done in a ps1 script.
    • Following \<inst>\Demo\site\readme.txt - RUNNING THE WEBSITE:
      1. Add http://slamfedemo.example.xx to your hosts file or DNS.
      2. Create folder C:\inetpub\wwwroot\slamfedemo.example.xx80\site
      3. Give user MYDOMAIN\SPAppPoolsAcc permissions to access the folder
      4. In IIS Manager create a site to host the SlamFeDemo site:
        • Server - Application Pools - Name: SlamFeDemo, Version: v4.0, Integrated
        • Server - Application Pools - SlamFeDemo - Advanced Settings - Identity - Custom Account: MYDOMAIN\SPAppPoolsAcc
        • Server - Sites - Add Web Site - Name: slamfedemo.example.xx, AppPool: SlamFeDemo, Path: C:\inetpub\wwwroot\slamfedemo.example.xx80\site, Host name: slamfedemo.example.xx
        • Server - Sites - slamfedemo.example.xx - IIS - Authentication - Anonymous: Disable, Windows: Enable
      5. In Visual Studio - Server Explorer - Connect to the SlamFeDemo database.
      6. On the SlamFeDemo database select properties to copy connection string:
        • Data Source=MyServer;Initial Catalog=SlamFeDemo;Integrated Security=True
      7. In \Demo\site\web.config paste the connection string into <add name="Database" connectionString="paste here;"....
      8. Verify under Control Panel - Programs that Microsoft ASP.NET MVC 3 is installed on the server.
      9. Don't access the site before the SP SiteCollection has been setup below.
    • Edit \inst\Demo\config\Environment.config. You might need to edit (... will be updated):
      • <NetworkCredential ClaimsAuthenticationType="Ntlm" UserName="USER" Password="PASS" Domain="DOMAIN"></NetworkCredential>
    • Setup the SP SiteCollection by following \<inst>\readme.txt - SETUP CONFIGURATION:
      1. Open \<inst>\setup.config
      2. Add settings from previous bullets
      3. <add key="Slam.ConfigurationManager.Config" value="C:\Data\CodeSamples\SPSlamCms\SlamCMSDeployment\Demo\config\slam.config" />
        <add key="SolutionDirectory" value="C:\inetpub\wwwroot\slamfedemo.example.xx80\" />
        <add key="FrontEndDirectory" value="site" />
        <add key="PublishingSiteUrl" value="http://slamspdemo.example.xx" />
        <add key="FrontEndUrl" value="http://slamfedemo.example.xx" />
        <add key="ConnectionString" value="Data Source=MyServer;Initial Catalog=SlamFeDemo;Integrated Security=True;" />
        <add key="ImportDirectory" value="Demo" />
      4. Edit \inst\Demo\config\Environment.config. You might need to edit:
        • <NetworkCredential ClaimsAuthenticationType="Ntlm" UserName="USER" Password="PASS" Domain="DOMAIN"></NetworkCredential>
      5. You might have problems reading setup.config from the .ps1 script. If that is the case you need to edit setup.ps1 and do a change according to https://rasor.wordpress.com/2012/09/10/config-files-for-powershell/ :
        [System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $configPath)
        #$SlamConfig = [System.Configuration.ConfigurationManager]::AppSettings["Slam.ConfigurationManager.Config"]
        #$ConnectionString = [System.Configuration.ConfigurationManager]::AppSettings["ConnectionString"]
        #$FrontEndUrl = [System.Configuration.ConfigurationManager]::AppSettings["FrontEndUrl"]
        #$FrontEndDirectoryName = [System.Configuration.ConfigurationManager]::AppSettings["FrontEndDirectory"]
        #$PublishingSiteUrl = [System.Configuration.ConfigurationManager]::AppSettings["PublishingSiteUrl"]
        #$SolutionDirectory = [System.Configuration.ConfigurationManager]::AppSettings["SolutionDirectory"]
        #$ImportDirectory = $(Get-Location).Path + "\" + [System.Configuration.ConfigurationManager]::AppSettings["ImportDirectory"]
        [xml]$configXml = Get-Content $configPath
        $SlamConfig = $($configXml.configuration.appSettings.add | where { $_.key -eq "Slam.ConfigurationManager.Config" }).value
        $ConnectionString = $($configXml.configuration.appSettings.add | where { $_.key -eq "ConnectionString" }).value
        $FrontEndUrl = $($configXml.configuration.appSettings.add | where { $_.key -eq "FrontEndUrl" }).value
        $FrontEndDirectoryName = $($configXml.configuration.appSettings.add | where { $_.key -eq "FrontEndDirectory" }).value
        $PublishingSiteUrl = $($configXml.configuration.appSettings.add | where { $_.key -eq "PublishingSiteUrl" }).value
        $SolutionDirectory = $($configXml.configuration.appSettings.add | where { $_.key -eq "SolutionDirectory" }).value
        $ImportDirectory = $($configXml.configuration.appSettings.add | where { $_.key -eq "ImportDirectory" }).value
      6. As with setup.ps1 do also change \<inst>\import.ps1 to read the setup.config file as above.
      7. Open cmd.exe and set the \<inst>\ folder to be current directory.
        Note: The \inst\ folder might not contain spaces (" ").
      8. Execute  setup.bat to setup the Sharepoint SiteCollection and the MVC site.
      9. Notes. The scipts did not run without errors:
        • SP features were not activated.
        • Some initial data seamed to be missing in order for \demo\content.sql to run correct
        • \demo\config\slam.config seemed to have to be manually copied to the root of the SP webapp: \wss\VirtualDirectories\slamspdemo.example.xx80\
        • Configure SLAM in SP via: http://slamspdemo.example.xx/_layouts/SLAM/SLAMControlPanel.aspx
      10. Test the Slam sites
    • Test the MVC site
  • Test the solution

If you want to use the demo site from SlamCMS it is using various 3rd party libraries that are located in the \bin\ folder of the downloaded package.

Miscellaneous other:

(*1) Install into Visual Studio using Tools - Extension manager - Updates
(*2) Install into Visual Studio solution using NuGet Package Manager (Tools - Library Package Manager)
(*3) Dll to include into Visual Studio solution
(*4) Source file to include into Visual Studio solution
(*5) Script to link to or to download


Learning MVC 3

You might want to migrate a web form app to MVC: http://www.rachelappel.com/migrate-to-asp.net-mvc-3-by-leveraging-your-asp.net-web-forms-skills






If you think MVC is not what you want, but you still want separation and testability, then look at MVP in Web Parts.
I can recommend the book SharePoint 2010 Web Parts in Action by Wictor Wilén.

A blog on the subject: http://annagacc.wordpress.com/2011/04/29/implementing-mvp-model-view-presenter-pattern-in-sharepoint-solutions/

The End.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: