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'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.

      Com.SharePointKings.Webparts.Com.SharePointKings.Webparts
        {



      public class CustomListViewWebPart : System.Web.UI.WebControls.WebParts.WebPart
        {
     #region Variable Declaration
      private string strlisttolink = string.Empty;
         private string strViewOfSourceList = string.Empty;
        private string strQuery = string.Empty;

    #endregion
   #region Properties

      //Get and Set Property of the Source List
    [Personalizable(true),
            WebBrowsable(),
          WebDisplayName("List Name"),
         WebDescription("Pass the Name of the List")]
         //Get and Set Property of the Source List
    public string ListToLink
        {
  get
       {
        return strlisttolink;
      }
    set
       {
 strlisttolink = value;
        }
 }

       [Personalizable(true),
       WebBrowsable(),
     WebDisplayName("View"),
  WebDescription("Pass Name of the View that you will like to apply to the List")]
      //Get and Set Property of the Source List
      public string ViewOfSourceList
              {
            get
        {
        return strViewOfSourceList;
    }
       set
     {
   strViewOfSourceList = value;
          }
     }
    [Personalizable(true),
        WebBrowsable(),
      WebDisplayName("Query"),
        WebDescription("Pass the Filter Query")]
      //Get and Set Property of the Source List
    public string FilterQuery
       {
       get
      {
        return strQuery;
      }
      set
     {
          strQuery = value;
           }
         }

      #endregion

       public CustomListViewWebPart()
      {
       this.ExportMode = WebPartExportMode.All;
        }



         protected override void CreateChildControls()
        {
         base.CreateChildControls();

         try
             {

       SPWeb web = SPContext.Current.Web;
           SPList list = web.Lists[ListToLink];



      ViewToolBar toolbar = new ViewToolBar();

        SPContext context = SPContext.GetContext(this.Context,  list.Views[ViewOfSourceList].ID,                list.ID, SPContext.Current.Web);

           toolbar.RenderContext = context;

          Controls.Add(toolbar);




     // Instantiate the web part
       ListViewWebPart lvwp = new ListViewWebPart();
        lvwp.ListName = list.ID.ToString("B").ToUpper();
        lvwp.ViewGuid = list.Views[ViewOfSourceList].
      ID.ToString("B").ToUpper();

     SPView webPartView = web.Lists[ListToLink].Views[ViewOfSourceList];
       SPList objList = web.Lists[ListToLink];
      webPartView.Query = FilterQuery;

    // Remove the Toolbar!
       // First Option: Do it throught OOTB.This needs to be done from the VIEW of the Web  Part
      // Second Option: Do it trhough Coding.This line is required to ensure that all the   appropriate internal nodes of the SPView are populated

      String temp = webPartView.SchemaXml;
      System.Reflection.PropertyInfo ViewProp = lvwp.GetType().GetProperty("View",        System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
      SPView spView = ViewProp.GetValue(lvwp, null) as SPView;
    // This forces a refresh of the views internal xml or the node's cild nodes are not          populated


        PropertyInfo nodeProp = webPartView.GetType().GetProperty("Node",              BindingFlags.NonPublic | BindingFlags.Instance);
      XmlNode node = nodeProp.GetValue(webPartView, null) as XmlNode;

        // Now get the Toolbar node from the view so we can update its type property
           XmlNode toolbarNode = node.SelectSingleNode("Toolbar");
            if (toolbarNode != null)
           {
        toolbarNode.Attributes["Type"].Value = "None";
       web.AllowUnsafeUpdates = true;
       webPartView.Update();
          web.AllowUnsafeUpdates = false;
        }
        //End Remove the Toolbar!


     web.AllowUnsafeUpdates = true;
         webPartView.Update();
      objList.Update();
          web.AllowUnsafeUpdates = false;
                lvwp.GetDesignTimeHtml();
          this.Controls.Add(lvwp);
           }
        catch (Exception ex)
    {
       Label lbl = new Label();
       lbl.Text = "Error occured: ";
       lbl.Text += ex.Message;
       this.Controls.Add(lbl);
         }
          }


         protected override void Render(HtmlTextWriter writer)
          {
     EnsureChildControls();
            base.Render(writer);
        }
       }
share|improve this question

1 Answer 1

up vote 3 down vote accepted

You are not allowed to use System.Reflection, it is prohibited in the SharePoint Sandbox and therefore you get this exception.

share|improve this answer
    
Wictor - SharePoint MVP : Thanks a lot –  ar.gorgin Jan 14 '12 at 6:47

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.