Click here to Skip to main content
14,577,604 members

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

Rate this:
5.00 (1 vote)
Please Sign up or sign in to vote.
5.00 (1 vote)
26 Oct 2010CPOL
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.

Image 1

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" 

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

Image 2

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.

Image 3

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.


Image 4 Image 5 Image 6 Image 7 Image 8 Image 9 Image 10 Image 11

License

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

Share

About the Author

Raymund Macaalay
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 --
Technical Blog
Posted 21 Oct 2010

Tagged as

Stats

42.7K views
3 bookmarked