Code Behind in MOSS This is great to be able to use ASP.Net code behind model for coding in MOSS. This adds great flexibility in writing customized code for MOSS. Let’s start doing this.
The first step is to create a Web site project in Visual Studio. The details of the files are as under: Hello.aspx
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Hello.aspx.cs” Inherits=”HelloNameSpace.HelloClass” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”>
<html xmlns=”” >
<head runat=”server”>
<title>Code Behind Example for MOSS</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:Button ID=”Button1″ runat=”server” Text=”Click Me” OnClick=”Button1_Click” />
</div>
</form>
</body>
</html>
Hello.aspx.cs
namespace HelloNameSpace{
public partial class HelloClass : System.Web.UI.Page {
protected void Button1_Click(object sender, EventArgs e) {
Response.Write(“HELLO WORLD.”);
}
}
}
The next step is to generate a strong name key. We can use the tool “Sn.exe”. The path, where it can be found, is “C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin”. Use the following command to generate the key.
sn –k HelloNameSpace.snk
The next step is to publish the site. For this, right click on the project and select “Publish Web Site”. This will generate a new window as shown in the figure below:
Check the option “Enable strong naming on precompiled assemblies” and gave the path of the file “HelloNameSpace.snk” that you created just before.
Click “OK” and publishing will be succeeded.
Go to the bin directory of the Visual Studio solution and copy the DLL file from the bin directory of the solution to the bin directory of the SharePoint site. The path of the bin directory of SharePoint can be “C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin”. Also, copy the name of the DLL file. In our case it is “App_Web_js_i-de4.dll”.
Now, its time to add the code in the MOSS site. Open the site in the SharePoint Designer and add a aspx page in the site.
Copy and past the HTML from the file “Hello.aspx” into the aspx page added in the SharePoint Designer. Don’t forget to change the first line of the code as under:
<%@ Page Language=”C#” AutoEventWireup=”true” Inherits=”HelloNameSpace.HelloClass, App_Web_js_i-de4″ %>
Next step is to add two entries in the web.config file accordingly
<SafeControl Assembly=“App_Web_js_i-de4“ Namespace=“SPCodeBehind“ TypeName=“*“ Safe=“True“ />
And
<PageParserPaths> <PageParserPath VirtualPath=“/*“ CompilationMode=“Always“ AllowServerSideScript=“true“ IncludeSubFolders=“true“ /> </PageParserPaths>
It is strongly recommended that you should save a copy of web.config file before making changes. The file web.config can be found at “C:\Inetpub\wwwroot\wss\VirtualDirectories\80”.
Now access the page and hopefully it will work fine.
The problem with this approach is that you can’t make changes in case you want to inherit master page, it will generate exception.
For more help, follow the link below