In this post you will learn how to implement a Picker Tree Dialog into your custom SharePoint 2007's pages and handle the postbacks generated when and user press the OK or the Cancel button. Searching on the web i've found differents implementations, but none of them cares about postbacks that are generated once you submit the form.
If you didn't recognize what is a SharePoint's Picker Tree Dialog, look at this image:
This control comes only if yopu are using SharePoint's Moss (standard and enterprise) versions and is not avaible in Windows SharePoint Services (WSS). It allows you to browse the entire SiteCollection and to select Sites, Lists and Document Libraries. Now you will learn how to retreive the selected items on server side using postbacks.
I will show how to use this control into a SharePoint's custom Page that is located under the /_layouts/ directory, but it won't be complicated to implement this tecnique into a SharePoint's WebPart. Let's start....
First you need to import a directive for SharePoint's dll into the ASPX markup page:
<%@ Import namespace="Microsoft.SharePoint" %>
and then add a reference to the PickerTreeDialog.js:
<script type="text/javascript" language="javascript"
src='<%= String.Format("/_layouts/{0}/PickerTreeDialog.js", SPContext.Current.Web.Locale.LCID.ToString()) %>'>
</script>
As you see you can see, I use a localized PickerTreeDialog, so it doesn't matter if your web site is in english or in a different culture.
Next you need to add 2 javascript funcions that will care on showing up the Picker Tree Dialog and handling the Postbacks caused from pressing the OK button.
Add this 2 JavaScript functions:
<script type="text/javascript">
function performPostBack(param) {
if (param.toString().indexOf("DELETE") == 0) {
if (confirm('Are you sure you want to delete this configuration?')) {
__doPostBack("<%= this.UniqueID %>", param);
}
}
else {
__doPostBack("<%= this.UniqueID %>", param);
}
}
function launchPicker()
{
var url = "<%= SPContext.Current.Web.Url %>";
var callback = function(arr)
{
if (arr == null || arr == undefined) {
return;
}
var strparam = "PICKER,";
for (indx in arr) {
strparam = strparam + arr[indx] + "§";
}
performPostBack(strparam);
}
LaunchPickerTreeDialog('CbqPickerSelectListTitle',
'CbqPickerSelectListTitle', '', "",
url, null, "", "", "/_layouts/images/generic.png", 0, callback);
}
</script>
The performPostBack() funcion will perform the Page's postback and launchPicker() function will pop up the control.
To invoke the launchPicker() function, let's use a classic Asp.Net Button control:
<asp:Button runat="server" CausesValidation="false" Text="..." OnClientClick="javascript:launchPicker();" />
Now we need to implement the IPostBackEventHandler interface into the page's code Behind, to be able to capture postback's paramters from server side.
Use the interface:
public partial class YourPageName : System.Web.UI.Page, IPostBackEventHandler
and implement the required method:
public void RaisePostBackEvent(string eventArgument)
{
string[] events = eventArgument.Split(',');
switch (events[0].Trim())
{
case "PICKER":
PickerParameterParser(events[1]);
break;
}
}
private void PickerParameterParser(string pickerParams)
{
string[] param = pickerParams.Split(new char[] { '§' }, StringSplitOptions.RemoveEmptyEntries);
}
Now, everytime the Ok button is pressed your PickerParameterParser() method will be invoked and the param[] array will contain all the PickerTreeDialog parameters that are passed using the callback.
Lets's see what happens everytime you select a different item from the Picker Tree Dialog:
The Root site has been selected:
param[0] = Area:?Type:Guid:
param[1] = SiteRelativeUrl
example:
param[0] = Area:?SPWeb:33cc66fc-c251-47a1-984a-3bb7e751a6cb:
param[1] = /
A sub site has been selected:
param[0] = Type:Guid:
param[1] = SiteRelativeUrl
example:
param[0] = SPWeb:fd00f025-b7df-4eae-83c0-7aec11c9929c:
param[1] = /YourSubSiteName
A List has been selected:
param[0] = Type:Guid:ParentType:ParentGuid
param[1] = SiteRelativeUrl
param[2] = ListName
example:
param[0] = SPList:a525db03-551b-44c4-9450-e7add15e510c?SPWeb:33cc66fc-c251-47a1-984a-3bb7e751a6cb:
param[1] = /
param[2] = YourListName
It's not hard to understand that a possibile PickerParameterParser implementation could be something like:
private void PickerParameterParser (string pickerParams)
{
string[] param = pickerParams.Split(new char[] { '§' }, StringSplitOptions.RemoveEmptyEntries);
if (param[0].StartsWith("Area"))
{
// add your code to handle when a rootsite is selected
}
else if (param[0].StartsWith("SPWeb"))
{
// add your code to handle when a SharePoint site is selected
}
else if (param[0].StartsWith("SPList"))
{
// add your code to handle when a SHarePoint List or Document library
// is selected
// Here's an example: let's write into a TextBox, the selected list's url:
//texBoxListUrl.Text = String.Concat(SPContext.Current.Site.Url,
// SPContext.Current.Site.OpenWeb(param[1]).Lists[param[2]].DefaultViewUrl);
}
}
Have fun...