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

Wictor Wilén

Microsoft Certified Master (MCM) - SharePoint 2010 | Microsoft Most Valuable Professional (MVP) - SharePoint Server MVP | Author

Custom application pages in the SharePoint 2010 Sandbox

Posted at 2010-05-18 03:00 by Wictor Wilén in Web Parts , SharePoint 2010 with 21 comments.

The Sandbox in SharePoint 2010 allows Site Collection administrators to upload solutions in their Site Collections without access to the physical server. This is a great way to open up for creativity and for each Site Collection owner to expand the functionality of their Site Collection without adventuring the stability of the SharePoint farm. Sandbox Solutions do have some limitations, for instance we cannot add new Application Pages. Application pages are normally pages added to the _layouts folder, which are located in the {SharePoint Root}\TEMPLATE\LAYOUTS physical folder on each server.

Application pages or similar are often used in different scenarios such as when you are building configuration or settings pages. I use them quite often and felt a bit limited with building Sandboxed solutions without them.

Sandboxed solutions still allows us to upload Content Pages, that is pages that are located in a document library for instance. So one workaround is to build a Web Part containing the custom logic and a Content Page that contains this Web Part. Then deploy this to the Solution gallery in your Site Collection. And this is how to do it:

Create a project

Create a new Empty SharePoint 2010 project using Visual Studio 2010 and deploy it as a Sandboxed solution.

Add a Web Part item

Solution ExplorerThen add a new Web Part item to your project. Remove the Web Parts Control Description file (.webpart) and the Elements.xml file. We do not want to add the Web Part to the Web Part gallery, this Web Part will only be used by our Sandboxed Application Page.

Then build your control logic in the Sandboxed Web Part.

Add a Module item

In the Web Part item add a new Module item. Rename the Sample.txt file to AdminPage.aspx or similar - just make sure the extension is .aspx. Your Solution Explorer should then look like the image to the right.

Edit the URL and location in the elements.xml file if you want to deploy it in a document library or any other location. If you have named your items as shown above your page will end up at http://site/AdminPage/AdminPage.aspx.

Edit the ASPX page

Open up the the AdminPage.aspx and build a standard Web Part Page. You can copy the source from a basic Web Part Page using SharePoint Designer for instance. The source should look something like this when you cleaned it up:

<%@ Page language="C#" 
  MasterPageFile="~masterurl/default.master"  
  Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c"%>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" 
  Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<asp:Content ContentPlaceHolderId="PlaceHolderPageTitle" runat="server">
Sandboxed Admin Page
asp:Content>

<asp:Content ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server">
Sandboxed Admin Page
asp:Content>

<asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">
<style type="text/css">
body #s4-leftpanel {
    display:none;
}
.s4-ca {
    margin-left:0px;
}
style>
asp:Content>

<asp:Content ContentPlaceHolderId="PlaceHolderLeftNavBar" runat="server">asp:Content>

<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">

asp:Content>

In this Web Part Page I have removed all unnecessary page directives and only kept the Page directive with the master page reference and the import of the Microsoft.Sharepoint.WebPartPages namespace, which we will soon use. In the PlaceHolderAdditionalPageHead some CSS is added to remove the Quick Launch and I added a page title and heading.

Of course you can use any kind of HTML in here. You cannot use code-behind or inline server-side code though.

Add the Web Part

In the PlaceHolderMain the Web Part will be added. This is done by using the SPUserCodeWebPart, which is a wrapper for Web Parts that lives in the Sandbox. In my case it will look like this:

<WebPartPages:SPUserCodeWebPart 
    runat="server" 
    Description="Admin" 
    Title="Admin" 
    AssemblyFullName="$SharePoint.Project.AssemblyFullName$" 
    SolutionId="473f9e55-bf55-4283-a9ce-3de0b05650f7" 
    ID="adminwp" 
TypeFullName="Wictor.SBAppPage.AdminWebPart.AdminWebPart" > WebPartPages:SPUserCodeWebPart>

Here we need to edit two things. First the TypeFullName must be the full name of your Web Part, just check the .cs file of your Web Part if you don't know how to get it. Secondly we need to set the SolutionId attribute to the solution id value of our solution. You can get a hold of this id by opening up the Package Editor and select Manifest. Then check the Solution element for the SolutionId attribute and just copy it.

SolutionId

The AssemblyFullName attribute contains a Visual Studio replaceable parameter which inserts the full name of your assembly.

Deploy and enjoy!

That's it - just deploy your solution and navigate to your custom sandboxed application page.

Ready for action!

Comments and trackbacks

#  Custom properties by martin
Screenshot from websnpr Do you know how to set custom properties of the web part ?
#  Custom properties by martin
Screenshot from websnpr Do you know how to set custom properties of the web part ?
#  Add this to CKS:DEV by Alex Angas
Screenshot from websnpr Thinking inside the sandbox, I like it! This would make a great template to add to CKS:DEV. Are you able to suggest that Wictor?
#  Re: CKS:DEV by Wictor
Screenshot from websnpr Good idea Alex - I'll see what I can do.
#  Thanks by Ashwini
Screenshot from websnpr Nice Post...
#  Summing up the year of 2010 and embracing 2011 by Trackback
Screenshot from websnpr The time has come for me to do my summary post of 2010. This is my fifth summary post (2006, 2007, 2008 and 2009). This year has been truly amazing. Working in the SharePoint world has been so interes...
#  Files not getting cleaned up. by Gary
Screenshot from websnpr I followed your guide and it was very helpful. The only issue is that after I deactivate and delete the solution, the content pages are left behind. Redeploying the solution (after deleting it) with changes to the content file do not get applied. So for now I am renaming the file every time I make a change. Any ideas? Thank you.
#  @Gary by Wictor
Screenshot from websnpr Files declared in Modules are not removed when retracting the solution. Use the FeatureDeactive or similar to remove the page.
#  Web Part Error by Skita
Screenshot from websnpr Hi: Once deployed the page shows this error, i have added the SolutionID from Package manifest file: Web Part Error: The request could not be completed because the specified solution was not found.
#  Why no code-behind? by John
Screenshot from websnpr Can you explain your comment: "...You cannot use code-behind ...code though."
#  @John by Wictor
Screenshot from websnpr What I mean is that you cannot use your own derivative of the WebPart Page or inline code.
#  Custom application pages in the SharePoint 2010 Sandbox by snreddy
ifollowed your guide and it was very helpful Nice post. The way you written is appreciated. and put your link here. http://sharepointsolution2010.blogspot.com/2011/05/add-custom-aspx-page-in-sharepoint-2010.html
#  Error in Sandboxed Solution by Stephan
Screenshot from websnpr I got the following error: Web Part Error: Unhandled exception was thrown by the sandboxed code wrapper's Execute method in the partial trust app domain: An unexpected error has occurred. In the link to SPUserCodeWebPart Class description on microsoft is a: Available in Sandboxed Solutions: No regards Stephan
#  Error in Sandboxed Solution by Stephan
Screenshot from websnpr Fixed it. I forgot a "
#  Clever Workaroun by Luke McGregor
Screenshot from websnpr This is a pretty clever workaround to the no-custom-codebehind problem :)
#  Modal Dialog Box in SharePoint Sandbox by Trackback
Screenshot from websnpr Earlier I blogged about how to make use of the new dialog framework in SharePoint 2010. It covered working
#  Just saying thanks by Thomas Carpe
Screenshot from websnpr I used this to create an admin page for my sandboxed solution, and it worked very well. Thanks again for the tips.
#  Custom application page in sharepoint by Ankit Singh
Screenshot from websnpr This is one of the best articles so far I have read online. No crap, just useful information. Very well presented. Thanks for sharing with us. Check out this link too its also having a wonderful explanation on how to use custom application page in sharepoint... http://mindstick.com/Articles/6345ba2f-1a8b-4ccf-9c81-d656ea0b4bf2/?Custom%20Application%20Page%20in%20SharePoint Thanks
#  Great stuff by Ernst Wolthaus
Screenshot from websnpr Hi Wictor, Just a comment to say thanks for the blogpost. It made my day :)
#  Error by Suraj
Screenshot from websnpr Hi Wictor, After deploying the sandboxed solution i am getting the following error: Web Part Error: Unhandled exception was thrown by the sandboxed code wrapper's Execute method in the partial trust app domain: The type is not registered as safe. Thanks, Suraj
#  Error by Ravi
Hi Wictor,i have successfully deployed your illustrated example.but when it come to utilize my custom code it is giving me errors for all of asp textbox,labels and other controls.that the name does not exist in current context. any suggestion is highly appreciated..thanks
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.