SharePoint 2010: Allow Server Side Code – Inline Code Blocks

One of the great features of SharePoint is that it is built on-top of .Net, thus allowing nearly unlimited modifications via additional code. Most of the time this additional functionality would be added via Web Parts, features, etc, but sometimes it would be nice to add inline code to a specific SharePoint managed aspx page. But by default SharePoint does not allow code blocks in .aspx pages.

The way around this is well documented but I keep getting asked can you add code blocks directly in your page, and if so, how. It’s quick and easy so let me explain.

First, an example

Say you want to create your own Search Results page. There are many ways to do this and I will leave a full explanation to another article. For this example I simply made a copy of the SearchResults.aspx file found in the Layouts directory in the 14 hive, added it to the root directory of my SharePoint site in SharePoint Designer and renamed it OSSSearchResults.aspx. Anyhow, say in your Search Results page you want to have the search string displayed somewhere on your page. You might add a line of code like:

<h1><sharePoint:EncodedLiteral runat="server" text="<%$Resources:wss,searchresults_pagetitle%>" EncodeMethod='HtmlEncode'/> : <% SPHttpUtility.HtmlEncode((Request.QueryString["k"].Length > 20) ? (Request.QueryString["k"].Substring(0, 20) + "...") : Request.QueryString["k"], Response.Output); %></h1>

Easy enough. This code will take the Querystring, and if it’s over 20 characters long, truncate it, and display it one the page. Yes, you normally would not blindly trust the user’s input and display it on the page, even with SharePoint security, but for this example, it works. If you add this code block, then load your page in a browser, you receive an error, “An error occurred during the processing of /OSSSearchResults.aspx. Code blocks are not allowed in this file.”


To enable code blocks in this file, on the server you need to open this site’s web.config file, by default found in c:inetpubwwwrootwssVirtualDirectories”the site directory”, and look for the following code block in the <Configuration> -> <SharePoint> section.

<safeMode MaxControls="200" CallStack="false" DirectFileDependencies="10" TotalFileDependencies="50" AllowPageLevelTrace="false">

By default the <PageParserPaths> section is blank. All you need to so is add the following to the <PageParserPaths> section:

<pageParserPath VirtualPath="/osssearchresults.aspx" CompilationMode="Always" AllowServerSideScript="true" />


<safeMode MaxControls="200" CallStack="false" DirectFileDependencies="10" TotalFileDependencies="50" AllowPageLevelTrace="false">
      <pageParserPath VirtualPath="/osssearchresults.aspx" CompilationMode="Always" AllowServerSideScript="true" />


You can use wildcards for the VirtualPath value, i.e. VirtualPage=”/Pages/*” to allow all pages to include code blocks. I wouldn’t suggest this though, add a <PageParserPath> section for each page you wish to add code blocks to.

There are three values for CompliationMode:

Always [default value] Always compile this page.
Auto SharePoint will not compile the page if possible.
Never This page should never be compiled.

AllowServerSideScript can be set to true or false. In this case it must be true.

For wildcard paths, you can add another tag, IncludeSubFolders=”true” to <PageParserPath>.

That’s it, save the web.config file and your page should now load.

3 thoughts on “SharePoint 2010: Allow Server Side Code – Inline Code Blocks

    1. Hi all,

      I whole heartily agree that include code is generally not a good idea in SharePoint, but people have asked for it before so I thought it would nice to show how to do it. A better option would be to use custom Web parts, or hopefully find a way of using OOTB web parts and possibly jQuery or SPServices to get what you are looking for. I have repeatedly found that every problem in SharePoint can be solved many different ways.

Leave a Reply

Your email address will not be published. Required fields are marked *

Human Check *

Filed Under


New year new you, amiright? Here’s to a promising 2020! What are your #SharePoint resolutions? We’ve got a few to help drive your communication and collaboration goals.

Happy New Year #PixelFriends! Looking for a few resolutions you can actually keep this year? We’ve got a plan for you! Check out latest blog to learn more about balancing your work life and how #O365 can help keep you accountable. Daily is out! Stories via @pixelmillteam @UnifySquare @stevegoodman #office365 #sharepoint

Did you catch our Day 12 of the #12DaysofMicrosoftTeams tip? This is the last tip we’re ever going to give (in this decade) so spend it wisely.

Welcome to Day 12 of the #12DaysofMicrosoftTeams! Thanks for spending your time with us this holiday season. If you like conspiracy theories and predictions, we hope you’re prepare to get your socks knocked off in 2020. Enjoy!