May
14
There are many ways to run custom code in SharePoint 2007. Arguably one of the simplest is to create a Custom Application Page, sticking all the source code into a script tag at the top of the page. You don’t have to muck around with compiling dlls or modifying the web.config file.
- Create a new aspx file. Put this at the top:
<%@ 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" %>
Watch out! SharePoint Designer likes to remove the ~/ from your MasterPageFile declaration. I have no idea why it does this. I sat there entering it, hitting save, and watching it disappear like 4 times before I opened up Notepad and put it in. Weird.
- Add any namespaces needed.
<%@ Import Namespace="System" %> <%@ Import Namespace="System.Web" %> <%@ Import Namespace="System.Web.UI" %>
- Add a script tag (don’t forget to runat server!).
<script runat="server"></script>
- Copy the code from your code-behind and stick it into the shiny new script tag you just created. Don’t include those pesky namespace or class declarations, just the functions you want.
- Application Master has some ContentPlaceHolders; we care about three of them.
- PlaceHolderMain will hold your tag soup from the ASPX file.
- PlaceHolderPageTitle will hold the title that shows in the title bar of the browser.
- PlaceHolderPageTitleInTitleArea holds the page title that goes in the SharePoint chrome (the yellow bar of a typical application page).
<asp:Content ID="Main" contentplaceholderid="PlaceHolderMain" runat="server"> <!-- all your aspx content goes here --> </asp:Content> <asp:Content ID="PageTitle" runat="server" contentplaceholderid="PlaceHolderPageTitle"> Title in browser title area </asp:Content> <asp:Content ID="PageTitleInTitleArea" runat="server" contentplaceholderid="PlaceHolderPageTitleInTitleArea"> Title in SharePoint chrome </asp:Content>
- Create a folder in the LAYOUTS virtual directory. Should be in the C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\LAYOUTS\ directory. Don’t ever skip this step! Always separate your grubby code from the pristine beauty of the default code with folders!
- Copy your newly created .ASPX into the folder created in step 6.
- Now you can get to your snazzy new Custom Application page in any site by adding /_layouts/PageName.aspx to whatever site you want.
- There is no step nine.
Have fun!
