SharePoint 2007: using ASP.NET server side code in your pages

Remember the problems you had in SharePoint 2003 pages because it was not possible to plug in a simple piece of server side script in your pages? That you always had to write custom controls to accomplish this? Those times could be over, as longs as you approach this with great care.

In the web.config file in the SharePoint virtual directory contains the following section:

  <SharePoint>
    <SafeMode MaxControls="200" CallStack="false" DirectFileDependencies="10" TotalFileDependencies="50" AllowPageLevelTrace="false">
      <PageParserPaths>
      </PageParserPaths>
    </SafeMode>
    :
  </SharePoint>

By default the node <PageParserPaths> is empty. You can add <PageParserPath> nodes to specify the virtual paths where you want to allow server side scripts:

<PageParserPaths>
        <PageParserPath VirtualPath="/pages/*" CompilationMode="Always" AllowServerSideScript="true" IncludeSubFolders="true"/>
</PageParserPaths>

Where CompilationMode is one of the following values:

Always The page should always be compiled (default value)
Auto ASP.NET will not compile the page, if possible. 
Never The page or control should never be dynamically compiled.

I assume that the AllowServerSideScript and IncludeSubFolders flags speak for themselves.

Be careful with the virtual paths you specify in your PageParserPaths. Anyone that can modify or add a page to the virtual path can insert code that will be executed server side with no restrictions.

A good location to specify as a PageParserPath is the location where you store your masterpages, for example /_catalogs/masterpage. You can now add server side script to your masterpages, which makes it available in all pages using this masterpage.

<PageParserPaths>
        <PageParserPath VirtualPath="/_layouts/masterpage/*" CompilationMode="Always" AllowServerSideScript="true" IncludeSubFolders="true"/>
</PageParserPaths>

There is no documentation available on this functionality. I found two references in the Microsoft SharePoint documentation that handled with variations: http://msdn2.microsoft.com/en-us/library/ms562040.aspx and http://msdn2.microsoft.com/en-us/library/ms551625.aspx.

Maurice Prather also describes the PageParserPath functionality in this blog post.

Thanks to Stramit for pointing me in the right direction in this blog post on SharePoint navigation.

Published Thursday, July 27, 2006 1:34 AM by svdoever
Filed under:

Comments

Thursday, July 27, 2006 6:08 AM by Renaud Comte

# re: SharePoint 2007: using ASP.NET server side code in your pages

My pleasure  !!!

Thanks for this cool analysis of the PageParserPaths

Sunday, July 30, 2006 11:08 AM by Farvashan

# re: SharePoint 2007: using ASP.NET server side code in your pages

Dear Oever

I read almost all of your blog, good articles, I also very interested about Macaw skinner tool, are you have any plan for continuing it for WSS 3?

Sunday, July 30, 2006 2:57 PM by svdoever

# re: SharePoint 2007: using ASP.NET server side code in your pages

Hi Farvashan, I have a new version ready that runs on .Net 2.0, should work for WSS3. Waiting for codeplex project to release it.

Monday, July 31, 2006 3:40 PM by Farvashan

# re: SharePoint 2007: using ASP.NET server side code in your pages

ok, I'm waiting for your release, also it's nice your picture on the home page ;) can you tell me how can I do same( I use community server 2 on sharepointblogs ), also I linked you, be happy you and your son.

Monday, August 14, 2006 9:55 AM by AndersR

# re: SharePoint 2007: using ASP.NET server side code in your pages

Hi,

thanx for the tips in your article.

i have looked at PageParserPaths for another reason: changing the VariationLabelMenu.ascx in WCM pages.

The MSDN article you mention above 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

http://msdn2.microsoft.com/en-us/library/ms551625.aspx

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

Saturday, January 27, 2007 11:37 AM by tuan anh

# re: SharePoint 2007: using ASP.NET server side code in your pages

Hi,

i want to target content, i want to Add Simple Rules to an Audience,

i have read the introduction in: http://msdn2.microsoft.com/en-us/library/ms582294.aspx

but i don't know, what to do to add that code to my site

can you help me?

thanks for help

Monday, January 29, 2007 10:24 AM by svdoever

# re: SharePoint 2007: using ASP.NET server side code in your pages

@tuan anh: I'm afraid you have to do some studying yourself;-) Pick up a few books on programming, install Visual Studio 2005 and start rolling...

If you have very specific needs and are willing to pay for the hours needed for implementation, yuo can always let me know;-)

Friday, February 02, 2007 4:22 AM by Andy

# re: SharePoint 2007: using ASP.NET server side code in your pages

Hello,

had this problem too - code block isnt allowed in my in sharepoint sites masterpage. Solved it with the  <PageParserPaths> - setting in web.config. (great)

Have another probelm aswell. Have assigned my topnavigation sharepoint:aspmenu control to a xmldatasource - works great - but when I try to hook an MenuItemDataBound event to that control I get a message saying thats not allowed in this page. Is there a simliar way to specify this in the web.comfig (as with code block problem) or do I have to look for another solution ?

Appreciate all the help I can get / Andy

Wednesday, May 16, 2007 2:15 PM by Jason Wang's SharePoint Blog

# Tricks for debugging Web Parts and Web pages in SharePoint

I decide to make this post as an ongoing one to record the tricks I come up with for debugging Web Parts

Monday, September 10, 2007 2:31 AM by btimur

# re: SharePoint 2007: using ASP.NET server side code in your pages

Hello. I added my server-side code to the custom Page. It's work. But How i can debug this code from VisualStudio? In which dll this code included? Also how i can work in Visaul Studio? develop my Custom page and then put it in SharePoint ?

# Adding Server Side code to the Publishing Page Layouts - PageParserPaths &laquo; SharePoint - The MOSS

Pingback from  Adding Server Side code to the Publishing Page Layouts - PageParserPaths &laquo; SharePoint - The MOSS

Leave a Comment

(required) 
(required) 
(optional)
(required)