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.
- 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.
- 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.
- 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.
- Complete the SharePoint Customization Wizard using the following information:
- Debugging site: https://intranet.contoso.com/sites/Lab13
- Deploy as a sandboxed solution
- Click Finish
- Using the Solution Explorer, right-click the SandboxedWebPart project and select Add » New Item. Complete the dialog that appears using the following information.
- Visual C#/Visual Basic » SharePoint » 2010
- Template: Web Part
Name: WebPart1
Figure 1
Create a web part
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
- In the WebPart1.cs/vb tab that opens, locate the WebPart1 class. Add the following code:
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); }
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
- Right click the project in Solution Explorer and choose Deploy in order to deploy the web part.
- Open a browser and navigate to the https://intranet.contoso.com/sites/Lab13 site.
- On the ribbon, select the Page tab and choose Edit.
- Select a Web Part Zone and then click the on the Insert tab in the Editing Tools tab group on the ribbon.
- Click the Web Part button in order to show the Web Part pane.
- On the Web Part pane, select the Custom group, then select WebPart1 and click the Add button.
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
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
In this exercise you created a new sandboxed Web Part that contained code that is allowed to run in the sandbox.