Google Analytics

Thursday, April 28, 2011

A Poor Man's Date Filter Web Part

Recently, I was asked to create a date filter to give users an easy mechanism to filter down some data in a list.

Normally, I'd create myself a view on the list then edit the page and add a date filter web part.  At that point, all you have to do is wire up the date filter and the list view web parts and you're done (see this article for more information).

That's all fine and dandy if you've got the SharePoint Enterprise license.  All of the filter web parts are in the Enterprise feature.  What if you don't have Enterprise?  Am I out of luck?

Absolutely not.  Instead of the Date Filter Web Part, why not slap on a Form Web Part?  This little guy lets you define your own HTML to filter with.  Need a text box?  Great.  Need a check box, dropdown list, ...?  Great.  Need a calendar?  Uhm...wait, there's no easy HTML control for a date picker.

Ah, but you're in SharePoint.  SharePoint has date pickers all over the place.  An easy way to toss one on a page is to add a Microsoft.SharePoint.WebControls.DateTimeControl server control to it.  If we can figure out what that control adds to the page, we're golden.

Modify the Form Web Part and add the below HTML to the Source Editor.  The text is red is what renders the date picker functionality onto the page.

<div onkeydown="javascript:if (event.keyCode == 13) _SFSUBMIT_">
  <SCRIPT language='javascript' src='/_layouts/datepicker.js'></SCRIPT>
  <SCRIPT language='javascript'>var g_strDateTimeControlIDs = new Array();</SCRIPT>
  <SCRIPT language='javascript'>g_strDateTimeControlIDs[""] = "ctl00_PlaceHolderMain_ctl00_Date";</SCRIPT>
  <input name="ctl00$PlaceHolderMain$ctl00$Date" type="text" maxlength="45" id="ctl00_PlaceHolderMain_ctl00_Date" class="ms-input" AutoPostBack="0" />
  <A href="#" onclick='clickDatePicker("ctl00_PlaceHolderMain_ctl00_Date", "\u002f_layouts\u002fiframe.aspx?&cal=1&lcid=1033&langid=1033&ww=0111110&fdow=0&fwoy=0&hj=0&swn=False&minjday=109207&maxjday=2666269&date=", ""); return false;' >
    <IMG id=ctl00_PlaceHolderMain_ctl00_DateDatePickerImage src="/_layouts/images/calendar.gif" border="0" alt="Select a date from the calendar."></IMG>
  </A>
  <IFRAME id=ctl00_PlaceHolderMain_ctl00_DateDatePickerFrame SRC="/_layouts/images/blank.gif" FRAMEBORDER=0 SCROLLING=no style="DISPLAY:none;POSITION:absolute; width:200px; Z-INDEX:101;" title="Select a date from the calendar."></IFRAME>

  <input type="button" value="Go" onclick="javascript:_SFSUBMIT_"/>
  <input type="button" value="Reset" onclick="javascript:_SFRESET_"/>
</div>

Awesome!  Now we've got a date picker in our Form Web Part.  All that's left is to wire it up to our list view web part the same way we always did.

9 comments:

  1. Ken this is fantastic.
    But how can i get the value of the calendar and how can i use it to filter a list.
    Cheers
    Patrick
    Email me to: naijacoder@hotmail.com

    ReplyDelete
  2. Patrick,
    You'd follow the same steps you would to connect any two web part that allow connections.

    Click on the down arrow for HTML Form Web Part and choose 'Edit Web Part'. Click the same arrow and this time hover over 'Connections'. A flyout should appear saying 'Provide Form Values To'. Hover over that flyout and names of other web parts on the page should apper. Click the name of the web part you want to connect to. In my case, I have a web part on the page called 'Shared Documents'. After you choose the web part to connect to, a new dialog box should pop up.

    Connection Type: Get Filter Values From
    Click 'Configure'.

    Provider Field Name: ctl00$PlaceHolderMain$ctl00$Date
    Consumer Field Name: Choose the date field to filter on (e.g. Modified)
    Click 'Finish'.

    That should be all you need to do.

    Ken

    ReplyDelete
  3. Hi Ken,
    Thanks for the reply to my email.
    Can i have two input textboxes?
    As i need to filter the list with 2 params between date.
    Cheers

    ReplyDelete
    Replies
    1. Patrick,
      In order to filter on a date range, put two Form web parts on the page and send the values to a Data View Web Part as parameters, not filters. You would have to modify the XSL inside the DVWP to take the appropriate filter actions.

      Ken

      Delete
    2. IS there an example of this?

      Delete
    3. I don't have an example of this. Laura Rodgers has a post, not specifically on this, but could help.

      https://sharepointdreamer.com/2011/07/12/how-to-send-filtered-data-from-an-external-list-to-a-data-view-web-part-of-a-form-page/

      Just to be clear though, this is not the approach I would take in today's world. There are too many open source calendars and SharePoint provides some nice REST APIs now. You should leverage those if you can.

      Delete
  4. Hi,
    Is it possible to set default value to today? without using javascript or jquery function?

    Sandra

    ReplyDelete
    Replies
    1. Sandra,
      I think you'd have to use javascript to accomplish this. And yes, jQuery can make this simpler.

      Ken

      Delete