Question

I'm in the process of creating a sandboxed web part, during debugging though once it is on the site the web part displays the error shown below:

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.

Now I've searched for this error but have thus far found little information. I've checked the list of restrictions, but I'm pretty sure I'm ok there unless System.Web.UI.Controls is not allowed. You can find my code below and any help would be appreciated.

using System.ComponentModel;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;

    namespace FPContentQueryCustom.FrontCusWebPart
    {
        [ToolboxItemAttribute(false)]
        public class FrontCusWebPart : System.Web.UI.WebControls.WebParts.WebPart 
        {
            public FrontCusWebPart()
            {
            }

            protected override void CreateChildControls()
            {
                SPWeb thisWeb = SPContext.Current.Web;
                SPListCollection siteLists = thisWeb.Lists;

                siteLists.ListsForCurrentUser = true;

                DataTable dt = new DataTable();
                dt.Columns.Add("data");

                foreach (SPList list in siteLists)
                {

                    SPListItemCollection listItem = list.Items;

                    foreach (SPListItem item in listItem)
                    {
                        dt.Rows.Add(item.DisplayName);
                    }
                }

                dt.AcceptChanges();

                SPGridView grid = new SPGridView();
                grid.ID = "ExampleGrid";
                grid.AutoGenerateColumns = false;

                BoundField col = new BoundField();
                col.DataField = "data";
                col.SortExpression = "data";
                col.HeaderText = this.DisplayTitle;
                grid.Columns.Add(col);

                grid.DataSource = dt;
                grid.DataBind();

                Controls.Add(grid);
            }
        }
    }
Was it helpful?

Solution

This is a generic error. Sometimes you have a plus-sign in front of the error that allows you to see more details about it.

Check the ULS logs for more details. Also as James says - use debugging - just hit F5 and set a break point in your code.

OTHER TIPS

Try adding the AllowPartiallyTrustedCallers attribute to your code:

using System.Security;
[assembly: AllowPartiallyTrustedCallers]
namespace FPContentQueryCustom.FrontCusWebPart

I think to help debug this and other solutions, you can set a breakpoint in your code and attach to SPUCHostService.exe`, which should hit the breakpoint upon executing the code in your solution.

I suggest this because there might not be an issue with the restrictions in sandbox solutions and your code, but a logic problem in your code itself which is throwing an exception.

` = EDIT: As Wictor suggested, just add a breakpoint and hit F5, no there's need to attach to the process in 2010.

I also face the same issue, using SPgridView, u can use Gridview instead of SPGridView then it should solve ur problem. Regards Sri

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.

If you attach the code to SPUCHostService.exe using F5, then this error is thrown after RenderControl method . So, not sure for where error is coming...

Any help on this. Thanks in advance

I don't think SPGridView is supported in Sandbox

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top