10/27/2015

SharePoint: Sort and Filter on One Column While Displaying Another

This article includes both 2010 and 2013 examples.

Let's say you wanted to display a Date/Time down to the minute or second, but you wanted the column heading to filter by days or months. By default the column filter dropdown displays one each of what ever it finds in the column, or in the case of dates, the nearest day.

image

But if you want just the months or years in the dropdown, then maybe something like this:

image        image

 

It's not to hard to do in SharePoint 2010, and fairly easy in SharePoint 2013

In SharePoint 2010 we still have a fully working SharePoint Designer that can easily edit web parts. In SharePoint 2013, Designer is pretty much broken for web part work, which is just as well as we should be looking at the newest tools in 2013 for our customizations. In this example we will be using JS Link for 2013.

 

Steps for SharePoint 2010

The basic steps:

  • Create a new list named "Event Announcements".
  • Add a Date and Time column named "EventDate".
  • Add a Calculated column named "Event Date" to display a YYYY-MM version of the date that can be used to filter by month.
  • Use SharePoint Designer to display the full date in the calculated column by hand customizing the XSLT.

Note: Although the names of my columns differ only by a space, you can use any column names you like. I will use EventDate for data entry and Event Date for display.

Steps:

  1. Go to your site and create a test list. For this example an Announcements list would be a good choice.
  2. Create a new column named "EventDate". (You will enter your date data here.) 
    1. In the List ribbon click Create Column.
    2. Enter "EventDate" as the name.
    3. Set the type to Date and Time.
    4. Set Date and Time Format to Date & Time.
    5. Click OK.
  3. Create a new column named "Event Date". (This column will control the filter grouping.)
    1. In the List ribbon click Create Column.
    2. Enter "Event Date" as the name.
    3. Set the type to Calculated.
    4. Enter this formula:
         =TEXT([EventDate],"yyyy-mm")
      or for years instead of months:
         =TEXT([EventDate],"yyyy")
    5. Set The data type returned from this formula to Single line of text.
    6. Click OK.
  4. Add some sample data with dates spread across two or three different months.
     
  5. Open SharePoint Designer 2010 and open your site.
  6. Click Lists and Libraries and click your list.
  7. Find and click your view (I used All Items).
  8. If the screen is not in Split view click Split at the bottom of the screen.
  9. In the Code view pane click in the web part code. (The code without the yellow background.)
  10. In the List View Tools / Design ribbon click Customize XSLT and Customize Entire View.
    image
  11. In the design view click on any one of the dates in the EventDate column.
       image
  12. Verify the field name by looking up a few lines for FieldRef_ValueOf. My field is EventDate.
       image
  13. In the design view click on any one of the dates in the Event Date column. (the calculated column)  This will highlight a line in the Code view.
  14. Edit the "value-of" element to change from this:
      <xsl:value-of select="$thisNode/@*[name()=current()/@Name]"/>
    to this:
      <xsl:value-of select="$thisNode/@*[name()='EventDate']"/>
    Note that you are replacing the expression "current()/@Name" to the name of the field wrapped in single quotes. I.e. 'EventDate'. EventDate is column from which you want to copy the data.
  15. Save the file.
  16. Return to the browser and test the view. You can edit the view and remove the EventDate column as we only need the Event Date column.
       image

 

Steps for SharePoint 2013

The basic steps:

  • Create the test list and columns. (Same steps as for 2010)
  • Create the JS Link JavaScript file.
  • Upload or save the file to a SharePoint library.
  • Edit the view's web part and link to the JS File.

Steps:

  1. Go to your site and create a test list. For this example an Announcements list would be a good choice.
  2. Create a new column named "EventDate". (You will enter your date data here.) 
    1. In the List ribbon click Create Column.
    2. Enter "EventDate" as the name.
    3. Set the type to Date and Time.
    4. Set Date and Time Format to Date & Time.
    5. Click OK.
  3. Create a new column named "Event Date". (This column will control the filter grouping.)
    1. In the List ribbon click Create Column.
    2. Enter "Event Date" as the name.
    3. Set the type to Calculated.
    4. Enter this formula:
         =TEXT([EventDate],"yyyy-mm")
      or for years instead of months:
         =TEXT([EventDate],"yyyy")
    5. Set The data type returned from this formula to Single line of text.
    6. Click OK.
  4. Add some sample data with dates spread across two or three different months.
     
  5. Open SharePoint Designer 2013 and your site.
  6. In the Navigation area click Site Assets. (or any other library)
  7. Right-click in any empty space in the file area and select New and HTML.
  8. Right-click the new file and rename it to GroupByMonthJSLink.js (any name will do)
  9. Click the new file and then click Edit File.
  10. Select all of the HTML and delete it.
  11. Enter the JavaScript listed below.
  12. Save the file.
  13. Go to the new list.
  14. Click Settings (gear) and Edit Page.
  15. In the list’s web part click the dropdown and click Edit Web Part.
  16. In the web part properties panel expand Miscellaneous.
  17. Enter the following path into the JS Link box:
    ~site/SiteAssets/GroupByMonthJSLink.js
    Note: "~site" points to current site/subsites URL while "~sitecollection" points to the top level site's URL.
  18. Click Apply. (You should see the change to the Event Date column.)
  19. Click OK
  20. In the Page ribbon click Stop Editing.
  21. Test! You can edit the view and remove the EventDate column as we only need the Event Date column.
       image

 

The JavaScript File

(function () { 

  // do all of the setup work...
  var TTNctx = {};
  TTNctx.Templates = {}; 

  // configure the Event_x0020_Date field to copy the EventDate values 
  // into the Event_x0020_Date column
  TTNctx.Templates.Fields = { 
    "Event_x0020_Date": 
{ "View": function (ctx) {return ctx.CurrentItem.EventDate} } }; // register the override SPClientTemplates.TemplateManager.RegisterTemplateOverrides(TTNctx); })(); // end of function

 

.

No comments:

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.