How to Add ASP.NET Server-Side Code in SharePoint 2007 Pages


Many people have asked me how we can add ASP.Net server-side code in SharePoint pages created using the Designer; the following are the simple steps to do that.

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> 

 By default the node <PageParserPaths> is empty. We 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>

We can set any of the following values for Compilation Mode:

  • 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.

We should be very cautious with the virtual paths specified in our PageParserPaths since 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 better location is to use PageParserPath to specify the location where our master pages reside, for example /_catalogs/master page. You can then add server-side scripts to master pages, which makes it available in all pages using this master page.

<PageParserPaths>
 

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

  </PageParserPaths>

Up Next
    Ebook Download
    View all
    Learn
    View all