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

SharePoint-Branding-Code-Blocks-Error

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">
   <pageParserPaths>
   </pageParserPaths>
</safeMode>

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

i.e.


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

Notes:

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

Always Focus on Productivity https://t.co/qTc7NoPaa6 #CollabTalk #SharePoint #Office365 #Collaboration #Productivity

One week away! Join us next week for 'Uncovering the Latest in #SharePoint #Development' w/ @EricOverfield… https://t.co/Wo6WVEOg3n

Don't forget! Join the @ebspug team tonight at 6pm in Downtown Oakland for a look at Power BI with Roy HIggs… https://t.co/mAbbtoS2MX

Our June webinar is coming! Join @EricOverfield on 6/29 for 'Uncovering the Latest in #SharePoint Development'… https://t.co/UpvkvEHhKu