Search code examples
c#linqsharepointweb-parts

Could Not Load File or Assembly Sharepoint 2007 WebPart


I am playing around with Sharepoint 2007. I have a virtual machine (win server 2k3) with an instance of sharepoint server 2007 running on it. I am now working on creating web parts. I have successfully created simple ones, such as this one that displays text:

public class SimpleWebPart : WebPart
{
    private string _displayText = "Hello World!";
    [WebBrowsable(true), Personalizable(true)]
    public string DisplayText
    {
        get { return _displayText; }
        set { _displayText = value; }
    }
    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
        writer.Write(_displayText);
    }
}

I have this one (and a few test ones) inside of a Class Library, which I put into the _app_bin folder inside of C:\Inetpub\wwwroot\wss\VirtualDirectories\80.

The latest one I added utilizes LINQ to get data from a table I added (not part of Sharepoint):

public class SimpleDBWebPart : WebPart
{
    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
        var oDB = new SPWebPartDataClassesDataContext();
        var oRes = oDB.GetAllFirstTable();

        foreach(var item in oRes)
        {
            writer.Write("<div>Item Name: {0}</div>",item.text);
            writer.Write("<div>Item ID: {0}</div>", item.id);
        }
    }
}

The GetAllFirstTable() is a stored procedure that gets all the data from my test table:

ALTER PROCEDURE dbo.GetAllFirstTable
AS
    SELECT * FROM FirstTable
    RETURN

When I try to add the WebPart to a page, I get this error:

The "SimpleDBWebPart" Web Part appears to be causing a problem. Could not load file or assembly 'System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.

I used Reflector to make sure I have the assembly inside the DLL: alt text

And that appears to be the case. Do I have to add the assembly to the web.config file of the sharepoint site? Or is there something else that I am missing?

Thanks guys!


Solution

  • To use LINQ or .NET 3.5 feature you need to first configure the SharePoint to run in 3.5 Mode. Refer to these links on how to do that

    Simplest way

    AnotherOne