Todd Baginski's Blog - A SharePoint, Mobile, Office365, PowerApps and Azure Blog

HOW TO: Create a Sandbox Compatible Visual Web Part with the Visual Studio 2010 SharePoint Power Tools

21 June 2010

Comments:

4
 June 21, 2010
 4

Last week Mike Morton and the folks on the Visual Studio team released the Visual Studio 2010 SharePoint Power Tools.  The Visual Studio 2010 SharePoint Power Toolsextend the SharePoint 2010 project templates and provide the following enhancements.

  1. A sandbox-compatible Visual Web Part item template.
  2. Compile-time validation to ensure your code will run the the sandbox.

This short HOW TO walks you through these features and demonstrates how to make a sandbox compatible Visual Web Part with the Visual Studio 2010 SharePoint Power Tools.

To begin, go download the Visual Studio 2010 SharePoint Power Tools and install them on your SharePoint development machine.  Make sure you have Visual Studio 2010 installed first.  Next, open Visual Studio 2010 and create an Empty SharePoint project.

Empty SharePoint Project

When the SharePoint Customization Wizard prompts you, select Deploy as a sandboxed solution.

Sandboxed Solution

After the project is created, right click the project node and select Add, then select New Item.

New Item

In the Add New Item dialog, select Visual Web Part (Sandboxed), provide a name for the Web Part, and click Add.

Sandboxed Visual Web Part Item

Open the sandbox compatible Visual Web Part’s .ascx file and add some code to create the controls.

<br />
<asp:Label ID="Label2" runat="server" Text="Select a ski resort and click the button to see the best lift."></asp:Label>
<br /><br />
Resorts:
<asp:DropDownList ID="ResortDropDownList" runat="server">
<asp:ListItem>Breckenridge</asp:ListItem>
<asp:ListItem>Arapahoe Basin</asp:ListItem>
<asp:ListItem>Copper Mountain</asp:ListItem>
</asp:DropDownList>
<br /><br />
<asp:Button ID="Button1" runat="server" Text="Show best lift" OnClick="ResortDropDownList_Click" />
<br /><br />
<asp:Label ID="BestLiftLabel" runat="server" Text="Best lift:"></asp:Label>
<asp:Label ID="BestLiftValueLabel" runat="server"></asp:Label>

 

Open the sandbox compatible Visual Web Part’s .cs file and add an event handler for the dropdown list.

protected void ResortDropDownList_Click(object sender, EventArgs e)
{
    switch (ResortDropDownList.SelectedValue)
    {
        case "Breckenridge":
           BestLiftValueLabel.Text = "Falcon Chair";
           break;
        case "Arapahoe Basin":
           BestLiftValueLabel.Text = "Palavicini";
           break;
        case "Copper Mountain":
            BestLiftValueLabel.Text = "Storm King";
            break;
    }
}

 

In the Solution Explorer, right click the project node and select Deploy.  Notice the WSP is deployed to the Solutions Gallery where sandboxed solutions are deployed.  (http://<server>/_catalogs/solutions/Forms/AllItems.aspx)

Solutions Gallery

Add the sandbox compatible Visual Web Part to a SharePoint page and test it out.

Best Lift Web Part

Return to Visual Studio and try to add some code (to the sandbox compatible Visual Web Part) which is not permitted in the sandbox.  In this example I added a SharePoint DateTimeControl control.

<SharePoint:DateTimeControl ID="DateTimeControl1" runat="server" />

 

Try to deploy the solution again and notice the error in the Error List which indicates the SharePoint DateTimeControl control is not permitted in the sandbox.

Sandbox Error

As you can see, you can find out before runtime if the code you are trying to run in the sandbox is permitted.  This feature, coupled with the ability to use the visual designer to create sandbox compatible Visual Web Parts certainly saves development time.  Thanks Mike and team!

4 responses on “HOW TO: Create a Sandbox Compatible Visual Web Part with the Visual Studio 2010 SharePoint Power Tools

  1. devi says:

    i have no choice for a sandboxed solution when project creates

    why wrong?

  2. Cornelius J. van Dyk says:

    Nice article Tidd!

  3. Jomit says:

    Indeed a good addition to the existing tools. Also I liked some of the features in the Visual Studio Pro Power Tools like the Pinned Tabs, Triple click for select line, searchable Add Reference dialog and more on which I blogged recently : jomit.blogspot.com/…/…io-2010-power-tools.html

    Cheers,
    Jomit

  4. raka says:

    The visual web-part allows only size of 8k.

    is there any alternative