Wednesday, March 28, 2012

ASP MVC and SharePoint: Revisited

When I first wrote about integrating MVC and SharePoint, I wrongfully stated that one is limited to using MVC 2 and .NET 3.5. I was corrected by Rob Johnson (@bertyJobbo), and there have been numerous requests since to show how to use SharePoint with MVC 3. This post is all about work that Rob sent me, so all credit goes to him.


Here are the step by step instructions:

  1. Create a new MVC 3 project, note that you need to select .NET 4 in the dropdown





  2. Select your view engine and template type





  3. Once the project has loaded, right click on references and add a reference





  4. Select the SharePoint tab, and add the Client Object Model dlls





  5. Add a new controller





  6. Give the controller a name and choose any scaffolding you may want. Mine is empty.





  7. Add a using statement to the top of your controller code, to ensure the client dll is loaded


    using Microsoft.SharePoint.Client;
    


  8. Create the action method below, simply gets the title of the SharePoint site using Client Object Model.


            public string Index()
            {
                ClientContext ctx = new ClientContext("http://demo.xcomplica.local");
                Web web = ctx.Web;
                ctx.Load(web, w => w.Title);
                ctx.ExecuteQuery();
    
                string title = web.Title;
                return title;
            }
    


  9. Run your site, and you should see your SahrePoint site title in your MVC3 app!





9 comments:

Pascal Van said...

Heyhoi,

It's not to insult you in anyway, but I'm having serious issues with these kinds of integrations.

I've seen these kinds of integrations a couple of times already and they all boil down in making SharePoint a data repository.

Let's be honest SharePoint would be one of the most expensive repositories and you're making it yourself very hard without any reason. You're not using any of the SharePoint features.

I would like to see an MVC application running within SharePoint, but that's impossible because we can't enhance the global.asax in a save way.

Joe said...

You make a good point, and I would never recommend a customer only use SharePoint as a data repository. "Just because you can doesn't mean you should", right? But there are valid scenarios where this can be extremely useful.

One such example - you have a group of users that need access to specific data in SP, but they are in the field using iPads. You can try all the apps that are out there, or struggle with the horrible experience on the iPad when browsing a SP site. Or you can create a MVC site that renders modern lightweight HTML and shows these users just the info they need. The rest of your user base can keep using SP as designed.

I hope that clears up why I write posts like this.

AnarchyInTheLK said...

Hi Pascal,

The original thinking for me was that our organisation would adopt Sharepoint internally (ie intranet), managing and viewing data natively - but it would be very useful to be able to quickly leverage some of that data into lightweight applications (ie microsites, mobile apps). And there's nothing more lightweight and clean for a .NET developer than an MVC3 (soon MVC4) application!

Completely agree with your point re "an expensive repository" but it's all about leveraging the power of the Sharepoint's data management, should you already be using it.

AnarchyInTheLK said...

ps - I'm the Rob who Joe mentions (@bertyJobbo)!

Unknown said...
This comment has been removed by the author.
Allan Wellenstein said...

SharePoint is great at managing content (with content types, permissions, workflow, etc.) but can be frustrating in terms of the flexibility it affords you on the frontend, particularly if you are an ASP.NET MVC developer.

We've just released an open source project that allows you to have your cake at eat it too. It allows you to build content managed sites with SharePoint 2010 on the backend (Foundation or Server) and ASP.NET MVC3 on the frontend.

We've been using the approach for 2 years now and want to share it with the community. Documentation is still sparse but take a look - if you have any questions or need support, please don't hesitate to each out.

http://www.slamcms.com

Thanks!

Allan

Konstantin Goroxov said...

Thanks for post!

And it seems that i can use MVC 4 (Framework 4.0) with Sharepoint 2010?

robot said...

Sorry for the newby question but how do I get around the 401 unauthorized error? VS opens its browser and says "Connecting" but then I fall back into VS with WebException was unhandled by user code: The remote server returned an error: (401) Unauthorized.

My SP2010 site allows anonymous and I can browse to the site from the same machine where I'm running VS without authenticating.

for IT the said...

asp.net mvc training | MVC online training | MVC 5 Training | asp.net mvc online training | MVC Training | .net mvc training | C# mvc training | Online MVC Training | asp.net mvc training in chennai | MVC 6 Training | MVC 6 Online Training

Dot Net Training in Chennai |
.Net Online Training | ASP.NET MVC Training in Chennai