Home > Best Practices, Development, SharePoint Server 2010 > Using List Picker Control in Custom WebPart Properties

Using List Picker Control in Custom WebPart Properties

if you are developig custom webpart and one of the requirements is getting list title as webpart property. so, you can do that by just adding custom Property as text and the user write list title and you in code behind will handle and validate list title and check if this list already exist or not. Also if you want to query , add or update list in subsite , you will need to add relative path to the list. to override all of these , you can add Out of the Box List Picker, just you will develop custom toolpane for the list and add TextBox and Button to handle user Click.

Let us do an example ,

1-firstly, we need to create custom toolpane Class as the following, here, we need two controls TextBox to get Full path of selected List and Button to handle User Click.

namespace SharePoint14.Webparts.QuickLinkWebPart

{


public
class
ListPicker: EditorPart

{


TextBox txt;


Button btn;

2-then add Custom ToolPane ID and Title in Construct ,also define Control Init function.

public ListPicker(string webPartID, string webpartname)

{


this.ID = “listpicker” + webPartID;


this.Title = “Custom WebPart Settings”;


//this = webpartname;


this.Init += new
EventHandler(ImagePickerToolPart_Init);

}

3- define Init Function, in this function will add define Script Link Control , and in Name Property add “AssetPickers.JS” Load OOTB JavaScript File which is responsible for Opening nice picker dialog where you can choose the List

private
void ImagePickerToolPart_Init(object sender, EventArgs e)

{

txt = new
TextBox();

btn = new
Button();

sl = new
ScriptLink();

lit = new
Literal();

sl.Name = “AssetPickers.js”;

sl.Localizable = true;

sl.Language = “javascript”;

btn.Text = “…”;

SyncChanges();

}

4- Define CreateChildControls()

protected
override
void CreateChildControls()

{


base.CreateChildControls();

Controls.Add(sl);


this.Controls.Add(new
Label() { Text = “Select a List” });


this.Controls.Add(new
HtmlGenericControl(“br”));

Controls.Add(txt);

Controls.Add(btn);


string strRelativeWebURL = SPEncode.HtmlEncode(SPControl.GetContextWeb(Context).ServerRelativeUrl);


if (strRelativeWebURL.Trim() ==“/”)

{

strRelativeWebURL =“”; //blank browses the root spweb

}


string strAssetPickerConfig = string.Format(@”

<script type=’text/javascript’>

with(new AssetPickerConfig(‘testAssetPickerObj’))

{{

DefaultAssetImageLocation=”;

CurrentWebBaseUrl='{0}’;

OverrideDialogFeatures=”;

OverrideDialogTitle=”;

OverrideDialogDesc=”;

OverrideDialogImageUrl=”;

AssetUrlClientID='{1}’;

AssetTextClientID=”;

UseImageAssetPicker=true; //make this false to show Documents instead

DefaultToLastUsedLocation=true;

DisplayLookInSection=true;

ReturnCallback = null;}}

</script>”,

strRelativeWebURL,

txt.ClientID);

lit.Text = strAssetPickerConfig;

Controls.Add(lit);

btn.OnClientClick = “APD_LaunchAssetPickerUseConfigCurrentUrl(‘testAssetPickerObj’); return false;”;

}

i hope this help you

Advertisements
  1. G_Coder
    June 5, 2013 at 7:52 pm

    I could not get the AssetPickerConfig to work on SP 2013. I tried both the ReturnCallback and AssetUrlClientID.

  2. August 2, 2013 at 9:06 am

    It’s amazing to visit this site and reading the views of all colleagues on the topic of this piece of writing, while I am also zealous of getting experience.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: