Search This Blog

2011-02-08

Create a custom application page in sharepoint 2007

Step 1-Create a Asp.NET Web Application project "CustAppPage" and a class library project "CustAppPageClassLib" and make the folder structure as follows



Step 2-Feature.xml will be as follows-
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
Id="{E0A4DFA4-717D-44b0-8292-B46593E64B80}"
Title="My Feature-Cust App Page"
Description="Shows a navigation link in under site action menu and ECB menu"
Hidden="false"
Scope="Web"
ImageUrl="menuprofile.gif"
>
<ElementManifests >
<ElementManifest Location="Elements.xml"/>
</ElementManifests>

</Feature>

Step 3-Elements.xml will be as follows-
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<!-- Add Command to Site Actions Dropdown -->
<CustomAction Id="CustomApplicationPage_StandardMenu"
GroupId="SiteActions"
ImageUrl="~/_layouts/images/menuprofile.gif"
Location="Microsoft.SharePoint.StandardMenu"
Sequence="2001"
Title="Cust Application Page 1"
Description="CustomApplicationPage under StandardMenu">
<UrlAction Url="~site/_layouts/MyCustAppPages/ApplicationPage1.aspx"/>
</CustomAction>

<!-- Per Item Dropdown (ECB) Link -->
<CustomAction Id="CustomApplicationPage_ECB"
RegistrationType="List"
RegistrationId="101"
ImageUrl="~/_layouts/images/menuprofile.gif"
Location="EditControlBlock"
Sequence="240"
Title="CustomApplicationPage under ECB" >
<UrlAction Url="~site/_layouts/MyCustAppPages/ApplicationPage1.aspx?ItemId={ItemId}&ListId={ListId}"/>
</CustomAction>

</Elements>

Step 4-"ApplicationPage1.aspx"(Code behind approach) will be as follows-
<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Assembly Name="CustAppPageClassLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=30534ee939de1c1d" %>

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

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

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

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

Step 5-"HelloWorld.aspx"(Inline c# code) will be as follows-
<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%>
<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" Inherits="Microsoft.SharePoint.WebControls.LayoutsPageBase" %>

<%@ Import Namespace="Microsoft.SharePoint" %>

<script runat="server">
protected override void OnLoad(EventArgs e) {
//SPWeb site = SPContext.Current.Web;
SPWeb site = this.Web;
lblSiteTitle.Text = site.Title;
lblSiteID.Text = site.ID.ToString().ToUpper();
}
</script>

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

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

<asp:Content ID="PageTitleInTitleArea" runat="server"
contentplaceholderid="PlaceHolderPageTitleInTitleArea" >
The Quintessential 'Hello World' of Application Page
</asp:Content>

Step 6-"ApplicationPage1.cs" under "CustAppPageClassLib" will be as follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace CustAppPageClassLib
{
public class ApplicationPage1 : 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();

if (Request.QueryString["ListId"] != null)
{
lblSiteID.Text = lblSiteID.Text + " ,List ID :" + Request.QueryString["ListId"].ToString();
}
if (Request.QueryString["ItemId"] != null)
{
lblSiteID.Text = lblSiteID.Text + " ,Item ID :" + Request.QueryString["ItemId"].ToString();
}

}
}
}

Step 7-Add the strong name to your project "CustAppPageClassLib" and install it in the GAC.

Step 8-Copy your aspx pages in the following folder
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\MyCustAppPages

and copy the others pages as the folder structure in your project

Step 9-Install the feature and activate it.

Step 10-click the "Cust Application Page 1" menu as follows and you can see the output as follows-


Step 11- Or click on the ECB menu on any list item


You can see the output as follows-


Step 12-You can type the URL and see your Helloworld.aspx page

No comments: