SharePoint and ASP.NET Code Behind Pages
In a follow on to his earlier post regarding SharePoint development, Andrew Connell has posted detailed instructions on how to create, configure and deploy ASPX pages using code-behind files in SharePoint 2007. What I found most interesting in this article are the detailed solution package instructions and XML samples, both of which are sadly lacking in the SDK. I should point out that you can also deploy code-behind pages very easily to the /_layouts/ directory by simply copying the .aspx and .cs files to \12\TEMPLATE\LAYOUTS, where ASP.NET will do the runtime compilation for you. I like Andrew’s example better though because it includes a complete run-through on packaging and deployment. Way to go AC!
UPDATE: AC added a comment that made me realize that I really should expand upon what I said – I was pointing out alternative scenarios and should have thought it through a bit more before hitting the ‘Publish’ button.
The method that Andrew describes is not only the correct one, it is also the preferred one – leveraging the feature framework to deploy the pages is bang on and he is also correct that compiled objects are much better. There are situations where you need to deploy to the /_layouts/ directory (mostly when the page must be shared by multiple web apps) but, when you do, it is preferable to use a compiled assembly rather than JIT code-behind (for security, if nothing else – I hate having all that raw code on the server for anyone with permissions to look at and change).
I haven’t yet found a way to deploy pages to /_layouts/ using a Feature, but then I haven’t really tried yet as it’s not something we do very often; if there is a way, then that is how it should be done, including the solution packaging to get the proper safe control entries, etc. If someone has code for doing this, then by all means post it here so we can share it with the world.