Logo Home   Downloads   Up to Bluedog Limited
V3 - How To Publish an ASP.NET Page with Code and Master Page to a Doc Library
Posted on 6/15/2006 8:38 PM by pBoy
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=12.0.0.0,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=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

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

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

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

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

<%@ Register Tagprefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=12.0.0.0, 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>
TD.ms-titleareaframe, .ms-pagetitleareaframe {
 height: 10px;
}
Div.ms-titleareaframe {
 height: 100%;
}
.ms-pagetitleareaframe table {
 background: none;
 height: 10px;
}
</style>
</asp:Content>
 
<!-- your inline script & event handlers can go here -->
 
<asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
 <!-- your controls and content goes here -->
</asp:Content>
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:
<PageParserPaths>
        <PageParserPath VirtualPath="/pages/*" CompilationMode="Always" AllowServerSideScript="true" IncludeSubFolders="true"/>
</PageParserPaths>
 
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.
8. Do an IISRESET
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.
 
pBoy
re: V3 - How To Publish an ASP.NET Page with Code and Master Page to a Doc Library
Hi,
 
thanx for the tips in your article.
 
i have looked at PageParserPaths for another reason: changing the VariationLabelMenu.ascx in WCM pages.
 
This MSDN article shows how to do that -there's an obvious error in the web.config code (no PageParserPaths start tag) but apart from that im following method #2
 
i copied the control to my _catalogs/masterpage dir but get the following error when i add the control to my master page:
 
"The user control 'VariationsLabelMenu.ascx' is not compiled, and can only be used dynamically. To force it to be compiled, set compilationMode="Always" in its @control directive."
 
This i take it is why i added the PageParserPath in the first place!
 
Any ideas?
 
thanx
AndersR
 
 
AndersR @ 8/14/2006 7:02 AM
re: V3 - How To Publish an ASP.NET Page with Code and Master Page to a Doc Library
AndersR @ 8/14/2006 7:04 AM
pare: V3 - How To Publish an ASP.NET Page with Code and Master Page to a Doc Library
Hi
I ve created my custom aspx page thru SP Designer and replaced the NewForm.aspx with my page for a list so that my custsom page appears whenever a  new list item is created. I tried to put inline code for my custom button in tht page, but the page is throwing an error " Code blocks are not allowed here"...:(
Is there any way I can achieve this? Plz help me out.
Thnx
Suja
suja @ 2/19/2007 2:59 AM
re: V3 - How To Publish an ASP.NET Page with Code and Master Page to a Doc Library
Suja,
 
This is basic functionality.  Once a page is unghosted, they are not allowed to run code.  For more information, I would recommend reading the following article...
 
 
The article here describes how to utilize the PageParserPaths feature to enable compilation.  However, use this option with EXTREME care.
 
-Maurice
Maurice Prather @ 2/20/2007 9:00 PM
re: V3 - How To Publish an ASP.NET Page with Code and Master Page to a Doc Library
Is there also a way to accomplish using the SharePoint site template for a custom page, but keeping the code behind instead of having to place the code inline?
Brinkie @ 2/27/2007 2:28 AM
re: V3 - How To Publish an ASP.NET Page with Code and Master Page to a Doc Library
hi
I am trying to create my own error page which needs to be displayed  when a certain condition is not met. This page must have the same look and feel as SharePoint . So i am trying to inherit the application.master page but when i am trying to do this it gives an error saying "file ~\_layouts\application.master.aspx not found"
 
am i missing something?
I would be glad if you could reply at the earliest
 
thank you
 
PS: can you please elaborate further on how to create a new page
 
Swati @ 3/2/2007 3:15 AM
re: V3 - How To Publish an ASP.NET Page with Code and Master Page to a Doc Library
I faced problem although I registered the sharepoint assembly references as you mentioned in the blog the error is "An error occurred during the compilation of the requested file, or one of its dependencies. The type or namespace name 'SPWeb' could not be found (are you missing a using directive or an assembly reference?)"
 
Can you please advise me how to handle it?
 
Regards
Tariq Mardawi 
Tariq Mardawi @ 10/22/2007 11:27 AM
Microsoft Certified Master
Are you looking for a team of SharePoint experts?
ShareSquared can help ... drop us a note.
 
 
ShareSquared, Inc.
MVP Logo
Keyword Search
 
View by category
 

Disclaimer:
The contents of this site represent thoughts and opinions of the authors , not those of anyone else - such as past, present and future employers.  This a forum of the exchange of ideas centered on SharePoint technologies.  It is not a support channel.  :)

Copyright © 2004-2009 BluedogLimited.com. All rights reserved.