Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / WPF

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 44.9K   3  
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:

HTML
<!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:

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:

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

Otherwise, indicate a file like such

XML
<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.


License

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


Written By
Technical Lead
New Zealand New Zealand
http://nz.linkedin.com/in/macaalay
http://macaalay.com/

Comments and Discussions

 
-- There are no messages in this forum --