views:

304

answers:

3

Hi, 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...

+2  A: 

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"
IrishChieftain
Thanks for the tip, it has been useful.It seems i am getting this because of some trust level problem.And it goes like :Exception Details: System.Security.SecurityException: Request for the permission of type 'Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' failed.
SirTwilight
Put it in the GAC and try again or up the trust level in your Web.Config file: http://www.codersbarn.com/?tag=/webpart
IrishChieftain
Correction: I should have said "lower" the trust level! :-S
IrishChieftain
if you put the webpart in the bin folder ypu'll need to write CAS policy files. My philosophy is, when the client doesn't explicity forbids it, use the GAC. I've only had it happen once and that was a bank, which kinda makes sence seeing as any dll in the GAC gets automatic full trust and can basically do anything to the system.
Colin
+1  A: 

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);
   }
  }
 }
}
Dylan Berry
Thank for info, didn't knew they weren't managed objects.
SirTwilight
A: 

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

SirTwilight