Allow Server Side Code Inline Code Blocks in SharePoint 2010

InfoPath alternatives for form designing SharePoint

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.

Read some SharePoint tutorials

SharePoint Server 2016 Installation FREE PDF guide

Export SharePoint designer List workflow from one site to another site

Update or Delete SharePoint 2010 List Item using Javascript Object Model

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=”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:
<PageParserPaths>

<PageParserPath VirtualPath=”/SitePage/*” CompilationMode=”Always” AllowServerSideScript=”true” IncludeSubFolders=”true”/>

Notes:
You can use wildcards for the VirtualPath value, i.e. 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] 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.

Example: Add item in SharePoint List using Server side script

Allow Server Side Code Inline Code Blocks in SharePoint 2010
Allow Server Side Code Inline Code Blocks in SharePoint 2010

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)
{
try
{
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();


item.Update();
mySite.AllowUnsafeUpdates = false;
Page.Response.Write(“Success”);
}

}
catch (Exception ex)
{
Page.Response.Write(“failed” + ex.Message.ToString());
}
}
</script>


You May Also like the Following SharePoint Online Tutorials:

About Sagar Pardeshi

I am Developer working on Microsoft Technologies for the past 6+years. I am very much passionate about programming and my core skills are SharePoint, ASP.NET & C#,Jquery,Javascript,REST. I am running this blog to share my experience & learning with the community I am an MCP, MCTS .NET & Sharepoint 2010, MCPD Sharepoint 2010, and MCSD HTML 5,Sharepoint 2013 Core Solutions. I am currently working on Sharepoint 2010, MOSS 2007, Sharepoint 2013,Sharepoint 2013 App Dev, C#, ASP.NET, and SQL Server 2008.

View all posts by Sagar Pardeshi →

Leave a Reply