Thursday, December 16, 2010

Use C# 3.0 features in application layout pages in Sharepoint

During everyday Sharepoint development we often use application layout pages (aspx pages which are located in 12/Templates/Layouts folder on file system of web server) for tracing and debugging. It is useful technique, as you can include server-side script directly into the page and show results on the page in context of the living site. Suppose that we have sub folder 12/Templates/Layouts/Test and we created simple aspx page here with the following code:

   1: <%@ Page Language="C#" %>
   2: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   3: <html xmlns="http://www.w3.org/1999/xhtml" >
   4: <head id="Head1" runat="server">
   5:   <title>Test</title>
   6: </head>
   7: <body>
   8:   <form id="form1" runat="server">
   9:     <%
   1:  int i = 1;
   2:        this.lbl.Text = i.ToString();
   3:     
%>
  10:     <asp:Label ID="lbl" runat="server" />
  11:   </form>
  12: </body>
  13: </html>

Look at the code inside <%%>. It is very simple example: it just assigns integer value to variable “i” and shows it in the label. In real life you will execute some meaningful code and trace its result in the label or in another control (e.g. display current user identity).

However it is annoying that by default you are limited with C# 2.0 features, and can’t use features available in C# 3.0 (see Overview of C# 3.0 for full list). E.g. if instead of “int i = 1;” you will write “var i = 1;” (i.e. if you will use Implicitly Typed Local Variables), then you will get the following error when you will try to access your page:

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\test\test.aspx(9): error CS0246: The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)
   at System.Web.Compilation.AssemblyBuilder.Compile()
   at System.Web.Compilation.BuildProvidersCompiler.PerformBuild() 
   … 
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Recently my colleague showed me the tip how to avoid this issue (unfortunately he doesn’t blogging so he is not able to publish it by himself). So in order to enable C# 3.0 features in application layout pages you should create the following web.config file in your sub folder (12/Templates/Layouts/Test in our case):

   1: <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
   2: <configuration>
   3: <system.codedom>
   4:   <compilers>
   5:     <compiler
   6:       language="c#;cs;csharp"
   7:       extension=".cs"
   8:       type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0,
   9:             Culture=neutral, PublicKeyToken=b77a5c561934e089"
  10:       warningLevel="4">
  11:       <providerOption name="CompilerVersion" value="v3.5"/>
  12:       <providerOption name="WarnAsError" value="false"/>
  13:     </compiler>
  14:   </compilers>
  15: </system.codedom>
  16: </configuration>

This web.config overrides compiler version (see <providerOption name="CompilerVersion" value="v3.5"/>) used by ASP.Net, so you will be able to write C# 3.0 code in your pages after that. It helped us to write application layouts page more effectively and I hope it will simplify your everyday Sharepoint development and debugging too.

No comments:

Post a Comment