This post is migrated from previous hosting provider. There are still some issues with old posts. Please make a comment on this post with any issues.

The simplest form of SharePoint application, part 2

Tags: SharePoint

Yesterday I did a short post on how to make a really simple SharePoint application with a .aspx page inside a document library and some coding with SharePoint Designer. I ended the post with giving a hint that you can also place the code in an assembly. To aid you in your self studies, here comes the solution…

Make an assembly

Class LibraryFirst of all we need to make an assembly to host our code behind. Start Visual Studio and create a Class Library project. Add a reference to Microsoft.SharePoint.dll and System.Web. The Microsoft.SharePoint.dll can be found under c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI (who came up with the idea to place this stuff here by default?).

Create a class called HelloWorldPage and make it inherit from Microsoft.SharePoint.WebPartPages.WebPartPage. Now add an asp:Label control to your class and then write the code for the Page_Load, just as in the previous post.

Your code should look like this:

namespace HelloWorld2 {
    public class HelloWorld2Page : Microsoft.SharePoint.WebPartPages.WebPartPage {
        protected Label label1;
        void Page_Load(object sender, EventArgs e) {
            label1.Text = "Hello world";
        }
    }
}

Before finalizing this assembly we need to sign it – SharePoint does not really like unsigned code, and it also allows you to install it into the GAC.

Signing

Create the application page

Just as in the previous sample we need to create a .aspx page in the document library. Create a new Basic Page in your library and call it HelloWorld2.aspx. Then open it up in SharePoint Designer and remove the WebPartZone, just as we previously did. Then add the asp:Label control like this:

Code...

Make magic happen

Now the fun begins. Change the @Page directive attribute Inherits to your own class and assembly.

Inherits="HelloWorld2.HelloWorld2Page,HelloWorld2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a425d507c93d43c0" 

It should look something like above. First you have the full name to your class, then you have the assembly strong name. You can find the strong name by looking at your DLL using Red Gate’s Reflector.

Reflector

Save your page.

Copy your DLL to the SharePoint site bin directory and load your application page in the browser. You can also register your assembly in the GAC, but using the bin directory method is easier when developing.

SafeControl!

You will most probably see an error message like this:

Parser Error

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. Parser Error Message: The base type 'HelloWorld2.HelloWorld2Page' is not allowed for this page. The type is not registered as safe.

To fix this you have to mark your DLL as Safe in the web.config, like this under the SafeControls element:

SafeControl

Reload again. Tada…

Hello World

From here I hope you can continue on your own and make some really neat stuff with SharePoint.

No Comments

  • Randy Williams said

    Well Written Wictor. Of course we work with SafeControls with web parts, and I didn't consider that you could do the same with a content page and an inherits attribute using SPD. It's a clever and practical solution. Much better than having to change the web.config to enable managed code from the page.

  • Trackback said

    John Anderson is new to both Bamboo Solutions and to SharePoint, but he isn\'t new to online community. Having recently departed AOL, where he was a Programming Manager for that company\'s social media team, John is thrilled to have joined the Bamboo family as Manager of Content & Syndication. As a member of the Online Operations team, John takes great pride in helping shape the creation and direction of Bamboo Nation, our nascent SharePoint community. Within Bamboo Nation, John writes the blog SharePoint...

  • Wictor said

    My bet is that this will not change. I don't Microsoft will make a significant change for us developers in this aspect a third time (remember 2001 to 2003 and 2003 to 2007).

  • JC said

    Simple walkthroughs like these are absolutely the best when you are just starting out.. Thank you so much for this. That said, Once my assemby and page start getting much more complex, How can I troubleshoot the code from inside VS 2005 on my client that is NOT running MOSS 2007. Also, what's the best deployment strategy? Will I need to keep updating my web.config file? Please email me at (remove extra @ signs) sales@@@@@cyberpine.com

  • Wictor said

    Thanks, first of all; to be able to debug your code you have to have Visual Studio installed on your SharePoint server (or use Bamboos WSS on Vista solution or use remote debugging). Deployment should in "real" scenarios always be done using WSP packages, these deployment packages takes care of most the web.config fiddling if you are creating the WSP's using WSS extensions for Visual Studio or other tools such as WSPBuilder (which is my preferred tool)

  • Avadhoot said

    Thanks for your post. It really helped me to start coding with VS 2010.

    My question : I have added asp.net LinkButton control using SPD2010. Now I want to handle events for the control. I am getting the error message 'An unexpected error has occurred' when I click on the button.

    Can you please guide me how can I debug the code or am I missing anything here ?!

    Thanks in advance for your help
    - Avadhoot

Comments have been disabled for this content.

About Wictor...

Wictor Wilén is the Nordic Digital Workplace Lead working at Avanade. Wictor has achieved the Microsoft Certified Architect (MCA) - SharePoint 2010, Microsoft Certified Solutions Master (MCSM) - SharePoint  and Microsoft Certified Master (MCM) - SharePoint 2010 certifications. He has also been awarded Microsoft Most Valuable Professional (MVP) for seven consecutive years.

And a word from our sponsors...