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

By | October 21, 2010

If you have a developer background, your company uses Sharepoint and they want you add a lot of functionalities to that Sharepoint instance, by nature as a programmer you will try to modify the aspx files and use your choice of .Net language.  So you started to download Sharepoint desinger and have a look on what’s running under the hood and how to modify it.

After downloading Sharepoint designer it also occured 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 excited to do coding so you tried modify your new aspx file to look like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ Page Language="C#" %>
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">

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

<body>

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

</html>

Then you tried to browse on it and this appears:

An error occurred during processing of “YOUR PAGE URL”. Code blocks are not allowed in this file.

Your first reaction is WTF, you let me do the coding but you it wont allow me to run it.

To be able to code in Sharpoint 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.


6 thoughts on “How to enable code blocks in Sharepoint 2010 (Works also in 2007)

  1. Ranjani

    Hi,

    I gave

    above in web.config but i am still getting the code blocks are not allowed error.
    I tried giving /Pages/Search.aspx. It dint work either. Search is the publishing which i detached from the page layout and wrote inline code. Please let me know how to fix this

    Reply
    1. rsmacaalay

      What do you mean by Search is publishing? Also take note that web config is case sensitive and make sure the path is correct

      Reply
  2. Pingback: Migrating Sharepoint 2010 Custom Farm Solutions to Sharepoint 2013 | Raymund Macaalay's Dev Blog

  3. Roy

    it doesn’t work in sharepoint 2013 unless you do this

    do you any alternative because this is not accetable to allow scripting on all the pages…

    Reply

Leave a Reply