0
votes

I am very new to Sharepoint programming, like the rest of my team is. We have decided to use smart part as our bridge between sharepoint and our development efforts. After some effort we got it up and running. However, the problem is, that when i use a simple user control for test, with sharepoint om code that gets names of files in a document library, sharepoint gives me a rather helpful "An unknown error has occured". This code works just fine when inside an aspx page. I have written another simple test user control that just executes a Response.Write() line to check is there a problem with executing code, but this one works just fine in smart part too.

Code goes like

protected void Button1_Click(object sender, EventArgs e)
{
            Microsoft.SharePoint.SPSite srv1 =
             new SPSite("http://server:port/");

            SPWeb web = srv1.OpenWeb();
            var list = web.GetFolder("http://server:port/documentLibrary");
            for (int i = 0; i < list.Files.Count; i++)
            {
                ListBox1.Items.Add(list.Files[i].Name);
            }
}

Anything we may be missing or doing wrong? Many thanks in advance...

3

3 Answers

1
votes

AFAIK, Smart Part hasn't been really needed since SharePoint 2003. Why don't you just create a regular user control and plop it in the /ControlTemplates folder? Deploy it is as part of a Feature with related code, if appropriate...

Also, update your Web.Config file to display meaningful error messages:

  • customErrors=off
  • Enable Stack Traces by adding CallStack=”true” to the SafeMode tag
  • Set the compilation debug attribute to "true"
1
votes

Just a side note, you should generally wrap your SPSite and SPWeb objects in a using clause as these are unmanaged objects as outlined here: http://msdn.microsoft.com/en-us/library/aa973248.aspx

protected void Button1_Click(object sender, EventArgs e)
{
 using (Microsoft.SharePoint.SPSite srv1 = new SPSite("http://server:port/"))
 {
  using (SPWeb web = srv1.OpenWeb())
  {
   var list = web.GetFolder("http://server:port/documentLibrary");
   for (int i = 0; i < list.Files.Count; i++)
   {
    ListBox1.Items.Add(list.Files[i].Name);
   }
  }
 }
}
0
votes

Ok it's solved, thanks everybody for information and help.

It was about trust level and i set thrust level to "WSS_Medium" in relevant site collection's web.config file.

<trust level="WSS_Medium" originUrl="" />

I have found this solution (along with some more relevant information on subject) in Jan Tielen's blog at here