Code-blocks are not allowed in this file: Using Server-Side Code with SharePoint
If you use the Microsoft Office SharePoint Designer to add a new page to your site, you will see that it looks just like any other ASP.NET page. Open up your site with the SharePoint Designer, and then go to the Pages folder. Right-click the Pages folder and choose New / ASPX. That will generate a new ASP.NET page with the following default template:
<%@ Page Language="C#" %>
<html dir="ltr">
<head id="Head1" 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>
</body>
</html>
You can see this is the same ol' ASP.NET. There is a Page directive, and the familiar form element with the runat attribute set to "server". Yep, same ASP.NET code. The first thing I tried to do was to add some server-side code to my new page and see it actually work.
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
form1.InnerHtml = "<h1>Hello</h1>";
}
</script>
When I hit F5, though, I got this error message:
"An error occurred during the processing of /Pages/test.aspx. Code blocks are not allowed in this file."
Hmm… yeah, code blocks are allowed in ASP.NET pages. What's going on? Oh yeah! SharePoint disables the ability to create server-side script by default, you have to turn it on. You do that in the web.config file, in the configuration/SharePoint/PageParserPaths configuration section:
<PageParserPaths>
<PageParserPath VirtualPath="/pages/test.aspx" CompilationMode="Always" AllowServerSideScript="true" />
</PageParserPaths>
Success! Now, when I view the page in the browser, the page shows the Hello message as an H1 element instead of that error message about "code blocks are not allowed in this file", because we explicitly enabled code blocks in the web.config file.
A word of caution, here. The VirtualPath attribute accepts wildcards. Be cautious about allowing any ol' page to have server side script, you can restrain to a single page (as I did above) or only a subset of pages or directories.
Comments
Anonymous
May 09, 2007
The comment has been removedAnonymous
June 28, 2010
When I first started developing with SharePoint, I wanted to learn how to do the things that I did inAnonymous
June 28, 2010
When I first started developing with SharePoint, I wanted to learn how to do the things that I did inAnonymous
June 28, 2010
See the attachment to this post for the full source code. When I first started developing with SharePointAnonymous
June 29, 2010
See the attachment to this post for the full source code. When I first started developing with SharePointAnonymous
November 13, 2014
Thanks for putting that caution in there Kirk!Anonymous
November 14, 2014
A word of caution. This post was written in 2007. Since then, we have learned quite a lot about SharePoint customizations, scalability, and the lessons learned from introducing custom code in a platform that you do not control. The recommendation is to not introduce server-side code to SharePoint, but rather use the SharePoint 2013 app model. See http://dev.office.com for more information.Anonymous
February 15, 2015
I only have access to the site through Sharepoint Designer 2013. How do I enable scripting in this case?Anonymous
February 15, 2015
@Prashant - only the administrator would enable this, if you do not have farm administrator privileges with access to the .config files, then you are unable to make this change. A word of caution. This post was written in 2007. Since then, we have learned quite a lot about SharePoint customizations, scalability, and the lessons learned from introducing custom code in a platform that you do not control. The recommendation is to not introduce server-side code to SharePoint, but rather use the SharePoint 2013 app model. See http://dev.office.com for more information.Anonymous
March 31, 2015
Dear Kirk, Thank you for a prompt reply. I am sorry for a late reply.Anonymous
January 12, 2016
so where is that web.config located? Ill look at the app model as well but I want to try this in my SharePoint 2013 sandbox