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