SharePoint Application Page with Custom Controls
By Alon Havivi
In this article, I will describe the use of Application Pages (_Layouts) and will show you, step by step, how to build your custom Application Page with your own Web Controls
SharePoint Application Page with Custom Controls
In this article, I will describe the use of Application Pages (_Layouts) and will
show you, step by step, how to build your custom Application Page with your own
Web Controls.
Usually, as a SharePoint developer, when we need to add specific business logic with
user interface to SharePoint application, we will create a Web Part. In many
cases web parts are great and also the best solution, but not always. It is important
to understand your business requirements and to find the best way to implement
it in SharePoint application.
Pages Types in SharePoint
First, let’s understand the main different between Application Pages and Content
Pages:
- Content Pages – Contains content controls that can be modified by end users.
ONLY Content Pages can contain Web Part Zones, therefore can contain Web Parts.
- Application Pages – Are regular ASPX pages that exposed in every site collection or sub site under
the _layouts folder.
When we understand these main differences it is easy to know when to use each page,
For example:
Scenario A - Content Pages
You need to create an RSS to show relevant content to users in a certain group and
you need to support customization by sites administrators and personalization
for end users.
Scenario B - Application Pages
Your company wants to provide a central page that displays the latest articles from
Eggheadcafe.com. This page will be available for company employees and could
not be configured by end users or sites administrators.
Walkthrough: Create Custom Application Page with Custom Controls
Applies to: Microsoft Office SharePoint Server 2007, Visual Studio 2008 with WSPBuilder Extensions
This programming task describes how to create one Application Page that contains
two custom Web Controls: a control that display a clickable image which leads
to a Eggheadcafe.com website and another control that display the latest articles
from Eggheadcafe.com.
Step 1 – Create your solution
- Start Visual Studio.
- On the File menu, click New, and then click Project. The New project dialog box appears.
- Select WSPBuilder and then WSPBuilder Project.
- Give your solution a name and click OK.
Step 2 – Create the WSPBuilder deployment project
- Add a folder under the 12 folder called “TEMPLATE”.
- Add a folder under the TEMPLATE folder called “Images”.
- Add a folder under the Images folder called “eggheadcafe”.
- Add this image to your Images folder.
- Add a folder under the TEMPLATE folder called “LAYOUTS”.
- Add a folder under the LAYOUTS folder called “ASPX”.
- Right click on the ASPX folder, Add new item, Select Text File.
- Rename the TextFile1.txt to “CustomPage.aspx”
Step 3 – Create the code behind folder and files
- Add a folder called “Assembly” under the project menu.
- Add a folder under the Assembly folder called “WebControls”.
- Right click on the WebControls folder, Add new item, Select Class.
- Rename Class1.cs to Control1.cs
- Right click on the WebControls folder, Add new item, Select Class.
- Rename Class1.cs to Control2.cs
Step 4: Add a Reference to System.Web.dll
- On the Project menu, click Add Reference.
- On the .NET tab, click System.Web.
- Click OK.
Step 5: Create The Apllication Page
- Open the CustomPage.aspx file.
- Link the custom application page to application.master
<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master"
%>
- Associates aliases with namespaces and class names.
<%@ Register TagPrefix="CustomControls" Namespace="Your Namespace
Name" Assembly="Your Assembly Name, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=You Public Key Token" %>
- Add the Control Place Holder.
- Add the Custom Controls to the Control Place Holder.
<asp:Content ID="Main" runat="server" ContentPlaceHolderID="PlaceHolderMain">
<table>
<tr>
<td>
<CustomControls:Control1 ID="control1" runat="server" />
</td>
</tr>
<tr>
<td>
<CustomControls:Control2 ID="control2" runat="server" />
</td>
</tr>
</table>
</asp:Content>
- Add site title to the Browser.
<asp:Content ID="PageTitle" runat="server" ContentPlaceHolderID="PlaceHolderPageTitle">
Application Page With Custom Controls
</asp:Content>
- Add site title to the Page.
<asp:Content ID="PageTitleInTitleArea" runat="server" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea">
Application Page With Custom Controls
</asp:Content>
Step 6: Create The Image Control
- Open the Control1.cs file.
- Add the following using statements:
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
3. Override the CreateChildControls method with this code:
protected override void CreateChildControls()
{
base.CreateChildControls();
Image imgButton = new Image();
imgButton.ImageUrl = "/_layouts/images/eggheadcafe/eggheadcafe.jpg";
imgButton.ToolTip = "eggheadcafe";
imgButton.Attributes.Add("OnClick", "location.href('http://www.eggheadcafe.com')");
imgButton.Attributes.Add("style", "cursor: hand;");
this.Controls.Add(imgButton);
}
Step 7: Create the RSS Reader Control
1. Open the Control2.cs file.
2. Add the following using statements.
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
3. Override the CreateChildControls method with this code:
protected override void CreateChildControls()
{
System.Net.WebRequest myRequest = System.Net.WebRequest.Create ("http://www.eggheadcafe.com/rss_pheedo.xml");
System.Net.WebResponse myResponse = myRequest.GetResponse();
System.IO.Stream rssStream = myResponse.GetResponseStream();
System.Xml.XmlDocument rssDoc = new System.Xml.XmlDocument();
rssDoc.Load(rssStream);
System.Xml.XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");
tring title = "";
string link = "";
string description = "";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < rssItems.Count; i++)
{
System.Xml.XmlNode rssDetail;
rssDetail = rssItems.Item(i).SelectSingleNode("title");
if (rssDetail != null)
{
title = rssDetail.InnerText;
}
else
{
title = "";
}
rssDetail = rssItems.Item(i).SelectSingleNode("link");
if (rssDetail != null)
{
link = rssDetail.InnerText;
}
else
{
link = "";
}
rssDetail = rssItems.Item(i).SelectSingleNode("description");
if (rssDetail != null)
{
description = rssDetail.InnerText;
}
else
{
description = "";
}
sb.Append("<p style='font-family:ariel;font-size 8pt'><b><a href='" + link + "' target='new'>" + title + "</a></b><br/>");
sb.Append(description + "</p>");
if (i == 9)
{
break;
}
}
Label rss = new Label();
rss.Text = sb.ToString();
this.Controls.Add(rss);
}
Step 8: Deploy the WSP
- Build Your solution.
- Right-click on the project.
- Click WSPBuilder Build WSP.
- Right-click on the project.
- Click WSPBuilder – Deploy WSP.
- Get the assembly information from the GAC at C:\Windows\assembly
- Right-click on the DemoProjectUI assembly and click properties.
- Add the assembly information to the Application Page.
- Open CustomPage.aspx file.
- Copy the namespace name, assembly name and the public key token to the application
page:
<%@ Register TagPrefix="CustomControls" Namespace="Your Namespace
Name"
Assembly="Your Assembly Name, Version=1.0.0.0, Culture=neutral, PublicKeyToken=You
Public Key Token" %>
Step 9: See it in action
1. Navigate to http://YOURSERVER/_layouts/aspx/CustomPage.aspx
Summary
The article shows another way to add custom functionality with user interface to
SharePoint pages.
Download the Solution.
Share this article
Popularity (18931 Views)
Article Discussion: SharePoint Application Page with Custom Controls
sharepoint application page
daryl uberall replied
to Alon Havivi at Saturday, October 16, 2010 2:18 AM
downloaded your solution. it was hardly at all like your step by step, written-out instructions.
for the life of me, I cannot find application.master, and there is no mention of _layouts. it is a key file and nothing works w/o it. am using VS2008.
do you ever try downloading your own solutions to see if they even setup and work?
Edward replied
to Alon Havivi at Saturday, October 16, 2010 2:18 AM
Read your instructions. Created project with my own name. Followed instructions step by step. Ran build. Went to assembly folder to get the public key token. Then ran build and wsp build and deploy. Error read:
"The solution file located at "MyNewName.wsp" does not appear to be a valid upgrade for solution "MyNewName.wsp".The two solutions must have the same resource types (global or Web-application scoped)."
when I checked the Assembly folder, the assembly was gone. It makes no sense.
Naresh V replied
to Edward at Saturday, October 16, 2010 2:18 AM
Thanks for the steps.. i run into some issues while follwoing the steps but have figured out to make fixes.
1) in the code the class name should be decleared as public other wise the page will give an eror.
2) after generating the wsp for the first time we ned to update the assembly infomation in aspx page and the application tab of the project properties. then we have to redeploy the solution to take those changes.
and here is question for folkes,. how can target the specific web application to add the wsp instead of ading it globally?
Thanks,
Naresh
Naresh V replied
to Edward at Saturday, October 16, 2010 2:18 AM
Hi,
if you have class name variations or any changes made to the code which are major like name spaces etc... then you might recieve this error.. the best thing is to go to the central administration - solution management - retract the solution - remove the solution. then doa fresh deploy of the solution woould be apossible overcome to this problem.
Thanks,
-Naresh
Edward replied
to Naresh V at Saturday, October 16, 2010 2:18 AM
Always create classes as public so that was not the issue. It has been so long since I had this problem that I don't know what ended up fixing it. Have subsequently created several custom applications in layouts folder and have never had this issue.
Edward replied
to Edward at Saturday, October 16, 2010 2:18 AM
Interestingly enough one application always was able to run the WSPBuilder but always reported seeing two xxx.dll's one under the bin/debug folder and one under the obj folder. The next application never reported this, despite having the same structure. Each was able to be built and successfully run.