Bluedog Limited > SharePoint Thoughts > Posts > V3 - How To Publish an ASP.NET Page with Code and Master Page to a Doc Library
June 15
V3 - How To Publish an ASP.NET Page with Code and Master Page to a Doc Library
This post was migrated from an older system. Some links may point to content that no longer exists. Comments were not migrated.
Now that V3 (or MOSS or Office12 or whatever you want to call it) is now out in the open, I thought I'd start occassionally sharing some hard earned information about it.  I've been periodically doing some How To pieces internally for folks and it seems like sharing is a good idea.  So, with that in mind, here's the first piece in what I hope will be multiple installments over time.
This particular topic came about as the result of a couple persistent questions I was seeing - a) how can I write a custom aspx page and have it run from a doc library and b) how can I get my custom pages to take on the same look and feel of the site in which it is hosted.  This How To answers both of those questions.
  1. Create your page and code in a separate directory within the portal. 
  2. Get your code working
  3. Create a page without the code behind and add your code and event handlers inline to the new page.
  4. Modify the page directive to: 
    1. Use the master page for the site
    2. Inherit from webpartpage.  Your page declaration should look something like this:

<%@ Page Language="C#" MasterPageFile="~masterurl/custom.master" Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>

5.  Add the other register directives and <asp:Content> controls.  You can use existing pages that SharePoint provides out of the box as reference.  As an example, here are the directives and controls added from the default.aspx page in the SPS site template:

<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register Tagprefix="OSRVWC" Namespace="Microsoft.Office.Server.WebControls" Assembly="Microsoft.Office.Server, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register Tagprefix="SPSWC" Namespace="Microsoft.SharePoint.Portal.WebControls" Assembly="Microsoft.SharePoint.Portal, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register Tagprefix="SEARCHWC" Namespace="Microsoft.Office.Server.Search.WebControls" Assembly="Microsoft.Office.Server.Search, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register Tagprefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
NOTE:  I'm including several Content controls below; the controls you need will vary depending upon your Master Page.
<asp:Content ContentPlaceHolderID="PlaceHolderPageTitle" runat="server"></asp:Content>
<asp:Content ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server"></asp:Content>

<asp:Content ContentPlaceHolderId="PlaceHolderPageImage" runat="server"><IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt=""></asp:Content>

<asp:Content ContentPlaceHolderId="PlaceHolderTitleBreadcrumb" runat="server"/>

<asp:Content ContentPlaceHolderId="PlaceHolderTitleAreaClass" runat="server">
<style>, .ms-pagetitleareaframe {
 height: 10px;
} {
 height: 100%;
.ms-pagetitleareaframe table {
 background: none;
 height: 10px;
<!-- your inline script & event handlers can go here -->
<asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
 <!-- your controls and content goes here -->
6. Upload the page to the Pages library (or any doc library for that matter)
7. Add the path to the library where you uploaded the item into the PageParserPaths section of the web.config for your SharePoint web application.  For example, to enable inline code in the Pages library you would add a line like this:
        <PageParserPath VirtualPath="/pages/*" CompilationMode="Always" AllowServerSideScript="true" IncludeSubFolders="true"/>
NOTE:  You want to be careful about where you add a PageParserPath VirtualPath.  ANYONE that can upload a page can upload something that will execute code at that point.
9. Click on your page; it should execute now and appear using the same master page and UI as the rest of the site.
That's it!  This is something that I think will come in handy, and hopefully these instructions will get you there faster.


There are no comments for this post.

© 2004-2013 Okean Solutions
Aptillon, Inc.
Microsoft Certified Master - SharePoint 2010 & 2007
Sign In