Add to My Yahoo! | Google Reader or Homepage | Add to Windows Live | Add to Windows Live Alerts

Wictor Wilén

SharePoint Server MVP / Author / MCT / MCTS / MCP / MSc writing about SharePoint and other interesting Microsoft technologies

Visual Web Parts in SharePoint 2010 cannot be Sandboxed

Posted at 2009-11-22 01:28 by Wictor Wilén in Visual Studio , Web Parts , SharePoint 2010 with 7 comments.

Visual Web Parts are a nice addition for SharePoint 2010 developers. A Visual Web Part is essentially SmartPart revisited and is one of the project types of the new SharePoint extensions for Visual Studio 2010. Sandboxing is one of the most interesting features of SharePoint 2010, especially for large scale implementations and hosting providers. The Sandboxing feature, or User Code Solution, allows you to easily upload a SharePoint feature into your farm into a special controlled environment – called a Sandbox. These Sandboxed features can be controlled by the SharePoint administrators so that they do not consume to much resources and automatically disable them if the exceed their limits. Sandboxed solutions also have severe security restrictions – you cannot access all objects or resources that you would like.

Visual Web Parts

The Visual Web Parts allows the developers to create and build Web Parts in a typical Microsoft manner using drag-and-drop. This will allow for better acceptance from the ASP.NET developers who are aiming for a SharePoint developers status (one notch up the ladder :-). It’s not rocket science, and all of use who knows about the excellent SmartPart are glad to see it finally supported out of the box. What happens really is that the WebPart class will load a User Control in the Web Parts CreateChildControls() method and this user control is what you can drag and drop your controls to. The Visual Studio interface nicely makes these operations seamless and takes care of all the wiring for deployment.

The Visual Web Parts will, for good and  bad, make more developers creating Web Parts. In previous versions of SharePoint we had to manually create our Web Parts – which led to either really good code or really bad code (if you didn’t understand how ASP.NET and SharePoint really worked). But with Visual Web Parts I think we will see more ugly-code solutions rapidly developed – fortunately I see some room for some hard core Web Part development experience.

Sandboxed Web Parts

Since you can Sandbox your SharePoint solutions some of you by now feel pretty safe; ok, what if I get bad code – they are running in my sandbox so I just throttle the solution. This is not really the case – Visual Web Parts cannot be Sandboxed!

Since a Visual Web Part loads a User Control (.ascx) from the SharePoint Root it needs access to run the ASP.NET method LoadControl and file system access to the ControlTemplates folder in the SharePoint Root – and this is not allowed by a Sandboxed Solution, and we are back on square on and have to use the manual Web Part creation mode.

I think this is reasonable; for instance if we are to deploy a Sandboxed solution to SharePoint Online – I doubt that Microsoft (or any other host) would like us to deploy stuff into the SharePoint Root (unless we are on a dedicated machine) and there are several more reasons.

Proof of Concept

Just to show for you how it all works and how I first discovered it, and this is still asked on Twitter.

When you create a new project in Visual Studio 2010 it asks you if this project is going to be a Sandboxed or Full-trust solution, but when you choose the Visual Web Part template the Sandboxed option is disabled, so Visual Studio 2010 already there prohibits you from creating Sandboxed Visual Web Parts.

Visual Web Part trus level selection

But what happens if we make a manual Visual Web Part/SmartPart? If we create an empty SharePoint Project and then choose Sandboxed solution.

Let's play in the Sandbox

Then we add a normal Web Part as a new project item, add a user control to the the mapped SharePoint folder ControlTemplates, this will make our solution look like this.

Faked Visual Web Part solution

Finally we add some code to load the user control.

Add some code...

And then when we try to package and deploy it we get a few errors, already here Visual Studio tells us that this can’t be done.

Package fails

You can even try to manually create a WSP and deploy it to SharePoint to see what happens, but I leave that exercise to you…

 

I hope that you by know get the idea about why Visual Web Parts cannot be Sandboxed, and that you (unlike me) don’t try to get Visual Studio 2010 to be able to create Sandboxed Web Parts.

Comments and trackbacks

#  Sandboxed Solutions by Jeremy Thake
Screenshot from websnpr I have added some other bits that aren't supported by sandboxed solutions that didn't get mentioned on MSDN too: http://www.sharepointdevwiki.com/display/sp2010/SharePoint+Sandboxed+Solutions
#  @Jeremy by Wictor
Screenshot from websnpr Awesome! Keep up the SPDevWiki work!
#  Sandboxed Solutions by André Lage
Screenshot from websnpr Other Microsoft Article about sandboxed Solution. http://msdn.microsoft.com/en-us/magazine/ee335711.aspx
#  This video shows otherwise? by Wouter wanna-be
Screenshot from websnpr Sandboxed Visual Web Part Teaser http://blogs.code-counsel.net/Wouter/SharePoint%20Dev%20Tools/Sandboxed%20Visual%20Web%20Part.aspx
#  @Wouter by Wictor
Screenshot from websnpr Yup, you're a ahead of me. You gotta gimme a cpl more days :-) I got work to do also...
#  Visual Studio 2010 in Focus at Great Indian Developer Summit 2010 by Shaguf
Screenshot from websnpr Mehfuz Hossain and Sachin Vinod Rathi is coming this April to India's 3rd Edition of Great Indian Developer Summit (developersummit dot com) to speak about putting the 2010 version of Visual Studio to use, which will be held on 20th April in IIsc, Bangalore
#  hggj by fhgj
Screenshot from websnpr
Make a comment on this post:
Subject:  

Your name:  
Your Url:  
Note: submissions may have to be approved before being visible, so don't submit your comment multiple times.
Real Time Web Analytics