Publish Your .ASPX Page With Code Behind To SharePoint 2007

It was a headache for me to create a HelloWorld.ASPX page, that contains only one button that changing its text to “Hello World !” when clicked (Imagine!!), and deploy it in my SharePoint 2007 Site, so I’ve start surfing the internet looking for a help on this issue. And after testing many ways, here is my 100% working Step-by-Step example on how to write a simple Hello World ASPX Page with Code Behind File example inside SharePoint 2007.

(I’ll be using Visual Basic .Net language to accomplish this Example)

1. Create new web site: Open Micorosft Visual Studio 2005, and start a new WebSite & Name it any thing and save it any where you want.

By default a Default.aspx page will be created with a CodeFile named Default.aspx.vb. Add a Button from the toolbox to this Default.aspx (in the design mode) , i assume that you (as a .Net developer) know that when you’r using a CodeFile, an Attribute will be added in the page directive of the Default.aspx page, named CodeFile, as follows

<%@ Page Language=”VB” AutoEventWireup=”false” CodeFile=”Default.aspx.vb” Inherits=”_Default” %>

2. Writing Code: Double click on the Button1 control and add the following line of code 

                     Button1.Text = “Hello World”

So the CodeFile will be looking as follows :

Partial Class _Default

Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles  Button1.Click

Button1.Text = “Hello World”

End Sub

End Class

3. Modify the code file class & add namespace: Now we want to make some modification to this file, first add a Namespace and name it HelloWorld and change the name of the class (which is _Default) to be Test and make the class Partial Public Class, after these modifications, your code behind file should look like the following:

Namespace HelloWorld
    Partial Public Class Test
        Inherits System.Web.UI.Page
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles  Button1.Click
Button1.Text = Date.Now
        End Sub
    End Class
End Namespace

4.Modify the Default.aspx “inherites” attribute: Now beacause of these modifications, we must now open the Default.aspx page in code view, and modify the Inherits=”_Default” in the page directive (the first line of the page) to inherit from this new named class, so make it Inherits=”HelloWorld.Test”. now save your work and run the application, just to be sure that every thing is going well. click the button. the Button text shall state “Hello World”



5.Creating Strong Name Key : Now we must prepare a “Strong Name Key” for our application, this can be done using a tool named “SN.exe”, you could find it in the following path:

“C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\sn.exe”

or you can run it from Start >> Programs >> Microsoft Visual Studio 2005 >> Visual Studio Tools >> Visual Studio 2005 Command Prompt

write the following command line : SN -k c:\MyKey.snk .

after this command runs it will create a MyKey.snk file in the C:\ root directory (as we write in the comman line).and amessage will appear stating “key pair written to c:\mykey.snk”.

6.Publishing the web site: Now you must publish our site to any folder in our machine, as follows:

a.    From solution explorer, point to your project, right click.

b.    Select Publish Web Site (as shown below)

c.    Now brows to the folder you wish to publish your site to. In my example I’ll name it MySitePublished to the C:\ root, so its path will be C:\MySitePublished.

d.    Enable “use a key file generated with the strong name tool” option.

e.    Check the “Mark assemblies with Allow Partially Trusted Caller Attribute APTCA”, (as shown below)

f.     In the “key file location:” box, browse to the file “c:\MyKey.snk” (that you’ve generated in step “5”) & click open.

g.    Finally click ok. Now the publish operation will start, and will take only a few seconds to complete.


Please note that after publishing your web site, a “.DLL” file is generated, holding a name starts wirh “App_Web_” followed by random generated letters, like “App_Web_fyrnh9gi.dll”, and stored in the bin folder of your publishing folder (in our case its C:\MySitePublished\bin).

7. Preparing Your SharePoint Site for the new comer: Open the SharePoint Designer, we will do the following major steps:

1-    Creating a new .ASPX page inside a new folder called “Applications”.

2-    Copying our generated .DLL file to the bin folder of the SharePoint site.

3-    Modify the Web.Config file of the SharePoint site.


So Let’s We Start

A.    In your SharePoint Designer open your web site, Create a folder in the root of your SharePoint site and name it for example “Applications”.

B.     Add a new .ASPX page into it naming it Hellowrold.aspx, go to the code view, and delete all code inside.

C.    Open a new instance of your Visual Studio 2005, and open the published web site (which is C:\MySitePublished), ignore the warning message by pressing “Yes”, and open the Default.aspx page in the “Source View” and copy all the code.

D.   Back to the SharePoint Designer paste the source code inside the Helloworld.aspx page, notice in the page directive, the inherits attribute is changed to  inherits=”HelloWorld.Test,” followed by the name of the .DLL file generated, in my case App_Web_cotcnlm3, so the full page directive is stating <%@ page language=”VB” autoeventwireup=”false” inherits=”HelloWorld.Test, App_Web_cotcnlm3″ %>.

E.    Go to the bin folder of the published website (which is C:\MySitePublished), and copy the .DLL file (in my case its App_Web_cotcnlm3.dll), and paste it into the bin folder of your SharePoint site, which can found in the following path:

“C:\ Inetpub\Wwwroot\wss\VirtualDirectories\port number\bin”

The port number is the port of the SharePoint Server Web Application

F.    Now open the “Web.Config” file of your SharePoint Web Application, which can found in the following path:

“C:\ Inetpub\Wwwroot\wss\VirtualDirectories\port number

The port number is the port of the SharePoint Server Web Application

G.   Navigate to the <SafeControls> directive and add the following line:

<SafeControl Assembly=App_Web_cotcnlm3 Namespace=HelloWorld TypeName=* Safe=True />

H.    Navigate to the <PageParserPaths> directive and add the following line:

<PageParserPath VirtualPath=/Applications/* CompilationMode=Always AllowServerSideScript=true IncludeSubFolders=true/>

I.     I think that you need to make iisreset, so go to start menu > run, and write the following command : IISRESET.

J.     Preview your HelloWorld.aspx page and have fun.



12 Responses to Publish Your .ASPX Page With Code Behind To SharePoint 2007

  1. Hello there. Thanks for outstanding step-by-step. I have no idea how you figured all this out. I’d been looking for days. Anyway, my question is, how do you take this further and reference the microsoft.sharepoint.dll and code against the Sharepoint object model? I need to list all the sites a user currently has rights to, for starters.

  2. SAM says:

    Big problem occured after creating SN -k c:\MyKey.snk—-
    Instead of the c drive path , i had a folder with my name which contained hell lot of data may be 4 GB data.

    I typed in the command prompt SN -k c:\SAM\MyKey.snk….SAM contained my data….
    After i pressed entered the keey was created.After this when i published, a window popped up saying the data u will be deleted something like yo want to continue….I said continue..and all the data from my SAM folder got deleted….

    Did you also got the same message after clicking on publish…

  3. Rizwan says:

    I was searching for many days for writing code behind file for my sharepoint site, and found much blogs and sites for that but didn’t get succeessfull but after reading this blog and the way the author described each step I got all the steps and hope that This will work now.. I again wanna say Great blog for writing code behind for Sharepoint Site.

  4. Dam says:

    Hi, thanks a lot for this great tutorial. Like Rizman, i’ve been looking for this for so many time. Good job

  5. Pirsey says:

    If you ever want to read a reader’s feedback 🙂 , I rate this article for 4/5. Detailed info, but I just have to go to that damn msn to find the missed pieces. Thanks, anyway!

  6. Mohit says:

    After doing all above step i m getting following error can you please reply me i m stuck with this problem

    An error occurred during the processing of . Could not load the assembly ‘App_Web_34pej8xs’. Make sure that it is compiled before accessing the page.

    • alkalony says:

      Hi Mohit;
      I think that you must check the following:
      1- sign your assembly with a Strong Name Key pair (SNK).
      2- build it, to generate the DLL file.
      3- make sure that you’ve copied the DLL file into the SP bin directory.
      4- Add the safe control entry in the web.config file of the SP web application.
      5- make sure that the name of the DLL file is written correctly in both, the ASPX page header section, and in the safe control entry in the web.config file.


  7. dmitry says:

    thank you very much for this article. It is very useful for me… but anyone knows, how can i attach masterpage(of SharePoint site) to my custom .ASPX page?

  8. […] my post ”Publish Your .ASPX Page With Code Behind To SharePoint 2007“, here is a simpler way of creating no code behind aspx page in […]

  9. Vaishnavi Desai says:


    Excellent post!:)
    Actually I cannot find SP bin directory to copy the DLL file.
    Any suggestions for that?
    Please help!


    • alkalony says:

      Hi Vaishnavi Desai ;

      Will, ok i think that SP was wrong to be written like this, so what i ment is the Bin directory of your Web application.

      The default place to find the bin directory for a SharePoint Web Application is:
      C:\inetpub\wwwroot\wss\VirtualDirectories\ [port number] \bin
      [port number] : is the port number for your web application. the default is 80.

      hope this helps you.

  10. Vaishnavi Desai says:


    I am not able to find the location for the bin directory for the sharepoint application.
    Could you please explain how should i go about copying the DLL file?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: