How to Execute Inline Code in SharePoint 2007

At times, you may want to run inline server-side code (e.g. C#) directly inside your SharePoint 2007 pages. However, if you try it, you'll find that SharePoint prohibits this behavior by default. To give your SharePoint installation the ability to run inline code, you need to tweak the contents of the web.config file.

Author's Note: Before you continue, note that enabling inline server side code directly in your pages has security ramifications and is not recommended—you should not do so on a regular basis. Instead, consider it exclusively as a technique that can save you some time in challenging situations.

  1. Before making any changes, backup your current web.config to some safe location; you might want to revert back to it.
  2. Open web.config and search for the <PageParserPaths> element. This is the parent element of the content you're going to add. By default, it's empty (it has no content or child nodes).
  3. Under the <PageParserPaths> element, add a new <PageParserPath> child node with the following attributes.
<PageParserPaths>
  <PageParserPath 
    VirtualPath="/pages/*"
    CompilationMode="Always"
    AllowServerSideScript="true"
    IncludeSubFolders="true" />
</PageParserPaths>

Here's an explanation of each attribute for the <PageParserPath> element:

  • VirtualPath enables all the pages in the specified virtual path to run inline code. You can use wildcards (as shown above), or specify a particular page (.aspx or .master).
  • CompilationMode can take three values. The Always value shown above means that the inline server side code will always be compiled—this is a default value, and you should not change it unless you have a valid reason. You can also set it to Auto to prevent the ASP.NET framework from compiling the page. Lastly, you can use Never, meaning the control or page should never be dynamically compiled.
  • AllowServerSideScript enables or disables server-side scripting. The example above sets it to true, because you want to run the inline server-side code. You can set it to false to stop running inline code. This convenient when you're testing code and then later want to remove it from the page altogether.
  • IncludeSubFolders enables server-side code execution not only for the pages within the virtual path provided, but also any pages within subfolders of that path. Usually, you'd set it to true as in the example. Setting it to false enables server-side code execution only for the path specified in the VirtualPath attribute, and not for any subfolders.

Adding this capability to your toolbox, can sometimes help you discover the source of problems in running sites. But because it can be a security hazard, use it carefully.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Networking Solutions

Partners

More Networking