Skip to content

Instantly share code, notes, and snippets.

@umbraesoulsbane
Created July 23, 2012 17:56
Show Gist options
  • Save umbraesoulsbane/3165005 to your computer and use it in GitHub Desktop.
Save umbraesoulsbane/3165005 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI;
using Microsoft.SharePoint;
namespace QuickLinks
{
public class ListPicker : EditorPart
{
private TextBox txtListUrl;
private Button _selectList;
private String WebPartName = String.Empty;
public ListPicker(string webPartID, string webpartname)
{
this.ID = "ListPicker" + webPartID;
this.Title = "Select a Quick Links Enabled List";
this.WebPartName = webpartname;
}
protected override void CreateChildControls()
{
base.CreateChildControls();
txtListUrl = new TextBox();
//_listUrl.Enabled = false;
Controls.Add(txtListUrl);
_selectList = new Button();
_selectList.OnClientClick = "javascript:launchPicker();";
_selectList.Text = "...";
Controls.Add(_selectList);
Controls.Add(new LiteralControl("<br /><br />"));
}
public override void SyncChanges()
{
EnsureChildControls();
if (WebPartName.Equals("QuickLinks"))
{
QuickLinks.QuickLinks webPart = WebPartToEdit as QuickLinks.QuickLinks;
if (webPart != null)
{
txtListUrl.Text = webPart.qlListProp;
}
}
}
public override bool ApplyChanges()
{
EnsureChildControls();
if (WebPartName.Equals("QuickLinks"))
{
QuickLinks.QuickLinks webPart = WebPartToEdit as QuickLinks.QuickLinks;
if (webPart != null)
{
webPart.qlListProp = txtListUrl.Text;
}
}
return true;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
string webLocale = SPContext.Current.Web.Locale.LCID.ToString();
Page.ClientScript.RegisterClientScriptInclude("PickerTreeDialog", string.Format("/_layouts/{0}/PickerTreeDialog.js", webLocale));
RegisterSelectListScript();
}
private void RegisterSelectListScript()
{
StringBuilder launchPicker = new StringBuilder();
launchPicker.Append("<SCRIPT LANGUAGE='JavaScript' >");
launchPicker.Append("var lastSelectedListSmtPickerId = '';");
launchPicker.Append("function launchPicker() {");
launchPicker.Append(" if (!document.getElementById) return;");
launchPicker.Append(" var listTextBox = document.getElementById('" + txtListUrl.ClientID + "');");
launchPicker.Append(" if (listTextBox == null) return;");
launchPicker.Append(" var serverUrl = '\u002f';");
launchPicker.Append(" var callback = function(results) {");
launchPicker.Append(" if (results == null || results[1] == null || results[3] == null) return;");
launchPicker.Append(" lastSelectedListSmtPickerId = results[0];");
launchPicker.Append(" var listUrl = '/';");
launchPicker.Append(" if (results[3].charAt(0) == '/') results[3] = results[3].substring(1);");
launchPicker.Append(" listUrl = listUrl + results[3];");
launchPicker.Append(" listTextBox.value = listUrl;");
launchPicker.Append(" disablePostbackControls();");
launchPicker.Append(" " + Page.ClientScript.GetPostBackEventReference(txtListUrl, string.Empty) + ";\n");
// __doPostBack('ctl00_MSOTlPn_EditorZone_Edit0g_15f9eea6_e2c8_4a1f_9772_0a32b6394eb2_CBQToolPartshowItemsFromListRadioButton', listTextBox.value);
launchPicker.Append(" };");
launchPicker.Append("LaunchPickerTreeDialog('CbqPickerSelectListTitle','CbqPickerSelectListText','websLists','', serverUrl, lastSelectedListSmtPickerId,'','','/_layouts/images/smt_icon.gif','', callback, '', '');");
launchPicker.Append("}");
launchPicker.Append("</SCRIPT>");
if (!Page.ClientScript.IsClientScriptBlockRegistered("launchPicker"))
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "launchPicker", launchPicker.ToString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment