Tuesday, June 17, 2008

SharePoint MOSS 2007: Adding Custom Code - An Introduction

So, you've been developing code on some other web development platform like ColdFusion, JSP, PHP or even ASP.NET and suddenly you find yourself on a SharePoint project. Poking around, you suddenly realize "Dude...I cant edit the code!". Sure, they give you SharePoint designer to modify page templates, allowing you to drag and drop existing controls into your html layout. But how the heck do you add custom code to a page? Where are my code behind pages and handy-dandy User Controls?

Sadly, code behind pages are all blocked by default in SharePoint. You dont have direct access to any of the .CS code behind pages via SharePoint Designer and you have no way of building an .ASCX user control (that I know of anyway). So even though you can modify HTML and drop in existing controls to your page templates and custom .ASPX pages, our hands are tied when it comes to building additional custom functionality.

What we can do is package our custom functionality into a Web Part or a new Server Control, which we can then expose to SharePoint for use in our pages. Both are built in VS, then compiled down to a .DLL that we'll need to install on the SharePoint server. The trick is knowing when to use a Server Control and when to use a Web Part.

Server Controls are more what traditional ASP.NET developers are accustom to implementing in .ASPX pages. Server controls deployed to the SharePoint server become available in SharePoint Designer for use in custom .ASPX pages and SharePoint Page Templates (and masterpages). Server Controls can handle just about any kind of custom functionality you might need and also support design time functionality that your SharePoint Designer people might find useful. I'm a big fan of server controls and use them whenever possible.

Web Parts allow for custom code within a provisioned page ... as in a page created via the site actions menu and there afterward controlled by SharePoint. These pages are typically stored as data in the Pages library, where you have no access to the .ASPX source code at all. Since we cant even drop a Server Control into a provisioned page, the only option we have left is to drop a Web Part into an existing Web Part Zone to get the functionality we need in a particular page instance.

Web parts are actually Portlets. If you are familiar with portlets, you'll notice the same features become available to you within a Web Part. You can allow users to custom configure a Web Part, but for our needs we'll just be using them to create custom code for a public website.

With all that in mind, lets take a look at building a simple Server Control to add some custom functionality to the default out of the box SharePoint master page... (coming soon!)

No comments: