Musings on code and stuff RSS Feed


Adding Code-Behind to Custom Pages in SharePoint 2007
In my last post, I described how to add code blocks to your custom SharePoint 2007 pages. In this post, I will describe a similar technique that allows you to use the ASP.NET 2.0 code-behind model for your pages. Apparently, this was easier to achieve in SharePoint 2003, and I have seen quite a few posts out there from frustrated people who haven't been able to get this working in SharePoint 2007. First, I created a minimal test page in Visual Studio 2005. Here's the contents of Test.aspx: <%@ Page Language="C#" CodeFile="Test.aspx.cs" Inherits="SPCodeBehind.Test" %>                     SharePoint 2007 Code-Behind Test Page                                                             And here's the contents of Test.aspx.cs: using System; using System.Web.UI;   namespace SPCodeBehind {       public partial class Test : Page       {             protected void Button1_Click(object sender, EventArgs e)             {                   Button1.Text = DateTime.Now.ToString();             }       } }   It's just a simple page with a button that changes its label when clicked. It looks like this:   Next, I used the Publish Web Site feature in Visual Studio 2005 to prepare my code for deployment to my SharePoint site. First, I generated a strong-name key pair file: C:\Program Files\Microsoft Visual Studio 8\VC>sn -k "C:\Users\WillA\Desktop\SPCodeBehind.snk" Then, I published the code to a folder on my local machine (using my key file): Next, I created a new test.aspx page in my SharePoint site using SharePoint Designer, and then pasted the contents of my published page into the new page: Next, I ran the page in my SharePoint site. I knew it would fail since I hadn't told SharePoint where my code-behind DLL was yet. Here's the error message: An error occurred during the processing of /Test.aspx. Could not load the assembly 'App_Web_7kov8md2'. Make sure that it is compiled before accessing the page. Next, I copied the DLL into the bin folder of my SharePoint site and retried the page. Again, an error! The base type 'SPCodeBehind.Test' is not allowed for this page. The type is not registered as safe. Next, I modified the web.config file of my SharePoint site to include the following line: Another error, but this one looks familiar – it's the same error I got when I tried to add a code block to my page in my last blog post! An error occurred during the processing of /Test.aspx. The event handler 'OnClick' is not allowed in this page. Finally, I modified the web.config file one last time: Success! J Please note that I used namespaces, strong-named assemblies, and checked the AllowPartiallyTrustedCallerAttribute option when publishing my initial code. I'm not 100% positive yet if all of these are required to get this working, but it's what I ended up with after much trial and error. Please let me know if you have additional information, or find a better way to do any of this. Good luck!
 
Posted by Will Allan | 11 Comments | Trackback Url | Bookmark with:        
Tags: SharePoint 2007

Links to this Post

Comments

Tuesday, 13 Mar 2007 05:44 by That's insane
I can't believe how difficult they made developing with standard asp.net code in SharePoint.  Such a pain to have to build, publish, modify, etc...  Back in the day things were simple.  Are you still using this ,eh, workflow now? Thanks for the great post!

Tuesday, 13 Mar 2007 09:04 by The codefile attribute on the page directive is not allowed in this page.
I'm getting the following error message when I try to load the page. The codefile attribute on the page directive is not allowed in this page. Here is my page directive: <%@ Page language="C#" CodeFile="Reg.aspx.cs" MasterPageFile="~masterurl/custom.master" Inherits="SPCodeBehind.Reg, Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> ------ Here's the codebehind source: ------ namespace SPCodeBehind {     public partial class Reg : Page     {         public void CreateUserWizard1_CreatedUser(object sender, EventArgs e)         {             string[] users = new string[] { ((CreateUserWizard)sender).UserName };             try             {                 Roles.AddUsersToRole(users, "Members");             }             catch (Exception ex)             {                 Response.Write("Unable to add user to role: " + ex.Message);             }         }     } } ---- I added all the stuff to the web.config.  Any ideas?

Thursday, 15 Mar 2007 12:39 by RE: The codefile attribute on the page directive is not allowed in this page.
I believe that if you use the Publish Web Site feature of VS 2005, and copy the contents of that file (rather than the contents of the original .aspx file that you created), then it should work. I think.

Thursday, 15 Mar 2007 12:15 by Codefile attribute issue
Hey, You might be right.  I didn't even use a publishing site to create the aspx page or the DLL.  I tried to use your process with a library project and the original ASPX file created by SharePoint designer. I guess I'll give your method a shot.  Is there any other way to access the SPGroup object in a script page like I am able to do in a codebehind file?  If not, I'll go forward with your suggestion. Thanks, Thomas Goddard

Tuesday, 27 Mar 2007 11:37 by Follow Up
I got it to at least load the page by using the publish and taking the exact contents as you suggested.  The problem is now, there's no way to specify a custom master page without it borking:   Parser Error Message: The referenced file '/~masterurl/custom.master' is not allowed on this page. Line 1: <%@ Page Language="C#" MasterPageFile="~masterurl/custom.master" AutoEventWireup="true" CodeBehind="Register.aspx.cs" Inherits="GHECWebControls.Register" %> Line 2: <%@ Register TagPrefix="SharePointWebControls" Namespace="Microsoft.SharePoint.WebControls" Line 3: Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>  

Tuesday, 10 Apr 2007 03:02 by Custom User Controls
I'm trying to run some custom web application projects using this same design.  I'm stuck with trying to get my user controls which are inside my web app project to be registered as safe controls.  They are in the web app dll so they have been signed and deployed to the GAC.  SharePoint doesn't like you to drag the ascx pages into designer and use them in your aspx page, and specifying SafeControl for the MyWebApp with TypeName=* doesn't include the user control evidently.   Example Situation MyWebApp.dll has TestPage.aspx/.cs and TestControl.ascx/.csDrag TestPage.aspx and TestControl.ascx into the site using DesignerTestPage.aspx includes an instance of TestControlRun TestPage.aspxI get an error "An error occurred during the processing of /TestPage.aspx. The referenced file '/MyControl.ascx' is not allowed on this page."   Any suggestions?

Thursday, 3 May 2007 10:55 by The codefile attribute on the page directive is not allowed in this page.
Friend, I modificated the file web.config and not functionality, you could help me, you me could send me your web.config, for look. Me email is jonathan_moscosos@hotmail.com

Friday, 18 May 2007 01:22 by GAC Deployment
First of all thanks for the post... I have this working when copy the assembly to the sharepoint /bin folder. For some reason it won't work when I deploy the assembly to the GAC. Any ideas what could be going wrong here?

Thursday, 8 Nov 2007 12:58 by It dose not work eith me
I have this problem:   An error occurred during the processing of /sites/sorbonne/Pages/test1.aspx. Value cannot be null. Parameter name: key

Monday, 7 Jan 2008 01:17 by Value cannot be null, parameter name:key
Hi I'm getting the following error   Value cannot be null, parameter name:key   What cvan be the reason for this, please help

Name:
URL:
Email:
Comments:

CAPTCHA Image Validation