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

September 23 2010 9 comments

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: 4% [?]

9 comments to “SharePoint 2010 Dynamic Compilation of .aspx and .ascx Files with .NET 3.5”

  1. You right!! Think exactly the same

  2. Larry says:

    This is a great tip partіcularly to those new to the blogosphere.
    Brіef but νеry acсurаte info… Thanks
    fοr shaгing this one. A must read article!

  3. Mac says:

    If you want to get a good deal from this piece of writing then
    you have to apply such methods to your won
    webpage.

  4. Nice post. I was checking constantly this blog and I am impressed!
    Very useful info specifically the last part :) I care for such info much.

    I was looking for this particular info for a very long time.
    Thank you and best of luck.

  5. Thanks for sharing such a fastidious opinion, post is fastidious, thats why i have read
    it completely

  6. If some one wishes to be updated with newest technologies therefore he must be pay a quick visit this web page and be up to date every day.

  7. msdt.exe says:

    Wow, awesome weblog structure! How long have you ever
    been blogging for? you made blogging look easy.
    The entire look of your site is excellent, let alone the content!

  8. blog says:

    Yoou can share the tunes you create with people around the globe.
    One of many text-editing tools forr i – OS, Hog Bay Software’s Plain – Teext iis
    my current favorite, mostly because it syncs documents directly to Dropbox (the file sync software I’ve mentioned numerous times).

    Consider the all advantages and disadvantages of both of them and choose the choice that iss going
    to be best for you to understand this application.

  9. Tarah says:

    Flash live video is a format that is designed for web playback, offering
    high rates off compression. Thhe augmented reality based travel app is Lonely –
    Planet that serves the user the city guides and displays the user of the immediate surroundings.
    i -OS is the king nnow but what if Android dominates the
    market in few years time.

Leave a Reply