{"id":115,"date":"2012-11-21T16:40:41","date_gmt":"2012-11-21T21:40:41","guid":{"rendered":"http:\/\/blogs.visigo.com\/chriscoulson\/?p=115"},"modified":"2012-11-21T16:42:40","modified_gmt":"2012-11-21T21:42:40","slug":"filter-a-sharepoint-calendar-list-with-a-date-picker","status":"publish","type":"post","link":"https:\/\/blogs.visigo.com\/chriscoulson\/filter-a-sharepoint-calendar-list-with-a-date-picker\/","title":{"rendered":"Filter a SharePoint Calendar List With a Date Picker"},"content":{"rendered":"<p>I was trying to add a date picker to allow the filtering of a Calendar list in SharePoint. Unfortunately I had SharePoint Foundation, which is missing the Date Filter web part which only comes with the Enterprise edition of SharePoint.<\/p>\n<p>I found <a href=\"http:\/\/kduenke.blogspot.ca\/2011\/04\/poor-mans-date-filter-web-part.html\">this<\/a> article, which works great for filtering a specific date. \u00a0My calendar items have a StartTime and EndTime though, and could take place over several days. \u00a0Since the filter connection only allows the filtering based on a single field, this wasn&#8217;t going to work.<\/p>\n<p>I ended up making the changes in SharePoint Designer, which allows for parameters and multiple filters based on those parameters. \u00a0Unfortunately it wasn&#8217;t as easy as adding a DateTimeControl to the view, as the DateTimeControl outputs dates in a format that&#8217;s incompatible with the list filters. \u00a0I ended up writing some Javascript to do the date conversion as well as populate the DateTimeControl with a default date of today. \u00a0Here&#8217;s how to do it:<\/p>\n<ul>\n<li>Open <strong>SharePoint Designer<br \/>\n<\/strong><\/li>\n<li>Select <strong>Lists and Libraries<\/strong><\/li>\n<li>Select the calendar you would like to add the filtered list to.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-116\" title=\"1SelectLibrary\" src=\"https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/1SelectLibrary-500x267.png\" alt=\"\" width=\"500\" height=\"267\" srcset=\"https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/1SelectLibrary-500x267.png 500w, https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/1SelectLibrary-300x160.png 300w, https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/1SelectLibrary.png 816w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/li>\n<li>Add a new view to your list.<br \/>\n<a href=\"https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/2AddView.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-117\" title=\"2AddView\" src=\"https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/2AddView-500x241.png\" alt=\"\" width=\"500\" height=\"241\" srcset=\"https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/2AddView-500x241.png 500w, https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/2AddView-300x144.png 300w, https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/2AddView.png 560w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/li>\n<li>Click the view to edit it.<\/li>\n<li>Put your cursor just above the list and select:<br \/>\n<em>Insert -&gt; SharePoint -&gt; DateTimeControl<br \/>\n<a href=\"https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/3AddDateTimeControl.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-118\" title=\"3AddDateTimeControl\" src=\"https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/3AddDateTimeControl-500x305.png\" alt=\"\" width=\"500\" height=\"305\" srcset=\"https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/3AddDateTimeControl-500x305.png 500w, https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/3AddDateTimeControl-300x183.png 300w, https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/3AddDateTimeControl.png 1201w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a>\u00a0<\/em><\/li>\n<li>Modify the properties of the DateTimeControl. Set:<br \/>\n<em>AutoPostBack = True<\/em><br \/>\n<em>DateOnly = True<\/em><\/li>\n<li>Now we have to add in some javascript to convert the output of the DateTimeControl to a format that the SharePoint filters will recognize (YYYY-MM-DD). Go to the Code view and between the &lt;\/div&gt; and &lt;\/content&gt; of the DateTimeControl add the following:<\/li>\n<\/ul>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;input type=&quot;hidden&quot; name=&quot;spFilterDate&quot; id=&quot;spFilterDate&quot;\/&gt;\r\n\r\n &lt;script type=&quot;text\/javascript&quot;&gt;\r\n function formatDateSharePoint(date)\r\n {\r\n var d = date.getDate();\r\n var m = date.getMonth()+1;\r\n var y = date.getFullYear();\r\n return '' + y +'-'+ (m&lt;=9?'0'+m:m) +'-'+ (d&lt;=9?'0'+d:d);\r\n }\r\n\r\nfunction formatDateSharePointView(date)\r\n {\r\n var d = date.getDate();\r\n var m = date.getMonth()+1;\r\n var y = date.getFullYear();\r\n return '' + (m&lt;=9?'0'+m:m) +'\/'+ (d&lt;=9?'0'+d:d) + '\/' + y;\r\n }\r\n\r\nfunction datePickerChange()\r\n {\r\n var datePicker = document.getElementById(g_strDateTimeControlIDs&#x5B;&quot;DateTimeControl1&quot;]);\r\n var date = new Date(datePicker.value);\r\n var dateField = document.getElementById(&quot;spFilterDate&quot;);\r\n dateField.value = formatDateSharePoint(date);\r\n }\r\n\r\nfunction datePickerDefault()\r\n {\r\n var datePicker = document.getElementById(g_strDateTimeControlIDs&#x5B;&quot;DateTimeControl1&quot;]);\r\n var date = new Date(datePicker.value);\r\n if (isNaN( date.getTime() ))\r\n {\r\n date = new Date();\r\n datePicker.value = formatDateSharePointView(date);\r\n __doPostBack(g_strDateTimeControlIDs&#x5B;&quot;DateTimeControl1&quot;],&quot;&quot;);\r\n }\r\n\r\n}\r\n\r\n\/\/Call the datePickerChange on Postback, as the client side DatePickerControl events only work on IE.\r\n \/\/The only reliable way to call the function on postback seems to be to override the __doPostBack function\r\n var __originalPostBack= __doPostBack;\r\n\r\nfunction beforePostBack(eventTarget, eventArgument) {\r\n datePickerChange();\r\n __originalPostBack(eventTarget, eventArgument);\r\n }\r\n\r\n__doPostBack = beforePostBack;\r\n\r\n\/\/Default the date picker to Today, if it's not already set\r\n datePickerDefault();\r\n\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>So now the code for the DateTime Control should look like:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;content&gt;\r\n &lt;div&gt;\r\n &lt;SharePoint:DateTimeControl runat=&quot;server&quot; id=&quot;DateTimeControl1&quot; AutoPostBack=&quot;True&quot; DateOnly=&quot;True&quot; \/&gt;\r\n &lt;\/div&gt;\r\n\r\n &lt;input type=&quot;hidden&quot; name=&quot;spFilterDate&quot; id=&quot;spFilterDate&quot;\/&gt;\r\n\r\n &lt;script type=&quot;text\/javascript&quot;&gt;\r\n function formatDateSharePoint(date)\r\n {\r\n var d = date.getDate();\r\n var m = date.getMonth()+1;\r\n var y = date.getFullYear();\r\n return '' + y +'-'+ (m&lt;=9?'0'+m:m) +'-'+ (d&lt;=9?'0'+d:d);\r\n }\r\n\r\n...\r\n\r\n__doPostBack = beforePostBack;\r\n\r\n\/\/Default the date picker to Today, if it's not already set\r\n datePickerDefault();\r\n\r\n&lt;\/script&gt;\r\n\r\n&lt;\/content&gt;\r\n<\/pre>\n<p>So what we&#8217;ve done is made it so that every time the DateTimeControl is changed, a hidden field is updated with the date in a format that&#8217;s valid for the list filter. \u00a0Now we need to read that value into a parameter and filter the list based on that parameter:<\/p>\n<ul>\n<li>Go back to design view, select and list and choose <em>Options -&gt; Parameters<\/em>.<\/li>\n<li>Create a new parameter &#8211; we&#8217;ll call it <strong>DateFilter<\/strong>. Set the Source to &#8216;<strong>Form<\/strong>&#8216; and the Form Field to &#8216;<strong>spFilterDate<\/strong>&#8216; which is the name of our hidden field.<br \/>\n<a href=\"https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/5AddParameter.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-119\" title=\"5AddParameter\" src=\"https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/5AddParameter-500x337.png\" alt=\"\" width=\"500\" height=\"337\" srcset=\"https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/5AddParameter-500x337.png 500w, https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/5AddParameter-300x202.png 300w, https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/5AddParameter.png 512w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/li>\n<li>Now click on Options -&gt; Filter and create two filters:<br \/>\n<em>StartTime Less Than or Equal To DateFilter And<\/em><br \/>\n<em> EndTime Greater Than or Equal To DateFilter<br \/>\n<a href=\"https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/6AddFilters.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-120\" title=\"6AddFilters\" src=\"https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/6AddFilters.png\" alt=\"\" width=\"448\" height=\"271\" srcset=\"https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/6AddFilters.png 448w, https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/6AddFilters-300x181.png 300w\" sizes=\"auto, (max-width: 448px) 100vw, 448px\" \/><\/a><\/em><\/li>\n<li>Save the view.<\/li>\n<\/ul>\n<p>You can now use this view in SharePoint to filter the calendar to items that occur on a specific date.<\/p>\n<p><a href=\"https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/7ViewList.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-121\" title=\"7ViewList\" src=\"https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/7ViewList-500x387.png\" alt=\"\" width=\"500\" height=\"387\" srcset=\"https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/7ViewList-500x387.png 500w, https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/7ViewList-300x232.png 300w, https:\/\/blogs.visigo.com\/chriscoulson\/wp-content\/uploads\/2012\/11\/7ViewList.png 766w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was trying to add a date picker to allow the filtering of a Calendar list in SharePoint. Unfortunately I had SharePoint Foundation, which is missing the Date Filter web part which only comes with the Enterprise edition of SharePoint. I found this article, which works great for filtering a specific date. \u00a0My calendar items [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,262],"tags":[259,255,256,258,264,260,86,148,261,22,24,322,257,263,39],"class_list":["post-115","post","type-post","status-publish","format-standard","hentry","category-sharepoint","category-sharepoint-designer","tag-calendar","tag-date-filter","tag-date-range","tag-datetimecontrol","tag-endtime","tag-filter","tag-javascript","tag-list","tag-parameters","tag-sharepoint-2","tag-sharepoint-2010","tag-sharepoint-designer","tag-sharepoint-foundation","tag-starttime","tag-web-part"],"_links":{"self":[{"href":"https:\/\/blogs.visigo.com\/chriscoulson\/wp-json\/wp\/v2\/posts\/115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.visigo.com\/chriscoulson\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.visigo.com\/chriscoulson\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.visigo.com\/chriscoulson\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.visigo.com\/chriscoulson\/wp-json\/wp\/v2\/comments?post=115"}],"version-history":[{"count":1,"href":"https:\/\/blogs.visigo.com\/chriscoulson\/wp-json\/wp\/v2\/posts\/115\/revisions"}],"predecessor-version":[{"id":122,"href":"https:\/\/blogs.visigo.com\/chriscoulson\/wp-json\/wp\/v2\/posts\/115\/revisions\/122"}],"wp:attachment":[{"href":"https:\/\/blogs.visigo.com\/chriscoulson\/wp-json\/wp\/v2\/media?parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.visigo.com\/chriscoulson\/wp-json\/wp\/v2\/categories?post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.visigo.com\/chriscoulson\/wp-json\/wp\/v2\/tags?post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}