Monday, July 23, 2012

Sharepoint 2010 and List Picker Custom Web Part Property

One of the frustrations I have learning to code for Sharepoint is that the amount of information available is miserable. Either you get incomplete or unhelpful information in the way of sterile Microsoft Documentation, or any fool with a blog that has the potential to understand just as little as the reader about the subject matter. And YES I do - in fact - understand I fall into that category as well, but at least I try to be open about my ignorance and only try to post information which was helpful to me and my process of learning. So if you are learning to code from me, I sincerely apologize and hope you have some other resources at your disposal.

In any case, even when you find some one knowledgeable you also run into the fact that they may not specify whether they are talking about Sharepoint 2003, 2007 or 2010 (I ONLY deal with Sharepoint 2010 on my blog) or maybe leave off information they deem too complex or too simple. As was the case when I needed to have a custom webpart property that allowed a users to search for a List. I found DOZENS of ways to accomplish this that ranged from super simple (and useless) to absurdly complex. I tried many of these and most did not work properly or how I would expect. MS has several webparts that to do this (i.e. Content Query Web Part), so why is doing it yourself such a mystery?

As some background, for my project I needed a web part that would take a list, read in the rows and items and display this in a branding friendly manner for our publishing sites. As such, I wanted Content Owners to be able to add the web part and select a list they created somewhere in the site for it to use. I called this web part "QuickLinks" so keep that in mind as you read my code.

First off, you should understand a bit about adding custom properties to a web part. Here are a few links I found very helpful:
http://morshemesh.blogspot.com/2011/05/adding-custom-properties-to-visual-web.html
http://www.lamber.info/post/2010/05/21/How-do-I-create-custom-properties-in-Visual-Web-Parts.aspx
http://naimishpandya.wordpress.com/2012/01/12/how-to-add-custom-web-part-properties-to-visual-web-part-in-sharepoint-2010/

Once you have a handle on adding a custom property then a List Picker will not contain many surprises. The main difference is that you have to (for lack of a better term) build your picker before it can be added as a functional control. Even though you are calling a built in tool that MS uses and has available, you have to set the stage and provide some code before you add it. This seems like a little overkill to me, but I guess MS likes to make us developers sweat a little.

Here is my code for the ListPicker. As a note, I pieced this together from a variety of sources. Many of which served other purposes, so please forgive me if there is something weird in there.


Now you have your ListPicker ready for use, so we will use the other tricks to add this as a custom property. Next make sure your webpart.cs file defines the ToolboxItem as IWebEditable. I have included my entire QuickLinks.cs file below for an example.


Finally you will want to add the few lines to your main webpart code behind to so you can use the custom property. I have a stub of code below to demonstrate how I added it:


If this works for you like it worked for me, you just spent less time adding the List Picker than searching for how to do it. One thing that bothers me is I have seen this code for 2007 and it is not much different, so hopefully this is a supported way of doing this. However, if it works maybe its worth the risk.

Happy Sharepointing!

2 comments:

  1. Nice post....I would like to point out that when creating Editor part, implementing IWebEditable is one way of creating editor parts. The other way is to just override the CreateEditorParts method.

    ReplyDelete
    Replies
    1. Thanks for the comment, Suhas. Any insight into the differences between these methods or benefits to using one vs the other? In looking briefly at other postings about them they seem very similar in practice.

      Thanks again.

      Delete