So lately at work I’ve been developing some reasonably complex document/picture library driven components, resulting in me writing some SPDocumentLibrary/SPFolder helpers for our SharePoint framework. And there’s nothing like a bit of alone time with the object model to remind you just what a monstrous abortion it is… it’s actually quite surreal how much lunacy Microsoft has managed to pack into SP.
Now, let’s play a game! Document/picture libraries consist of folders and files. The top level folder is the root folder which of course does not have a parent… so if you were to invoke the SPFolder.ParentFolder property on a root folder, what would you expect in return? Is it:
- An exquisite exception, baked with love from SharePoint? This isn’t a bad guess as SP does tend to be rather ‘exception happy’.
- Uh… refrigerator!
B is the obviously preferable answer, but A would be adequate (provided that there was some sort of HasParentFolder property/method in place). But of course (!), the answer is C. MSDN says that the parent folder of a root folder is itself… familial weirdness aside, it should then hold that SPDocumentLibrary.RootFolder.UniqueID == SPDocumentLibrary.RootFolder.ParentFolder.UniqueID. But this invariant doesn’t hold and it’s actually far worse… calling ParentFolder on a root folder really gives you a peculiar half-baked SPFolder instance whose ParentList is zeroed out despite its Exists property being set to true and whose parent folder is itself. Hmmm…
Conversely, if you call SPWeb.GetFolder(strUrl) for a non-existent folder, you’ll get a ‘properly’ half-baked SPFolder instance with Exists set to false… I mean they wouldn’t do anything completely outrageous like simply returning null now would they?
Conversely (in another dimension), invoking SPFolder.SubFolders[“IDontExist”] will result in an exception being raised. Make up your mind!
I’ve come across plenty of other funniness in SPFolder alone, but I’ll omit it for now as I’m trying to keep my posts short nowadays (and I’ve blogged about object model oddities before). Most of the object model reads like it was written by capricious bipolar developers who have never seen an API in their lives. And since it’s strangely comforting to hear about other people in the same boat with the same gripes, here’s some literature 😀