Sunday, November 14, 2010

Writing 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>
        <PageParserPaths>
        <PageParserPath VirtualPath="/Custom Webpart Pages/*" CompilationMode="Always"   AllowServerSideScript="true" />
      </PageParserPaths>
</PageParserPaths>




2) Write server side code as


<script runat="server">

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

</script>

The above script
I placed  inside the PlaceHolderMain place holder.

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>


Merits & Demerits

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


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
-  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).

No comments:


A sales engineer is someone who promise you a bridge, even when there's no river.