Search This Blog

2009-05-13

Creating Custom Application Page

We can Create custom pages by asp.net and Incorporate it to the Sharepoint Site.Following is the example of how can I Integrate a custom pages of ASP.NET with Sharepoint Site.The custom application pages may or may not have the code behind file.I am creating a class library project in the following example.
Step 1.Create a Classlibrary project "MyApplicationPages".
Step 2.Add the reference of Microsoft.Sharepoint and System.Web
Step 3.Create the folder as follows and rename the Class1.cs to ApplicationPage1.cs.This folder structure is created as 12/Template hierarchy.Under layout folder we will keep our application pages.

Step4.Now I will first create application page without codebehind file.The server side code is basically inline code.

Step5.Add a new item under the Layout/MyApplicationPages folder.

Step 6.Choose a text file and name it as "Hello.aspx".


Step7.Write the following code in your Hello.aspx page
<%@ 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>
Step7.Put the file PithHelmet.gif in your Images/MRB Folder

Step8.Now Create another file "ApplicationPage1.aspx" under the same root folder of Hello.aspx.ApplicationPage1.aspx will contain the code behind file.

Step 9.Write the following code in ApplicationPage1.aspx

<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Assembly Name="MyApplicationPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=74895b8b8177abca" %>

<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master"
Inherits="MyApplicationPages.ApplicationPage1"
EnableViewState="false" EnableViewStateMac="false" %>
<%@ Register Tagprefix="SharePoint"
Namespace="Microsoft.SharePoint.WebControls"
Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<asp:Content ID="Main" contentplaceholderid="PlaceHolderMain" runat="server">
<table border="1" cellpadding="4" cellspacing="0" style="font-size:12">
<tr>
<td>Site Title1:</td>
<td><asp:Label ID="lblSiteTitle" runat="server" /></td>
</tr>
<tr>
<td>Site ID:</td>
<td><asp:Label ID="lblSiteID" runat="server" /></td>
</tr>
<tr>
<td colspan="2">
<SharePoint:SPGridView runat="server"
ID="grdPropertyValues"
AutoGenerateColumns="false"
RowStyle-BackColor="#DDDDDD"
AlternatingRowStyle-BackColor="#EEEEEE" />
</td>
</tr>

<tr>
<td>
Site URL:</td>
<td>
<asp:Label ID="lblSiteUrl" runat="server" /> </td>
</tr>
<tr>
<td>
Site Collection URL:</td>
<td>
<asp:Label ID="lblSiteCollectionUrl" runat="server" /> </td>
</tr>
<tr>
<td valign="top">
<table style="width: 100%">
<tr>
<td class="ms-ToolPaneTitle">
Lists</td>
</tr>
<tr>
<td valign="top">
<asp:ListBox ID="lstLists" runat="server" Width="100%" />
</td>
</tr>
</table>
</td>
<td style="width: 50%" valign="top">
<table style="width: 100%">
<tr>
<td class="ms-ToolPaneTitle">
Site Users</td>
</tr>
<tr>
<td valign="top">
<asp:ListBox ID="lstSiteUsers" runat="server" Width="100%" Rows="10" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">
<SharePoint:SPTreeView ID="treeSitesFiles" runat="server" EnableViewState="false" NodeStyle-HorizontalPadding="6" />
</td>

</tr>
</table>
</asp:Content>

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

<asp:Content ID="PageTitleInTitleArea" runat="server"
contentplaceholderid="PlaceHolderPageTitleInTitleArea" >
Application Page : with code behind
</asp:Content>
Step 10.Now modify the ApplicationPage1.cs page(the renamed file of Class1.cs) as follows
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;


namespace MyApplicationPages
{
public class ApplicationPage1:LayoutsPageBase
{
// add control fields to match controls tags on .aspx page
protected Label lblSiteTitle;
protected Label lblSiteID;

protected SPGridView grdPropertyValues;


protected Label lblSiteUrl;
protected Label lblSiteCollectionUrl;
protected ListBox lstLists;
protected ListBox lstSiteUsers;

// define fields with names to match controls on .ASPX page
protected SPTreeView treeSitesFiles;

const string SITE_IMG = @"\_layouts\images\FPWEB16.GIF";
const string FOLDER_IMG = @"\_layouts\images\FOLDER16.GIF";
const string GHOSTED_FILE_IMG = @"\_layouts\images\NEWDOC.GIF";
const string UNGHOSTED_FILE_IMG = @"\_layouts\images\RAT16.GIF";

protected override void OnLoad(EventArgs e)
{

// get current site and web
SPSite siteCollection = this.Site;
SPWeb site = this.Web;
populateHelloWorldContent(siteCollection, site);
populateGridView(siteCollection, site);
populateListViewForListAndUser(siteCollection, site);
populateTreeView();


}
protected void populateHelloWorldContent(SPSite siteCollection, SPWeb site)
{
// program against controls on .aspx page
lblSiteTitle.Text = site.Title;
lblSiteID.Text = site.ID.ToString().ToUpper();
lblSiteID.Text = lblSiteID.Text;
}
protected void populateGridView(SPSite siteCollection, SPWeb site)
{
// program against controls on .aspx page
PropertyCollectionBinder pcb = new PropertyCollectionBinder();
pcb.AddProperty("Site Title", site.Title);
pcb.AddProperty("Site ID", site.ID.ToString().ToUpper());
pcb.AddProperty("Site Master Page Url", site.MasterUrl);
pcb.AddProperty("Site URL", site.Url);
pcb.AddProperty("Site Collection URL", siteCollection.Url);
pcb.AddProperty("Site Collection ID", siteCollection.ID.ToString().ToUpper());
pcb.AddProperty("Current User Name", site.CurrentUser.Name);
pcb.AddProperty("Is User Site Collection Admin", site.UserIsSiteAdmin.ToString());
pcb.AddProperty("Is User Site Admin", site.UserIsWebAdmin.ToString());
pcb.AddProperty("Site User Count", site.SiteUsers.Count.ToString());
pcb.AddProperty("Host Name", siteCollection.HostName);
pcb.AddProperty("Zone", siteCollection.Zone.ToString());
pcb.AddProperty("Site Collection System Account", siteCollection.SystemAccount.Name);
pcb.BindGrid(grdPropertyValues);
}
protected void populateListViewForListAndUser(SPSite siteCollection, SPWeb site)
{
lblSiteTitle.Text = site.Title;
lblSiteUrl.Text = site.Url.ToLower();
lblSiteCollectionUrl.Text = siteCollection.Url.ToLower();

lstLists.Items.Clear();
foreach (SPList list in site.Lists)
{
lstLists.Items.Add(list.Title);
}
lstLists.Rows = lstLists.Items.Count;


lstSiteUsers.Items.Clear();
foreach (SPUser user in site.SiteUsers)
{
lstSiteUsers.Items.Add(user.Name);
}
lstSiteUsers.Rows = lstSiteUsers.Items.Count;
}
protected void populateTreeView()
{
SPWeb site = SPContext.Current.Web;
SPFolder rootFolder = site.RootFolder;
TreeNode rootNode = new TreeNode(site.Url, site.Url, SITE_IMG);
LoadFolderNodes(rootFolder, rootNode);
treeSitesFiles.Nodes.Add(rootNode);
treeSitesFiles.ExpandDepth = 1;
}
protected void LoadFolderNodes(SPFolder folder, TreeNode folderNode)
{

foreach (SPFolder childFolder in folder.SubFolders)
{
TreeNode childFolderNode = new TreeNode(childFolder.Name, childFolder.Name, FOLDER_IMG);
childFolderNode.NavigateUrl = Site.MakeFullUrl(childFolder.Url);
LoadFolderNodes(childFolder, childFolderNode);
folderNode.ChildNodes.Add(childFolderNode);
}

foreach (SPFile file in folder.Files)
{
TreeNode fileNode;
if (file.CustomizedPageStatus == SPCustomizedPageStatus.Uncustomized)
{
fileNode = new TreeNode(file.Name, file.Name, GHOSTED_FILE_IMG);
}
else
{
fileNode = new TreeNode(file.Name, file.Name, UNGHOSTED_FILE_IMG);
}
fileNode.NavigateUrl = Site.MakeFullUrl(file.Url);
folderNode.ChildNodes.Add(fileNode);
}


}
}
}

Step 11. Now create a new class file PropertyCollectionBinder.cs in the root directory
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace MyApplicationPages
{
public class PropertyCollectionBinder {

protected DataTable PropertyCollection = new DataTable();

public PropertyCollectionBinder() {
PropertyCollection.Columns.Add("PropertyName", typeof(string));
PropertyCollection.Columns.Add("PropertyValue", typeof(string));
}

public void AddProperty(string PropertyName, string PropertyValue) {
DataRow newRow = PropertyCollection.Rows.Add();
newRow["PropertyName"] = PropertyName;
newRow["PropertyValue"] = PropertyValue;
}

public void BindGrid(SPGridView grid) {

SPBoundField fldPropertyName = new SPBoundField();
fldPropertyName.HeaderText = "Property Name";
fldPropertyName.DataField = "PropertyName";
grid.Columns.Add(fldPropertyName);

SPBoundField fldPropertyValue = new SPBoundField();
fldPropertyValue.HeaderText = "Value";
fldPropertyValue.DataField = "PropertyValue";
grid.Columns.Add(fldPropertyValue);

grid.Width = new Unit(400);

grid.AutoGenerateColumns = false;
grid.DataSource = PropertyCollection.DefaultView;
grid.DataBind();


}
}
}


Step 12.Now create feature.xml and elements.xml under TEMPLATE\FEATURES\MyApplicationPages
For feature.xml write the following code.

<?xml version="1.0" encoding="utf-8" ?>
<Feature Id="BCB88335-B064-4bda-8FB8-BCD40E69CFD2"
Title="My Custom Application Pages"
Description="This demoware was created for Inside Windows SharePoint Services"
Version="1.0.0.0"
Scope="Web"
Hidden="FALSE"
ImageUrl="TPG\PithHelmet.gif"
xmlns="http://schemas.microsoft.com/sharepoint/">

<ElementManifests>
<ElementManifest Location="elements.xml" />
</ElementManifests>

</Feature>

Step 13. For element.xml write the following code
<?xml version="1.0" encoding="utf-8" ?>

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<!-- Add Command to Site Actions Dropdown -->
<CustomAction Id="HelloApplicationPage"
GroupId="SiteActions"
Location="Microsoft.SharePoint.StandardMenu"
Sequence="2000"
Title="Application Page Without Codebehind"
Description="Getting up and going with inline code">
<UrlAction Url="~site/_layouts/MyApplicationPages/Hello.aspx"/>
</CustomAction>
<!-- Add Command to Site Actions Dropdown -->
<CustomAction Id="CustomApplicationPage"
GroupId="SiteActions"
Location="Microsoft.SharePoint.StandardMenu"
Sequence="2001"
Title="Application Page With Codebehind"
Description="Using the SPGridView control">
<UrlAction Url="~site/_layouts/MyApplicationPages/ApplicationPage1.aspx"/>
</CustomAction>
</Elements>

Step 14.Craete a install.bat which will Install the aspx pages to the Sharepoint Site

@SET TEMPLATEDIR="C:\program files\common files\microsoft shared\web server extensions\12\Template"
@SET STSADM="C:\program files\common files\microsoft shared\web server extensions\12\bin\stsadm"
@SET GACUTIL="C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe"
Echo Installing MyApplicationPages.dll in GAC
%GACUTIL% -if bin\debug\MyApplicationPages.dll
Echo Copying files
xcopy /e /y TEMPLATE\* %TEMPLATEDIR%
Echo Installing feature
%STSADM% -o installfeature -filename MyApplicationPages\feature.xml -force
IISRESET
REM cscript c:\windows\system32\iisapp.vbs /a "SharePointDefaultAppPool" /r

Step 15. Add a strong name ManabSnk.snk in your project

Step 16.At the end the Folder structure will be as follows

Step 17.Now Build your application and run the Install.bat file.
Step 18.Now activate your feature "My Custom Application Pages" under Site Feature of your sharepoint site.After activation ,you can see two menu item(Application Page Without Code Behind,Application Page With Code Behind) under the SiteAction Menu.
Step 19.Now I am clicking the option Application Page With Code Behind,Floowing is the output of this page








No comments: