070-573 Free Dumps Study Materials
Question 3: You have a SharePoint site collection. The root Web of the site collection has the URL
http://intranet.
You plan to create a user solution that will contain a Web Part. The Web Part will display the title of
the root Web.
You write the following code segment for the Web Part. (Line numbers are included for reference
only.)
01 SPSite currentSite = new SPSite("http://intranet");
02
03 Label currentTitle = new Label();
04 currentTitle.Text = currentSite.RootWeb.Title;
You add the Web Part to a page in the root Web and receive the following error message: "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."
You need to prevent the error from occurring.
What should you do?
A. Add the following line of code at line 02: currentSite.OpenWeb();
B. Add the following line of code at line 02: currentSite.OpenWeb("http://intranet");
C. Change line 01 to the following code segment: SPSite currentSite = SPContext.Current.Site;
D. Change line 04 to the following code segment: currentTitle.Text = currentSite.OpenWeb().Title;
Correct Answer: C
Explanation:
MNEMONIC RULE: "sandboxed = SPContext"
OpenWeb() method returns SPWeb object, so answers A and B are incorrect, since they assume
OpenWeb() method doesn't return an object.
Answer D is incorrect for the same reason.
This constructor is allowed in sandboxed solutions. in that case, the value of the requestUrl
parameter
must resolve to the parent site collection in which the sandboxed solution is deployed.
If the value of the requestUrl parameter resolves to the URL of any other site collection, the
constructor
throws an exception because a sandboxed solution is not allowed to access any SharePoint objects
outside its hosting site collection.
SPSite Constructor (String)
http://msdn.microsoft.com/en-us/library/ms466911.aspx