Click here to Skip to main content
14,846,774 members
Articles / Desktop Programming / WPF
Technical Blog
Posted 21 Oct 2010

Tagged as


3 bookmarked

How to enable code blocks in Sharepoint 2010 (Works also in 2007)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
26 Oct 2010CPOL1 min read
How to enable code blocks in Sharepoint 2010 (Works also in 2007)

If you have a developer background, your company uses Sharepoint and they want you to add a lot of functionalities to it by nature, you will try to modify the aspx files and use your choice of .NET language. So you started to download Sharepoint designer and have a look at what's running under the hood.

It also occurred to you that you can create custom forms as when you right click on Site pages, you can add an aspx file.

Now you added your new file and are excited to do coding so you tried to modify your new aspx file to look like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
<%@ Page Language="C#" %>
<html dir="ltr" xmlns="">

<head runat="server">
<meta name="WebPartPageExpansion" content="full" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled 1</title>


<form id="form1" runat="server">
<script runat="server"> 
protected void Page_Load(object sender, EventArgs e)
	Response.Write("Enable Code Blocks");


Then you tried to browse on it and this appears:

Code blocks are not allowed in this file.

Your first reaction is WTF!

To be able to code in Sharepoint, you need to enable it on web config. To enable it, go to your WSS Virtual Directory. The default directory is C:\inetpub\wwwroot\wss\VirtualDirectories\80 and modify your web.config (back it up first).

Now under Configuration –> Sharepoint –> SafeMode –> PageParserPaths, add a new section for PageParserPath.

To enable code blocks on a Folder, do a wildcard like such:

<PageParserPath VirtualPath="/TeamSite/CustomForms/*" 
    CompilationMode="Always" AllowServerSideScript="true"  IncludeSubFolders="true"/>

Otherwise, indicate a file like such

<PageParserPath VirtualPath="/TeamSite/CustomForms/Test.aspx" 
    CompilationMode="Always" AllowServerSideScript="true" /> 

Now there are some additional properties you can assign like CompilationMode, AlowServerSideScript and IncludeSubFolders and I guess the two latter parts are self explanatory but for CompilationMode, you have the following options:

  • Always – The default value, which compiles the page always
  • Auto – Page will not be compiled if possible
  • Never – The page will not be dynamically compiled

Once you have done that, the piece of code above should work, and be able to treat that aspx page like a normal aspx page on your web project.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Raymund Macaalay
Technical Lead
New Zealand New Zealand

Comments and Discussions

-- There are no messages in this forum --