Wednesday, June 3, 2009

Basic Publishing Site Definition

Andrew Connell wrote in his great SharePoint 2007 WCM book how to create a Minimal Site Definition to use for Publishing Sites. The idea is to have a clean starting point for new Projects which use Features as the main tool for customisation. The Site Definition itself is pretty basic and stripped down to its bare necessities while all the fun stuff lives in Features. A great way to start a project is to plan ahead what functionality will be living in different features, adding basic shell like features to the definition at the start and actually do the coding later. But even if you didn't plan for everything in advance and realise that there is a feature missing or a piece of code that can't be integrated into an existing feature, simple staple it on top of the Site Definition in hind sight and you're as good as gold.
Wrox Press also give you the code for that Book for download on their website. The Solution for the Minimal Site Definition you'll be lookng for lives in Chapter 5. But beware, there are a few bugs in the download. Check the Errata for Errors in Download and fix up the onet.xml and publishing layout page.
One thing that was missed totallly in this definition is the fact that all pages should live in the Pages Library. Also the homepage. In the example from Andrew, the home page lives WSS style in the Root. Not very clean. The fix is simple enough though: Change the url of the page (preferably even changing the default.aspx to actually use the layout properly!) and you're ready to go. Here is my version of the last entry in the onet.xml file:


OLD:
<Modules>
<Module Name="Default" Url="" Path="">
<File Url="default.aspx" NavBarHome="True" Type="Ghostable" />
</Module>
</Modules>

NEW:

<Modules>
<Module Name="Default" Url="$Resources:cmscore,List_Pages_UrlName;" Path="">
<File Url="Default.aspx" Type="GhostableInLibrary">
<Property Name="PublishingPageLayout" Value="~SiteCollection/_catalogs/masterpage/PSATOC.aspx, ~SiteCollection/_catalogs/masterpage/PSATOC.aspx" />
<Property Name="ContentType" Value="$Resources:cmscore,contenttype_welcomepage_name;" />
</File>
</Module>
</Modules>

Thursday, May 28, 2009

Exception handling Base WebPart

This piece of code will revolutionise the way you develop web parts in sharepoint. No more pages crapping out on you, no more crashes or unsolvable problems. All exception handling done for you automatically without the need to include a single try/catch block. I'm not saying you should not handle exceptions which you can forsee. But those are the easy ones. What about those unexpected ones?
Andreas Knudsen, a fellow SharePoint Programmer came up with a beautiful solution which takes full advantage of a combination between early and late binding to force the runtime to execute the exception handling version of the base routines before execuing your implementation code. As I'm not even going to try to take credit for this gem, get the code directly from the genius himself

http://andreascode.blogspot.com/2007/12/general-exception-handling-in-web-parts.html

How does it work? Playing with polymorphism is the answer.
The runtime always tries to find out the best version of a method on an object to call, no matter what type the pointer is.
When you put a line of code like

MyWebPart.CreateChildControls()

into your own code, the runtime will not have to look far, as you're calling it on the type of which the object is. But...

WebPart x = MyWebPart;
x.CreateChildControls();

Now the runtime will need to start at the base class WebPart and figure out which version is the best to call. Normally each new version overrides the old one and the runtime will end up at MyWebPart.CreateChildControls(). Unless you put a stop sign in between. Such a stop sign is the new keyword. When you have a class in between WebPart and MyWebPart (like MyBaseWebPart) and instead of writing public override CreateChildControls() but public new CreateChildControls() the runtime is forced to take a step back and execute the one a level up. To make it more visible you can seal off the exception handling method, just to be sure nobody tries to override it.

So now we've forced the runtime to execute the version of code that has exception handling. How do we get back to our implementation code? Through sidestepping the Stop sign. You need a new class which inherits from the exception handling class for this. the exception handling one calls a new function like CreateChildControlsX and the runtime will again try to figure out the best version and will end up in derived.CreateChildControlsX because you overrode it there. Here you simply call the original function name again (CreateChildControls) and the runtime will find the best version again. Now the runtime is not starting at the top (WebPart) but at the current location (derived) and thus the Stop sign is left behind and it can go all the way down to your implementation and execute that.

I know it sounds so geeky, But thise piece of code is seriously cooooooool.

Tuesday, May 26, 2009

SharePoint Explorer View in Windows 2008 and 2003

If anybody out there has been building demo machines on Windows Server 2008 or Windows Server 2003 you will have run into this issue at some point. Explorer View does not work on document libraries, nor can you map a network drive to a sharepoint site.
Following constellations come together:

a) the server platforms effectively don't like web folder views until you install the fix KB907306
That fix works for both Windows 2003 and 2008.
Now you can happily map a sharepoint site to your network places and use the Windows Explorer to browse the sharepoint sites.

b) your browser now complains that "This folder cannot be opened in Internet Explorer". This will happen when you have IE 7 or higher and protected mode is on. Simple. Disable protected mode via Tools>Internet Options>Security. Remember to add the site to the Intranet Zone

c) What? can't disable the protected mode on Windows Server 2008? No problem. Open the Server Manager. On the root node of the tool you should see Server Summary within there is a section for Security Information. Here you will find a link to Configure IE ESC (Enhanced Security Configuration). That's where you can disable the "microsoft parental controls for dummies" tick box.
d) now you can disable protected mode on the Intranet Zone.

You might ask, why not simply move the site to Trusted Sites. Well, Trusted sites actually have a higher security rating than Intranet zone. Plus you automatically get signed into the intranet zone and not to th trusted sites. Plus the sharepoint site is on the Intranet. So that is also the Zone it should live under!

PS. Running Windows Server 2008R2 or Windows Server 2008 SP1? Then try enabling the Feature called  "Desktop Experience" in the Roles and Features configuration of the Server

Happy Exploring!

Tuesday, April 14, 2009

Custom SQL Lookup Field in WSS 3.0

This is a question I get asked a lot! How to create your own version of the BDC Lookup for either WSS or MOSS. The answer is not simple. Nono. But not impossible either.
You need to create your own custom field that uses the Entity Picker to populate a list of records and allows you to pick one. There is a lot of CAML, XML, Classes, Events and Code to deal with.
Following is sample code to create just that based on another SharePoint list. Simply plug your own DAL instead of the SPQuery and you're up and running.
http://code.msdn.microsoft.com/WSS3EntityPicker

Step by step instructions:
http://msdn.microsoft.com/en-us/library/cc889345.aspx

Thursday, April 9, 2009

Best Practices for SharePoint - Blank Sites

If you ever plan on creating a new site, which starts off on a clean slate, especially with the WCM Publishing Features activated you must use stsadm to do that. Effectively three commands will get you started nice and simple with the most blank publishing site you can immagine:

a) create an absolute blank sitecolleciton with really nothing in it
stsadm -o createsite -url http://servername -ownerlogin domain\username -owneremail owner@domain.com

b) activate the publishing feature on the site collection level
stsadm -o activatefeature -name PublishingSite -url http://servername

c) activate the publishing feature on the web level
stsadm -o activatefeature -name PublishingWeb -url http://servername

That's it. go to your url and you will find the most minimalistic publishing site you will be able to get that has all the publishing features activated.
Now you can start building your own site template based on this and be sure that content deployment will give you less headaches should you ever decide to go down that route in future.

Upgrading WSS 3.0 to MOSS

Just came across a weird one. After doing an in place upgrade of a test installation the Publishing Feature would not activate. Keeps on coming up with an Access Denied message. Ok, so maybe I should have deinstalled WSS 3.0, installed MOSS clean and then reattached the content databases to be on the safe side. But hey, i did say it was a test machine.
The solution is not that obvious though. Looks like the Publishing Feature can't activate because the feature dependency Publishing Resources is not actice. Only solution is to activate the Resources manually via stsadm and then you can activate the publishing feature via the gui.

here's the magic command:
stsadm -o activatefeature -name PublishingResources -url http://YourSharepointSite
Happy upgrading!

Customising the SharePoint Calendar

A few days ago I came across a great post by syed.er's blog called Bending SharePoint without bending the rules. In November he talked about adding more items to the calendar view by adding MaxvisibleEvents to the Rendering Template for the Calendar. I decided to take that concept a level further.

Following problem scenario:
We need to be able to colour code calendar entries, add more than just the title to the item and break down the multi day events into separate blocks spanning across the whole day and not just a thin bar at the top which is easily skipped. All this without changing the insides of SharePoint. And the solution could not be simpler!

First of all you will need to create your own WebPart for the calendar. Sound daunting? not at all! you can easily implement the SPCalendarView control to do all the work for you.
Gunnar Peipmann has a great post on how to create a global calendar from multiple sources using the SPCalendarView.

The SpCalendarView control can also accept new Templates. So you copy the Rendering Templates for the Calendar Control from the C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES \defaulttemplates.ascx into a new ascx file, rename them and add the fields you want to the Item views. Then hook them up programatically with the SPCalendarView control and render the new output.

WARNING! Don't put the custom Rendering Templates into a subfolder. Although it might make perfect sense, they won't load properly programatically. Just use clear and concise naming conventions for your custom ascx page. Then use the SPControlTemplateManager.GetTemplateByName() function to get hold of the ITemplate programatically and plonk it into the Calendar View.
If you insist on having the custom template in a subfolder, check out Reverse Alchemy's post on loading templates programatically. He will show you how to use Page.LoadControl() as a workaround to SharePoint misbehaving.

You will need to populate the list of events manually though. I.e. use a SPQuery to get the events in a collection, iterate through the collection and create new Calendar Entries based on the list item. But Gunnar's blog will guide you through that. While you're recreating the calendar entries try changing the Backgroundcss attribute of the entry. :-) and voilla you have a colour coded calendar that has all the functionality of the normal calendar.
Ok. Maybe it is not quite as easy. I'll upload a step by step instruction sometime after easter if enough people pester me about it...