Introduction
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.
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:50" AllowPageLevelTrace="false">
<PageParserPaths>
</PageParserPaths> </SafeMode>
<PageParserPaths>
<PageParserPath VirtualPath="/SitePage/*" CompilationMode="Always"AllowServerSideScript="true" IncludeSubFolders="true"/>
Notes
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.
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"/>
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">
<%@ 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>
No comments:
Post a Comment