You definitely won't be able to do code-behind in SharePoint. However, here is a workaround to get inline code to work:
- "An error occurred during the processing of xxx.aspx. Code blocks are not
allowed in this file."
- "This page has encountered a critical error.
contact your system administrator if this problem persist."
Example inline
code
==================================
Open up SharePoint Designer and
the masterpage you want to edit. Switch to the code view and anywhere from
the first <head> tag down you can add some inline c# code:
<script runat="server">
string
myVar = "hello world!";
void Page_Load(object sender, System.EventArgs
e)
{
Response.Write(myVar);
SPSite siteCollection = new
SPSite("http://[sharepoint]");
SPWeb site =
siteCollection.OpenWeb("/[subsite]/");
SPList list =
site.Lists["Announcements"];
SPListItemCollection items =
list.Items;
foreach(SPListItem item in
items)
{
Response.Write(item["Title"].ToString());
Response.Write("<br/>");
}
}
</script>
This
shows how you can use the SharePoint object model in your inline code. If you saved the master page
now and opened the site in your browser you would get an error about not
being able to run code blocks (listed above). There’s an extra line you need
to put into your web.config file first to allow this inline c# to execute.
Modify the web.config
file
==================================
1. Open the web.config file and
make the following change:
<PageParserPaths>
<PageParserPath
VirtualPath="/_catalogs/masterpage/*" CompilationMode="Always"
AllowServerSideScript="true" IncludeSubFolders="true"
/>
</PageParserPaths>
The PageParserPaths xml tags will be
there already you just need to add the line in between.
2. Save
web.config .
3. Start -> Run -> iisreset /noforce , click OK.
Note: I would like to point out that making this change is possibly not
good practice. The fact that CompilationMode being set to "Always" points
out that there may be a performance hit and caching won’t exactly work
well.
Sources
==================================
http://weblog.vb-tech.com/nick/archive/2006/08/03/1710.aspx
http://www.wssdemo.com/blog/Lists/Posts/Post.aspx?List=d5813c18%2D934f%2D4fd6%2D9068%2D5cdd59ce56ba&ID=232