SharePoint Application and Site Pages - Part 2 of 2
page 2 of 5
by Steven Barden
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 28369/ 29

Writing the code

·         Open Visual Studio 2005 and begin a class project. It may seem odd that you are beginning with a class project, rather than an ASPX project, but more will soon be revealed.

·         Right mouse on references and select Add. You have likely noticed this before, but don't be surprised or concerned when it takes time for the desired window to pop up in place.

·         Select the "browse" tab and enter “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\Microsoft.SharePoint.dll” if you're using a standard install.

·         Do the same for "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Web.dll"

NOTE: It should make sense that you would add a reference to the SharePoint assembly but the manual reference to the assembly "System.Web.dll", might be a bit of a surprise. This goes back to the information described above… you are starting with a windows assembly project, but you need to add the information required to manually build ASPX pages. You will see more information like this as move farther along.

·         Rename the namespace, class, and file as desired.

·         Compile to ensure all is in order (<Control> + <Shift>+<B>)

·         In the root of the project add a folder named TEMPLATE.

·         Inside that, add directories named FEATURES, IMAGES and LAYOUTS.

·         Note that this example will not use UserControls on the page(s), but if you were to do so you would add a CONTROLTEMPLATES directory as well, which would hold the user controls. Recall the intent (as described in the previous articles, Developing and Deploying a SharePoint Feature - Part 1 and Developing and Deploying a SharePoint Feature - Part 2, found by way of the link, http://aspalliance.com/author.aspx?uId=45726) is to duplicate the directory structure found at "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE". In description of the directories we have created, the FEATURES directory is where we will turn on and off this functionality. The IMAGES directory is pretty straight forward, and while the LAYOUTS directory may not be as obvious by the name. This is where your ASPX pages will be placed.

·         At this point it is it is important to acknowledge the intended name of the project because various sub-directories use this name. We will use DevAppPages, and as such each of the directories used will get a sub-directory named such.

·         Next we will create an ASPX page inside the LAYOUTS\ DevAppPages directory. Since we did not start this application as a web application we will not have the selection of a web page to add to our project, necessitating that we do this from scratch. Luckily there is not very much that needs to be added to a text file to make it an ASPX page.

·         Start by naming it what you want with an .ASPX extension.

·         Add an ASPX type reference to the four part version of the SharePoint DLL. In this case it is "<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%>"

·         Add the four part name of the assembly for this project.

·         Using the project properties, assign a public key.

·         Open Reflector, open the compiled assembly in Reflector and copy everything from Name to the end of the public key token, being sure to change various characters as needed, such as the ":" after the name for "=" and adding double quotes where needed.

·         The end result should be "<%@ Assembly Name="Pages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c475c2fd38120dac" %>", or your version of it.

·         Finally add the Page declaration and the appropriate entries, "<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" Inherits="Pages.AppPage1" EnableViewState="false" EnableViewStateMac="false" %>".

·         Notice the location of the MasterPageFile… clearly this is intended for when the page reaches its final location.

·         Before I continue, I want to give credit to Ameet Phadnis for publishing an excellent article at http://aspalliance.com/1376_Customizing_WSS_30_with_Master_Pages, which covers (as it says) Customizing WSS 3.0 pages. He also references the Microsoft page http://office.microsoft.com/en-us/sharepointdesigner/HA101651201033.aspx#1, which, among other points, has a list of Content Placeholder names and descriptions that you will find helpful.

·         Now drop to the bottom of the page and manually add a place holder, "<asp:Content ID="Main" contentplaceholderid="PlaceHolderMain" runat="server"></asp:Content>"

·         Do the same for "<asp:Content ID="PageTitle" runat="server" contentplaceholderid="PlaceHolderPageTitle" ></asp:Content>"

·         Save and switch to the Design view and make various adjustments as desired, including adding a label that will be filled out later with the site name.

·         Switch to the AppPage1.cs file and (if you have not already) add the using statements for a web page and of course a reference to the SharePoint assembly. Also add a reference to the SharePoint.WebControls assembly. As you are likely aware, SharePoint sits on ASP.NET and inherits much of its functionality from it. This is the way to reference SharePoint's inherited functionality for building web controls.

·         In the name of the class, add the inheritance to the LayoutsBasePage. While on this step, ensure that the class is set as public.

·         In order for this class and the ASPX page to work together properly, since they were not added at the same time you need to add the required control references on the page to the class where you need interactivity. As such, since you will not affect the functionality of the placeholders, you do not need to reference them, but since you want to use the label in the page from the class, you want to make a reference to a label. The .NET and ASP.NET runtime will make the leap to link the two so long as the names match up. If you think about it, this is what an automated ASP.NET project is doing via Visual Studio. You are simply doing it manually here.

·         Now make use of the page OnLoad event. You can place the next statement inside this block or outside.

·         Use the following statements on two different lines to "drill into" the site to get the name, then add it to the label… "SPSite siteCollection = this.Site;", "SPWeb site = this.Web;", "LabelSiteName.Text = site.Title;".

·         That is it… you are complete with this stage. At this point your page and its corresponding class / assembly are complete.


View Entire Article

User Comments

Title: Using an application page as the home page for a team sire   
Name: Shilpa
Date: 2010-04-13 6:05:40 AM
Comment:
Hi,

I am very new to Sharepoint programming.
I have a requirement where a SP list should be populated on loading the team site home page. I understand his is not possible with site pages. But can I create an application page that will populate the list on page load and make the application page the default page for the site?

Your response to this question will be much appreciated

Regards,
Shilpa
Title: VS.NET 2008   
Name: Jagan
Date: 2008-10-09 12:01:54 PM
Comment:
Does that work with VS.NET 2008 + MOSS 2007 ?
Title: Re. E.G.   
Name: T.Holm
Date: 2008-10-03 8:51:14 AM
Comment:
Remarks retracted, didn't see the downloads :)
Title: E.G.   
Name: T.Holm
Date: 2008-10-03 7:22:34 AM
Comment:
It would have been great, if you had made an example (screenshot or literal of the aspx page, including the reference to a code-behind file). Anyway it got me started (in the process of writing an application page to the central adm)
Title: Great   
Name: Abdulla Abdelhaq
Date: 2008-09-22 5:30:29 PM
Comment:
Thank you .. well done :)

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 11:08:41 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search