Created
July 23, 2012 17:56
-
-
Save umbraesoulsbane/3165005 to your computer and use it in GitHub Desktop.
Umbrae's Tower http://umbraes-tower.blogspot.com/2012/07/sharepoint-2010-and-list-picker-custom.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace QuickLinks.QuickLinks | |
{ | |
public partial class QuickLinksUserControl : UserControl | |
{ | |
// For Custom Properties | |
public QuickLinks WebPart { get; set; } | |
protected override void OnPreRender(EventArgs e) | |
{ | |
base.OnPreRender(e); | |
// For Custom Properties | |
this.WebPart = this.Parent as QuickLinks; | |
// Rest of your code | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ComponentModel; | |
using System.Web; | |
using System.Web.UI; | |
using System.Web.UI.WebControls; | |
using System.Web.UI.WebControls.WebParts; | |
using Microsoft.SharePoint; | |
using Microsoft.SharePoint.WebControls; | |
using System.Collections.Generic; | |
namespace QuickLinks.QuickLinks | |
{ | |
[ToolboxItemAttribute(false)] | |
public class QuickLinks : WebPart, IWebEditable | |
{ | |
private string _qlListProp; | |
[Browsable(false)] | |
[Personalizable(PersonalizationScope.Shared)] | |
public string qlListProp | |
{ | |
get { return this._qlListProp; } | |
set { this._qlListProp = value; } | |
} | |
// Visual Studio might automatically update this path when you change the Visual Web Part project item. | |
private const string _ascxPath = @"~/_CONTROLTEMPLATES/QuickLinks/QuickLinks/QuickLinks.ascx"; | |
protected override void CreateChildControls() | |
{ | |
Control control = Page.LoadControl(_ascxPath); | |
Controls.Add(control); | |
} | |
#region IWebEditable Members | |
EditorPartCollection IWebEditable.CreateEditorParts() | |
{ | |
List<EditorPart> editors = new List<EditorPart>(); | |
// Add the base editor parts | |
editors.Add(new ListPicker(this.ID, "QuickLinks")); | |
return new EditorPartCollection(editors); | |
} | |
object IWebEditable.WebBrowsableObject | |
{ | |
get { return this; } | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment