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 am using the below code for retrieving all sharepoint lists from a site.

SPSite site = new SPSite("http://servername:port");
SPWeb web = site.OpenWeb();
SPListCollection SPlists = web.Lists;
foreach (SPList list in SPlists)
  {
    // do something
  }

But while deploying it shows the error

"Unhandled exception was thrown by the sandboxed code wrapper's Execute method in the partial trust app domain: An unexpected error has occurred."

Any ideas how can we get sharepoint lists and list items from a a site in sandboxed solution?

share|improve this question

4 Answers 4

Not 100% suer, but I think that SPSite constructor is not allowed in sandboxed mode, so You have to obtain your SPSite instance differently.

share|improve this answer

The error you are getting is a very generic sandbox error. This error can be thrown due to a variety of reasons by the SharePoint Sandbox.

Can you debug with Visual Studio? I would suggest you use some kind of logging mechanism if you cannot debug your solution directly with VS. Try the following code:

try
{
    using(SPSite site = new SPSite("http://servername:port"))
    {
        using(SPWeb web = site.OpenWeb())
        {
            SPListCollection SPlists = web.Lists;
            foreach (SPList list in SPlists)
            {
                // do something
            }
        }
    }

}
catch(Exception ex)
{
    Page.Controls.Add(new LiteralControl("<div>"+ ex.Message() +"</div>"));
}
share|improve this answer
    
while debugging it does not throw any error! I will get the lists successfully, But the error is shown when in the sharepoint page –  sharmila Oct 29 '12 at 6:56
    
Then I guess this has something to do with the Environment. Make sure all the sandbox processes and services are running properly. –  Vardhaman Deshpande Oct 29 '12 at 7:26
    
They are running....one thing i found that if i use SPContext.Current.Web.Lists then the code will not throw exception. But i need to find a way other than this. any ideas? –  sharmila Oct 29 '12 at 7:55

Can you find what exactly happens using SharePoint Log Viewer?

http://sharepointlogviewer.codeplex.com/

share|improve this answer

You can only access the CURRENT context inside sandboxed code, ergo a correction to this code would be

SPListCollection SPlists = SPContext.Current.Web.Lists;
foreach (SPList list in SPlists)
  {
    // do something
  }
share|improve this answer

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.