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. Madhur February 16, 2012

    Nice insightful article. This is not a good option though.

    1. Garreth van Leeve November 16, 2012

      Hi
      What is the best option then?
      Regards
      Garreth

    2. Eric Overfield December 13, 2012

      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 Comment

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

Enter Code *

Filed Under

Related

Are you preparing to launch a new #SharePoint intranet?

Don’t spring it on employees out of the blue. First, talk to as many of them as you can.

Planning is just phase one. For all of our tips, watch our recent webinar!

http://ow.ly/dsQM50GqPI5

It's that time again! Join #Microsoft MVP & Regional Director, @EricOverfield on Thursday, October 28th, at 11:00 AM PST as he walks through #MicrosoftViva’s current state & offerings and what they mean to your #digitalworkspace right now.

http://ow.ly/3QFK50Gnx9f

It's the final countdown! Join @EricOverfield at 11am PST to learn about common pitfalls and paths to success when moving to #SharePointOnline. You'll walk away with practical tips to use in your organization today!

http://ow.ly/fdf150GjyJT

In today's episode of the #PixelMillWebinars, you'll learn from the experience of a global organization’s path to digital teamwork victory and get to see first-hand how to successfully plan a smooth migration for your team.

Join us at 11am PST!

https://pixelmill.com/webinar-9-takeaways-from-a-global-organizations-migration-to-sharepoint-online/

It's the day of the show y'all! Join #Microsoft RD +MVP @EricOverfield at 11am PST as we walk you through a multi-national enterprise software company’s migration journey to #SharePointOnline. Learn about common pitfalls and paths to success.

http://ow.ly/cN9250GjtGk

Subscribe to PixelMill's
E-news

* indicates required

Let's Talk Digital
Workspaces Today

Get In Touch