Share via


Exercise 1: Creating the Visual Studio 2010 Project

In this first exercise you will create a new SharePoint project and set it up for sandboxed deployment. You will also author and test the first version of the Web Part.

  1. If you haven’t already done so, run the batch file named SetupLab13.bat, found in the c:\Student\Labs\13_SandboxSolutions\ folder, to create the new site collection that will be used to test and debug the code you will be writing in this lab. This batch file creates a new site collection at an URL of https://intranet.contoso.com/sites/Lab13.
  2. Before you can deploy sandboxed solutions to the SharePoint server, your server must be configured for this. Open SharePoint 2010 Central Administration and choose System Settings >> Manage services on server. Locate the Microsoft SharePoint Foundation Sandboxed Code Service and check its status. If it’s Stopped, click the Start hyperlink to start the service.
  3. Open Visual Studio 2010 and choose to create a new project. Pick Empty SharePoint Project as the template which you find under the Visual C#/Visual Basic » SharePoint » 2010 template group. Name the project SandboxedWebPart. A wizard will appear allowing you to configure the project.
  4. Complete the SharePoint Customization Wizard using the following information:
    1. Debugging site: https://intranet.contoso.com/sites/Lab13
    2. Deploy as a sandboxed solution
    3. Click Finish
  5. Using the Solution Explorer, right-click the SandboxedWebPart project and select Add » New Item. Complete the dialog that appears using the following information.
    1. Visual C#/Visual Basic » SharePoint » 2010
    2. Template: Web Part
    3. Name: WebPart1

      Figure 1

      Create a web part

  6. Using the Solution Explorer, expand Features, right-click Feature1 and choose View Designer. In the designer that appears, verify the Scope. It should be set to Site.

    Figure 2

    Change the feature scope

  7. In the WebPart1.cs/vb tab that opens, locate the WebPart1 class. Add the following code:

    C#

    protected override void CreateChildControls() { Label message = new Label(); Controls.Add(message); Controls.Add(new WebControl(HtmlTextWriterTag.Br)); Button testButton1 = new Button(); testButton1.Text = "Test 1"; testButton1.Click += delegate { message.Text = String.Format("This site contains {0} lists", SPContext.Current.Web.Lists.Count); }; Controls.Add(testButton1); }

    VB.NET

    Public Class WebPart1 Inherits WebPart Dim message As New Label() Protected Overrides Sub CreateChildControls() Controls.Add(message) Controls.Add(New WebControl(HtmlTextWriterTag.Br)) Dim testButton1 As New Button() AddHandler testButton1.Click, AddressOf testButton1_Click testButton1.Text = "Test 1" Controls.Add(testButton1) End Sub Private Sub testButton1_Click(ByVal sender As Object, ByVal e As EventArgs) message.Text = [String].Format("This site contains {0} lists", SPContext.Current.Web.Lists.Count) End Sub End Class
  8. Right click the project in Solution Explorer and choose Deploy in order to deploy the web part.
  9. Open a browser and navigate to the https://intranet.contoso.com/sites/Lab13 site.
  10. On the ribbon, select the Page tab and choose Edit.
  11. Select a Web Part Zone and then click the on the Insert tab in the Editing Tools tab group on the ribbon.
  12. Click the Web Part button in order to show the Web Part pane.
  13. On the Web Part pane, select the Custom group, then select WebPart1 and click the Add button.
  14. On the ribbon, select the Page tab and then select Save & Close from the Edit Section of this tab to stop editing the page.

    Figure 3

    Add the web part to the home page

  15. On the WebPart1, click the Test 1 button in order to test calling the context site collection. The page should refresh displaying a message of how many lists are in the current site:

    Figure 4

    Execute the Test 1 button

Note:
In this exercise you created a new sandboxed Web Part that contained code that is allowed to run in the sandbox.