Bilbro Bloggins

Musings of Brian Bilbro

Dropdown List Box Filter Web Part in SharePoint – Part One

This is the first part of a multipart blog series.

  1. In part one, we created a simple SharePoint Web Part which displayed a text label that could be set via the Web Part Properties Editor.
  2. In part two of this blog series, we extended the simple SharePoint Web Part by adding a dropdown list box and populating it with values from a SharePoint list.
  3. In part three, we add the capabilities to use the selected value in the dropdown list box to filter various SharePoint lists on the page.
  4. In part four, you can download the Visual Studio 2008 project solution.

 

Blog Series Summary:

As a part of a proof of concept site I was creating in SharePoint, I wanted to learn how to create a web part in SharePoint that can communicate to other web parts on the page.  The web part that I wanted to create was a drop-down list box that would display a list of projects.   Selecting a project on the drop-down list would then drive the contents of the various SharePoint project lists (issues, notes, risks, etc…) on the SharePoint page.]

 

Part One:

In part one of this blog, we’ll make a simple SharePoint Web Part that has a text label utilizing Visual Studio Extensions for Windows SharePoint Services.  You’ll be able to set the text of the label in the SharePoint web part property editor.

Step 1 – Create a SharePoint Web Part project

Step01.CreateNewWebPartProject

 

Step 2 – Delete the WebPart1 that is created by default when you create a SharePoint Web Part project.

Step02.DeleteWebPart1

Step 3 – Add the dropdown filter Web Part to the project.

Step03.AddWebPart

 

Step 4 – Add the necessary code in the CreateChildControls method that creates an instance of the label control.

private Label _textLabel = null;
protected override void CreateChildControls()
{
    base.CreateChildControls();
    _textLabel = new Label();
    this.Controls.Add(_textLabel);
}

Step 5 – Add the label text property (TextLabel).

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.User),
        WebDescription("Text"),
        Category("Dropdown Filter"),
        WebDisplayName("Text")]
        public string TextLabel
        {
            get 
            {
                EnsureChildControls();
                return _textLabel.Text;
            }
            set
            {
                EnsureChildControls();
                _textLabel.Text = value;
            }
        }
The TextLabel property takes advantage of some attributes.

·         WebBrowsable – Setting this to true will allow this property to be set with the Web Part property editor.

·         Personalizable – This allows the property value to be persistable at the web part control level.

·         WebDescription – This is the tool tip that will be displayed in the web part property editor when you mouse over the property.

·         Category – When the web part property editor is display this controls which category in the editor the property should be displayed.

·         WebDisplayName – This is the friendly name of the property that you would like to display in the web part property editor.

An item of note:

using statements need –The Category reference will require the following to be added to your list of using statements at the top of the class. 

using System.ComponentModel; 

 

Step 6 – Set a friendly title and description for your web part.  You can set these values in the .webpart xml file associated with your web part.

<?xml version="1.0" encoding="utf-8"?>
<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <!--
      The following Guid is used as a reference to the web part class, 
      and it will be automatically replaced with actual type name at deployment time.
      -->
      <type name="83f9fbbf-dba8-4a8e-a1f6-cafacbaafbbb" />
      <importErrorMessage>Cannot import DropDownFilterBlog Web Part.</importErrorMessage>
    </metaData>
    <data>
      <properties>
        <property name="Title" type="string">DropDown List filter web part</property>
        <property name="Description" type="string">This web part will allow you to 
        populate the dropdown list from a SharePoint list and then use the value
        in the dropddown to filter other web parts on the page.</property>
      </properties>
    </data>
  </webPart>
</webParts>

 

Step 7 – Set the URL to your SharePoint server:  Right click project -> Properties -> Debug -> Start browser with URL.

Step07.a.WebPartProperties

 

 Step07.b.DebugURL

 

Step 8 – Build and Deploy the web part to your SharePoint server. Right click project -> Deploy

Step08.b.Deploy

Step 9 – This step is not necessarily but if you want to see, you should see your web part in the Site Collection Feature list and it should already be activated.

Step09.Review.SiteCollectionsFeature

Step 10 – Edit a page in SharePoint that allows web parts.

Step10.EditPage

 

Step 11 –Add the web part to your page.

  Step11.a.AddaWebPart

Step11.b.SelectTheWebPart

 

Step 12 – Bring up the Web Part Properties Editor.

Step12.ModifySharedWebPart

Step 13 – Set the value for the Text label.

Step13.SetTextValue

 

Step 14 – After selecting OK on the editor and then exiting the page edit, you should see your text displayed in the web part.

Step14.TextWebPartDisplayed

 

This completes part one of Dropdown Filter web part.   You can proceed to part two to learn more by adding the dropdown list box and populating it with values from a SharePoint list.

» Similar Posts

  1. Dropdown Filter Web Part in SharePoint – Part Two
  2. Dropdown Filter Web Part in SharePoint – Part Three
  3. Creating a Lookup List for SharePoint with VSeWSS

» Trackbacks & Pingbacks

    No trackbacks yet.
Trackback link for this post:
http://bilbrobloggins.com/trackback.ashx?id=4
 

» Comments

  1. rediska avatar

    hi,

    Great post and on time. I have a customer who wants to filter list by using double drop-down filter.

    (for example: by category and by number of items to display.)

    What is the best way to do it?

    rediska — December 16, 2008 5:14 AM
  2. Brian Bilbro avatar

    Hi Rediska,

    I'm still very much learning SharePoint myself and I purposely created this blogs to post my learnings as I go. I haven't specifically had to deal with your scenario above.

    Having said that, I do have a few comments on this.

    1) SharePoint list web parts cannot receive multiple connections from different web parts. So, at minimum, you probably would want to have one filter control as above. You could modify the control here and add a second drop-down with the list size. However, the SharePoint list web part doesn't appear to do anything with multiple values. I just did a quick test with the Choice Filter and a SharePoint List. On the Choice Filter, I turned on the multiple selection options. When I tried to filter a SharePoint list with multiple options, it appears to only pick up one value.

    2) A SharePoint list web part has paging capabilities. You can modify the view to display only a certain number of items which you can configure in the Limit Item section. Unfortunately, that is not a configuration any user can modify on the fly.

    If I was tackling that problem, I would modify the dropdown filter web part as discussed in this article to include a second drop-down with page size. I would then write a second web part that can receive multiple filter values and use the SharePoint APIs to display that data that I needed.

    Brian Bilbro — December 18, 2008 10:56 AM
  3. Shoaib Adil avatar

    what is the value in the name parameter of the line

    <type name="83f9fbbf-dba8-4a8e-a1f6-cafacbaafbbb" />

    i m haing problem in this little web part... cant get it right

    Shoaib Adil — February 23, 2009 5:10 AM
  4. Brian Bilbro avatar

    Shoaib,

    if you are refering to step 6, you really don't have to do anything to that file. You can leave it alone if you want.

    That file is only if you want to have a custom title or description.

    Brian Bilbro — February 26, 2009 8:15 PM
  5. Miles avatar

    Great article.

    Here's the code to pull a list from a different site:

    [WebBrowsable(true),

    Personalizable(PersonalizationScope.User),

    WebDescription("Source Site Name"),

    Category("Dropdown Filter"),

    WebDisplayName("Site Name (i.e. http://ksdev7)")]

    public string SourceSiteName

    {

    get { return _sourceSiteName; }

    set { _sourceSiteName = value; }

    }

    and then change your SourceList Get:

    try

    {

    SPWeb web = new SPSite(SourceSiteName).OpenWeb();

    _sourceList = web.Lists[SourceListName];

    }

    Also, one small note: if you create your list item collection outside of the FOR loop, you can save hundreds of database hits.

    SPListItemCollection items = SourceList.GetItems(oQuery);

    for (int i = 0; i < items.Count; i++)

    {

    I put a sample that uses a SP query on my blog here:

    bluesurftech.com/.../Post.aspx

    Thanks.

    Miles — June 30, 2009 4:41 PM
  6. Biju avatar

    Hi,

    I have a feature(SPItemEventReceiver & SPFeatureReceiver) with a Webpart, deployed on the server. On deactivating the feature, through the FeatureDeactivating event i delete the webpart from the "Web Part Gallery" programatically.

    Now again on activating the feature(through SiteAction->Site Setting-> Site Feature) the webpart is not availbale on the webp part gallery (SiteAction->SiteSetting-> WebParts(Galleries)), but when i check it in the "Web Part Gallery: New Web Parts" (http://www.demoportal.com/_layouts/NewDwp.aspx) I could find it over there.

    How can i populate the webpart programatically on FeatureActivated event?????

    Could anyone please suggest me a solution or some sample code?

    With regards

    --------------------------------------------------------------------------------

    Biju

    Biju — September 23, 2009 4:17 AM
  7. liz avatar

    Hi Brian! Thanks for this helpful post! Is there a way to "clear the filter" on page load? :)

    liz — September 28, 2010 12:03 AM
  8. Jacquie avatar

    Hi - thanks for useful site - Having introduced a web part in order to introduce column heading text wrap, my list is no longer visible! I have tried to use site actions / edit page and although the base of page says "Done" there are no editing options available on this particular page. I trialled column title text wrap on previous site and this worked without problem. How can I resolve or at least get the edit web part available again?

    Jacquie — April 13, 2011 5:03 PM
  9. nagendra avatar

    hi biju,

    deploye your webpart in _catalogs/wp directly when the feature activated, and remove webpart from webpart galary when feature deactivated.

    see following link that will help to u

    msdn.microsoft.com/.../ff798301.aspx

    thanks,

    nagendra

    nagendra — March 8, 2012 5:46 AM

» Leave a Comment