Add to My Yahoo! | Google Reader or Homepage | Add to Windows Live | Add to Windows Live Alerts

Wictor Wilén

SharePoint Server MVP / Author / MCT / MCTS / MCP / MSc writing about SharePoint and other interesting Microsoft technologies

The simplest form of SharePoint application, part 2

Posted at 2008-10-21 01:15 by Wictor Wilén in SharePoint with 11 comments.

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.

Comments and trackbacks

#  Clever by Randy Williams
Screenshot from websnpr 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.
#  Re: Clever by Wictor
Screenshot from websnpr Nice, I think this a good way to get started with SharePoint programmin
#  Learn Sharepoint by Trackback
Screenshot from websnpr 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...
#  Upgrades by Ramesh
Screenshot from websnpr Thank you for the post. But, what will happen, if Microsoft makes an updgrade to MOSS 2010 or WSS 4.0? Thanks
#  Upgrades by Ramesh
Screenshot from websnpr Thank you for the post. But, what will happen, if Microsoft makes an updgrade to MOSS 2010 or WSS 4.0? Thanks
#  Re: Upgrades by Wictor
Screenshot from websnpr 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).
#  Still not Safe by Mark
Screenshot from websnpr I've followed this procedure but I'm still getting "The type is not registered as safe.". Any ideas on what else I can do?
#  Still not Safe by Mark
Screenshot from websnpr I've followed this procedure but I'm still getting "The type is not registered as safe.". Any ideas on what else I can do?
#  Re: still not safe by Wictor
Screenshot from websnpr Can you post the Parser Error Message as well as your SafeControl entry
#  This is great.. BUT.. by JC
Screenshot from websnpr 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
#  @JC by Wictor
Screenshot from websnpr 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)
Make a comment on this post:
Subject:  

Your name:  
Your Url:  
Note: submissions may have to be approved before being visible, so don't submit your comment multiple times.
Real Time Web Analytics