Académique Documents
Professionnel Documents
Culture Documents
Web Parts
Table of Contents
Developing a Sandboxed Solution with Web Parts ................................................................. 1
Getting Started ..............................................................................................................................................................2
Lab Setup Instructions ...................................................................................................................................................3
Exercise 1 Familiarize with MySites structures ..............................................................................................................4
Exercise 2 Calling SPSecurity..........................................................................................................................................6
Exercise 3 Calling a CAS secured method ......................................................................................................................8
Estimated Time to
Complete This Lab
45 Minutes
demo2010
Additional Resources
NA
There are no
Starter Files for
this lab.
Source
Code
C:\Student\Labs\14_sandboxedsolutions
\Solution
Completed lab
source code in C#
for the
SandboxedWebPa
rt project.
Resources
C:\Student\Presentations\14_sandboxedsol
utions
Supporting
presentations for
the topics in this
lab.
Page 1 of 9
Getting Started
Scenario
This Hands-On Lab contains a number of additional resources in fixed locations. By default, it is assumed that the
base HOL directory is C:\Student and that the labs and additional resources are located under this directory.
Page 2 of 9
Scenario
In the lab setup you will execute a PowerShell script that will create a new SharePoint site collection.
Tasks
Detailed Steps
prompt.
The setup script launches a PowerShell script that creates a sample SharePoint
site at the location http://intranet.contoso.com/sites/Lab14 that you will use in
the next exercises.
Page 3 of 9
Exercise 1
Familiarize with MySites structures
Scenario
In this exercise, we are getting familiar of the different structures available in my sites.
Tasks
Detailed Steps
Note: In this exercise you will create a SharePoint project and set it for Sandboxed
deployment.
demo2010
1. Creating a Visual
Web Part
b.
In the CreateChildControls method, add the code the following code between
the braces:
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);
};
Page 4 of 9
Detailed Steps
Controls.Add(testButton1);
Note: In this task you will start the code in debug mode and add the web part to the
page.
Code
a.
On the toolbar menu, click Debug, and then click Start Debugging.
Internet Explorer loads.
b.
c.
On the ribbon, click the Page tab, and then click Edit.
d.
Place cursor just above the Shared Documents web part by clicking the editor
zone, and on the ribbon, under Editing Tools, click the Insert tab.
e.
f.
g.
h.
i.
j.
k.
The Web Part displays the number of lists in the current site.
Page 5 of 9
Exercise 2
Calling SPSecurity
Scenario
In this second exercise you will add code to the Web Part that will call SPSecurity. You will re-deploy and test the
Web Part by starting debugging from Visual Studio.
Tasks
Detailed Steps
Note: In this task you will add code to the CreateChildControls method that uses the
SPSecurity object.
demo2010
1. Add Code Using the
SPSecurity Object
When typing this code you will notice that Visual Studios IntelliSense will not
recognize SPSecurity as a valid object as it knows the object is unavailable in a
sandboxed solution. When the entire line is typed out it wont add a red underline
indicating a syntax error as you are still building against the full SharePoint object
model.
a.
In the WebPart.cs file, add the following code to the end of the
CreateChildControls class, after the Controls.Add(testButton1); line and before
the closing brace.
Button testButton2 = new Button();
testButton2.Text = "Test 2";
testButton2.Click += delegate
{
try
{
SPSecurity.RunWithElevatedPrivileges(
delegate
{
using (SPSite siteCollection = new
SPSite(SPContext.Current.Site.ID))
{
SPWeb site =
siteCollection.OpenWeb(SPContext.Current.Web.ID);
message.Text = String.Format("This site
contains {0} lists",
site.Lists.Count);
}
});
}
catch (Exception e)
{
message.Text = e.Message;
}
};
Controls.Add(testButton2);
b.
Page 6 of 9
Detailed Steps
Internet Explorer loads.
c.
d.
e.
Page 7 of 9
Exercise 3
Calling a CAS secured method
Scenario
In this exercise you will add code to the Web Part that tries and perform a CAS security protected task. In this
example an HTTP connection is attempted.
Tasks
Detailed Steps
Note: In this task you will add code that attempts an HttpWebRequest. This is blocked
in the sandbox.
demo2010
a.
1. Adding
HttpWebRequest
Code
In the WebPart.cs file, add the following code to the end of the
CreateChildControls class, after the Controls.Add(testButton2); line and before
the closing brace.
Button testButton3 = new Button();
testButton3.Text = "Test 3";
testButton3.Click += delegate
{
try
{
System.Net.HttpWebRequest.Create("http://intranet.conto
so.com");
}
catch (Exception e)
{
message.Text = e.Message;
}
};
Controls.Add(testButton3);
b.
c.
d.
e.
Page 8 of 9
Related Resources:
Microsoft SharePoint Server 2010 Trial Download
Lab Summary
In this Lab you performed the following tasks:
Page 9 of 9