Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a SharePoint site collection. The root Web of the site collection has the URL http://intranet.com. I plan to create a user solution that will contain a Web Part. The Web Part will display the title of the root Web.

I wrote the following code segment for the Web Part.

SPSite currentSite = new SPSite("http://intranet.com");

Label currentTitle = new Label();

currentTitle.Text = currentSite.RootWeb.Title;

I added 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."

What should I do? :(

share|improve this question

1 Answer 1

up vote 3 down vote accepted

In a sandboxed solution you can only access the current site collection, not one specified by URL.

To get the title of the RootWeb of the current site collection use:

currentTitle.Text = SPContext.Current.Site.RootWeb.Title;
share|improve this answer
    
Thanks, after your reply, I tried to use SPSite currentSite = SPContext.Current.Site; which is also working fine!! –  NICK Jun 11 '12 at 11:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.