Monday, September 8, 2008

Creating Custom Application Page ( Code Behind )

You want to write a Custom Application Page for some additional configurations for all of your sites.


<%@ Assembly Name="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Assembly Name="CustomApplicationPages, Version=, Culture=neutral, PublicKeyToken=d4e5777b16a5749f" %>

<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master"
EnableViewState="false" EnableViewStateMac="false" %>

<asp:Content ID="Main" contentplaceholderid="PlaceHolderMain" runat="server">
<table border="1" cellpadding="1" cellspacing="0" style="font-size:12">
<td>Site Title:</td>
<td><asp:Label ID="lblSiteTitle" runat="server" /></td>
<td>Site ID:</td>
<td><asp:Label ID="lblSiteID" runat="server" /></td>

<asp:Content ID="PageTitle" runat="server"
contentplaceholderid="PlaceHolderPageTitle" >
Hello World

<asp:Content ID="PageTitleInTitleArea" runat="server"
contentplaceholderid="PlaceHolderPageTitleInTitleArea" >
Custom Application Page : 'Hello World' with code behind

MyConfig.cs(GAC ,CustomApplicationPages.dll)
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace CustomApplicationPages {

public class MyConfig : Microsoft.SharePoint.WebControls.LayoutsPageBase {

// add control fields to match controls tags on .aspx page
protected Label lblSiteTitle;
protected Label lblSiteID;

protected override void OnLoad(EventArgs e) {

// get current site and web
SPSite siteCollection = this.Site;
SPWeb site = this.Web;

// program against controls on .aspx page
lblSiteTitle.Text = site.Title;
lblSiteID.Text = site.ID.ToString().ToUpper();
You can access to the page from any web i.e.
or you can also write a CustomAction to access the URL