Wednesday, June 10, 2009

How to write Server-side ( C# ) code in SharePoint custom pages

There are basically two approaches to write server side code:
A) Inline coding ( C# and HTML code in same ASPX page)
B) Code-Behind (C# and HTML code in seperate files)

A) Inline coding

1) In this approach, first you have to modify application's web.cofig file.
Locate PageParserPaths node in config file and then change it as per your requirement.

If you doesn't know page(s) name which is(are) using server side code, then write /* in VirtualParth attribute,
else write page name as /pagename.aspx in VirtualParth attribute.
[For each page add a seperate PageParserPath node,
for example you want to allow server side code in 5 pages then you have to add PageParserPath nodes.]



<PageParserPaths>
<PageParserPath VirtualPath="/*" CompilationMode="Always"
AllowServerSideScript="true" />
</PageParserPaths>




2) Write server side code as


<script runat="server">

protected void Page_Load(Object sender, EventArgs e)
{
Response.Write("Server code");
}

</script>



B) Code-Behind approach

1) Create a C# file (or VB file) for your server code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyNameSPace
{
public class MyClass : Microsoft.SharePoint.WebControls.LayoutsPageBase
{
//Server control declarations
protected Label lblMessage;

protected void Page_Load(Object sender, EventArgs e)
{
lblMessage.Text = "Server Code";
}
}
}



Points to remember:
- Inherits the class with LayoutsPageBase (there are other options also available for choosing base class)
- Declare ASP.Net server control with protected modifier.



2) Register the DLL (that contains above class) in GAC.

3) safecontrol entry in application's web.config file.

4) Create ASPX file for HTML code


<%@ Assembly Name="MyNameSPace, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=bf118dafb4e11f4b" %>
<%@ Page Language="C#" MasterPageFile="~masterurl/default.master"
Inherits="MyNameSPace.MyClass" %>

<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<asp:Label runat="server" ID="lblMessage" ForeColor="Red"
Visible="false"></asp:Label>
</asp:Content>



Points to remember:
- Specify assembly reference of class created in step 1 (with proper Name, Version, Culture and PublicKeyToken)
- In case of Master-Content page scenario specify MasterPageFile attribute in Page directive
- Specify Inherits attribute in Page directive with proper NameSpace and Class name (e.g. Inherits="MyNameSPace.MyClass" )
- Write ASP.Net server control(s) that we have declared in Step 1 (with protected modifier)


Pros n Cons

Inline
- Easy to deploy, no need to do DLL registration in GAC, IISRESET and safecontrol entry in web.config
- It is unsafe, to allow server side code by modifying PageParserPaths node in web.cofig
- Can't debug the page [Major disadvantage while development].
- In case of any change(s) in code (HTML + C#), replace the existing ASPX file with modified one in SharePoint Designer.


Code-Behind
- Can debug server side code ( Debug -> Attach to Process -> w3wp.exe process )
- Need to do DLL registration in GAC, IISRESET and safecontrol entry in web.config
- It's safer to use compare to inline code
- In case of any change(s) in HTML code, replace the existing ASPX file with modified one in SharePoint Designer.
- In case of any change(s) in server-side ( C# ) code, re-register the DLL in GAC (drag-n-drop dll in assembly folder) and do IISRESET [ Must ] to see the change(s).

2 comments:

jagadish said...

can you please make a demo video and show the example

Dave Hunter said...

Not a recommended approach, could open site to code attacks as it bypasses the safe code processing.

Google