Sharepoint Folder Picker WebPart ( along with List and Site Picker )
There are a lot of places where Sharepoint provides a nice Picker to select a list ( and in some cases sites itself ). Even even if the Picker shows Folders of the list, you cannot select the folder for your use.
In this post, I will show you how you can select the folders too, by the list picker provided by sharepoint. The code for this post is attached at the end of the post.
For this webpart, first we need a reference to the PickerTreeDialog.js file
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Page.ClientScript.RegisterClientScriptInclude("PickerTreeDialog",
string.Format("/_layouts/{0}/PickerTreeDialog.js", "1033"));
if (_hiddenSelectionField != null && !string.IsNullOrEmpty(_hiddenSelectionField.Value))
{
_listUrl.Text = GetObjectUrl(_hiddenSelectionField.Value);
_hiddenSelectionField.Value = string.Empty;
}
}
Next we need to call the ‘LaunchPickerTreeDialog’ javascript function defined in the PickerTreeDialog.js file.
function LaunchPickerTreeDialog(title, text, filter, anchor, siteUrl, select, featureId, errorString, iconUrl, sourceSmtObjectId, callback)
There are a few parameters that has to be passed to the LaunchPickerTreeDialog js function. Some of the vital ones are as follows..
- siteurl : determines which site collection the treeviewpicker.aspx will show data from.
- filter: ‘WebsOnly’ will show only webs, if omitted will show all webs, lists and folders
- callback: the js function that will be called with the consequences of the selection.
private void RegisterSelectListScript(SPWeb web)
{
StringBuilder launchPicker = new StringBuilder();
launchPicker.Append("");
if (!Page.ClientScript.IsClientScriptBlockRegistered("launchPicker"))
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "launchPicker", launchPicker.ToString());
}
The callback function will return an array of consequences...
arr[0] = smtPickerSelection.Value
arr[1] = smtPickerSelectionWebServerRelativeUrl.Value
arr[2] = smtPickerSelectionListName.Value
By the first value.. we can determine which type of Object is selected from the picker tree. Examples of valueSeletion at arr[0] are
if a web is selected
SPWeb:18565691-e8a0-4839-9b0b-4a259069a789:
if a list is selected
SPList:0044bafc-6559-4bc0-bb8e-b7326af5ebce?SPWeb:27ab36b4-dd10-49de-8094-ff5b2e3fe34c:
if a folder is selected
SPFolder:75f2065c-51f6-4007-9fcf-0a01a18eeeae?SPWeb:27ab36b4-dd10-49de-8094-ff5b2e3fe34c:
Next we pass this values to the following function to return the proper productivity.
public string GetObjectUrl(string ValueFromTreePicker)
{
string[] val = ValueFromTreePicker.Split('&');
string objUrl = string.Empty;
string siteUrl = string.Empty;
string guidVal = string.Empty;
string tempGuid = string.Empty;
if (val.Length > 1)
{
guidVal = val[0];
siteUrl = val[1];
}
else
{
return ValueFromTreePicker;
}
SPSecurity.RunWithElevatedPrivileges(delegate()
{
by (SPSite siteCollection = new SPSite(SPContext.Current.Site.WebApplication.GetResponseUri(SPContext.Current.Site.Zone).ToString()
+ siteUrl.TrimStart('/')))
{
by (SPWeb web = siteCollection.OpenWeb())
{
if (guidVal.StartsWith("SPWeb:"))
{
objUrl = siteUrl;
}
else if (guidVal.StartsWith("SPList:"))
{
tempGuid = guidVal.Substring(0, guidVal.IndexOf('?'));
tempGuid = tempGuid.Substring("SPList:".Length);
SPList list = web.Lists[new Guid(tempGuid)];
objUrl = list.ParentWebUrl + "/" + list.Title;
}
else if (guidVal.StartsWith("SPFolder:"))
{
tempGuid = guidVal.Substring(0, guidVal.IndexOf('?'));
tempGuid = tempGuid.Substring("SPFolder:".Length);
objUrl = web.GetFolder(new Guid(tempGuid)).ServerRelativeUrl;
}
else
{
objUrl = ValueFromTreePicker;
}
}
}
});
return objUrl;
}
Download Code
SharePoint 2010 Machinate Code
Just the webpart cs file
Ref :
http://www.tonstegeman.com/Blog/Lists/Posts/Post.aspx?ID=72
Check it out:SharePoint 2010










Answers Rating