Allow Server Side Code Inline Code Blocks (Sever Side Script)

Posted: July 13, 2014 in SharePoint2010

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, and so on, 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.

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:inetpub\wwwroot\wss\VirtualDirectories”the site directory”, and look for the following code block in the <Configuration> -> <SharePoint> section.

<SafeMode MaxControls=200 CallStack=false DirectFileDependencies=10TotalFileDependencies=
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=/SitePage/* CompilationMode=Always
AllowServerSideScript=true IncludeSubFolders=true/>

You can use wildcards for the VirtualPath value, in other words VirtualPage=”/ SitePage /*” 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]: to always compile this page.
  • Auto: SharePoint will not compile the page if possible.
  • Never: This page should never be compiled.
  • AllowServerSideScript: Is either 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.

Example: Add an item to a SharePoint List using a Server-side script.

Allow Server Side Code


<form id=”form1″ runat=”server”>
New Title Name:
<asp:TextBox ID=”TextBox2″ runat=”server”></asp:TextBox>
<asp:button id=”Button1″ runat=”server” onclick=”Create1_Click” text=”Add”/>

<%@ Import Namespace=”System” %>
<%@ Import Namespace=”System.Data” %>
<%@ Import Namespace=”System.Web.UI.WebControls” %>
<%@ Import Namespace=”System.Web.UI.WebControls.WebParts” %>
<%@ Import Namespace=”Microsoft.SharePoint” %>
<%@ Import Namespace=”Microsoft.SharePoint.WebControls” %>
<%@ Import Namespace=”Microsoft.SharePoint.WebPartPages” %>
<%@ Import Namespace=”System.Xml” %>
<%@ Import Namespace=”System.Web.UI.HtmlControls” %>
<%@ Import Namespace=”System.Collections.Generic” %>
<%@ Import Namespace=”System.Collections” %>
<%@ Import Namespace=”Microsoft.SharePoint.Utilities” %>
<script type=”text/c#” runat=”server”>

protected void Create1_Click(object sender, EventArgs e)
SPWeb mySite = SPContext.Current.Web;
SPListItemCollection listItems = mySite.Lists[“Test Custom List”].Items;
mySite.AllowUnsafeUpdates = true;
SPListItem item = listItems.Add();

item[“Title”] = TextBox2.Text.ToString();
mySite.AllowUnsafeUpdates = false;



catch (Exception ex)
Page.Response.Write(“failed” + ex.Message.ToString());



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s