SharePoint 2010 Dynamic Compilation of .aspx and .ascx Files with .NET 3.5

September 23 2010 No comments yet

What’s the problem?

One of our colleagues, Petri, had a problem with using extension methods in his Visual Web Parts user control (.ascx file). He had developed some extension methods and tried to use them in user control:

<%= DateTime.Now.SomeExtensionMethod() %>

And all he got was this error:

Compiler Error Message: CS0117:
'System.DateTime' does not contain a definition for 'SomeExtensionMethod'

So it seems like this was compiled with a wrong compiler, and this was further revealed by looking at detailed compiler output:

C:\Windows\Microsoft.NET\Framework64\v2.0.50727\csc.exe

Clearly not a .NET 3.5 C# compiler. All I can say is, that SharePoint is not fully compatible with .NET Framework 3.5 out-of-the-box. You cannot use 3.5 language constructs (inline code) in dynamically compiled files by default, unless…

What’s the solution?

Christian had dealed with this same problem about a year ago, so all the respect for him in documenting the problem.

To fix this issue, you need to add the following in you web application’s web.config inside configuration-element (here is C# example):

<system.codedom>
  <compilers>
    <compiler
      language="c#;cs;csharp"
      extension=".cs"
      type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0,
            Culture=neutral, PublicKeyToken=b77a5c561934e089"

      warningLevel="4">
      <providerOption name="CompilerVersion" value="v3.5"/>
      <providerOption name="WarnAsError" value="false"/>
    </compiler>
  </compilers>
</system.codedom>

You can also use WebConfigModification classes or server-wide web.config merging to make this change in web.config.

Here is your SharePoint 2010 web.config.net35.xml merge file:

<?xml version="1.0" encoding="utf-8" ?>
<actions>
<add path="configuration" id="{57DFD0C8-7E2D-4815-9B36-FD165903D775}">
<system.codedom>
  <compilers>
    <compiler
      language="c#;cs;csharp"
      extension=".cs"
      type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0,
            Culture=neutral, PublicKeyToken=b77a5c561934e089"

      warningLevel="4">
      <providerOption name="CompilerVersion" value="v3.5"/>
      <providerOption name="WarnAsError" value="false"/>
    </compiler>
  </compilers>
</system.codedom>  
</add>
</actions>

Place the web.config.net35.xml in 14\CONFIG folder and run (or do your PowerShell magic):

stsadm -o copyappbincontent

Popularity: 5% [?]

Leave a Reply