Wednesday, May 5, 2010

LaunchPickerTreeDialog in SharePoint 2010, and the Importance of ScopeToWeb

You know what I hate – when people start a blog, and then go months or years without publishing a topic. And then they come back and say “sorry I was gone, I’ve been busy, blah blah blah it won’t happen again – this time I’m really committed to updating this blog regularly” – whatever. I may take another 15 months off after this post, but here’s something interesting I just discovered about the behavior of the LaunchPickerTreeDialog javascript function included in SharePoint that I think is worth sharing here since it definitely falls under the “voodoo magic” category.

LaunchPickerTreeDialog – what it does

If you’re unfamiliar with LaunchPickerTreeDialog, others have written about it extensively so I won’t go into the details of how it works here. Google is your friend. I will tell you that it prompts the user to select a website, list, or document library using a graphic interface rather than forcing them to type in a URL. It’s well worth your effort to figure it out and use it in your solutions.

What’s New in SharePoint 2010?

So I’m testing some of my custom code on a site collection that was upgraded from SharePoint 2007 to SharePoint 2010, and I discover that when I call LaunchPickerTreeDialog on my site in SharePoint 2010, it is only showing lists and libraries from the current site. In SharePoint 2007, the same code would display all sub-sites as well in the tree view that the dialog box displays. What’s up with that?

image (click to enlarge)

Where’d my sites go? I know I have sub-sites, and I know they showed up in SharePoint 2007:

image (click to enlarge)

My first step towards figuring out what was going on was to load up IE8’s debugger and look at the script tab. The script I'm looking for is in PickerTreeDialog.js – that hasn’t changed from 2007. I put a breakpoint on the first line of code in the LaunchPickerTreeDialog function:

image (click to enlarge)

I was wondering if this was different from the 2007 version, so I did the same thing in my SharePoint 2007 environment. Here’s what that looked like:

image (click to enlarge)

Notice the difference? Aside from the obvious code difference inside the function, the signature changed in the 2010 version: has two new parameters:

  • ScopeToWeb
  • RequireCT

And now, in a blatant attempt to trick the search engines help people find this post, ScopeToWeb, ScopeToWeb, ScopeToWeb. That’s the key. What does this parameter do? What is the purpose? Where is it documented? No clue. But what I do know is that if you pass in an empty string to this parameter, you get your sub-sites. Pass in anything else, you don’t get your sub-sites. Hopefully someone will add in the comments exactly what this parameter is supposed to do, but for now, that’s all you need to know to fix your code.

Of course, if you’re still supporting SharePoint 2007, you can’t just go adding parameters to the LaunchPickerTreeDialog call because you’ll code won’t work in 2007. So you need to do a version check (blah) to make sure you’re only adding this parameter when you’re running on SharePoint 2010. There are several ways to do that – here’s what I chose to do:

   1: if (Microsoft.SharePoint.Administration.SPFarm.Local.BuildVersion.Major >= 14)
   2: {
   3:     // SharePoint 2010 has two new parameters - 
   4:     // ScopeToWeb and RequireCT. ScopeToWeb needs
   5:     // to be empty string to show sub-sites in the tree
   6:     sb.AppendLine("LaunchPickerTreeDialog('CbqPickerSelectListTitle','CbqPickerSelectListText','listsOnly','',serverUrl, '','','','/_layouts/images/smt_icon.gif','',callbackList,'','');");
   7: }
   8: else
   9: {
  10:     sb.AppendLine("LaunchPickerTreeDialog('CbqPickerSelectListTitle','CbqPickerSelectListText',");'listsOnly','',serverUrl, '','','','/_layouts/images/smt_icon.gif','',callbackList);");
  11: }

Wow, do I hate that. Someone rescue me and tell me there’s a better way in the comments. But in the meantime, if you’re running your LaunchPickerTreeDialog in SharePoint 2010 and trying to figure out why you can no longer see your sub-sites in the tree view, now you know – pass in an empty string for the ScopeToWeb parameter.