SharePoint 2010: Group by View with Order by on DateTime Field
Introduction
This article is regarding creating “SharePoint Group by View with Order by on DateTime Field” for list or library in SharePoint 2010. First, lets us have a look on what is the need to create this kind of view?
Walkthrough
To create Group by view, SharePoint provides out of box settings in Group by node in view setting where column to group by can be easily defined. With Group by view SharePoint shows its own behavior and it will do order by on the same column on which we have enabled Group by. Below is the simplest example to understand this.
Suppose we have a list named as 'Ask Us' with columns ‘Question’, ‘Answer’, ‘Created’, ‘Modified’. Now we want to show a list of questions in expandable-collapsible manner with ascending order on modified date. To achieve that i have selected a ‘Question’ column in group by setting and a ‘Modified’ Column in sort order settings so its output will look like below.
This is incorrect as per our requirement because we want to do order by on ‘Modified’ column not on ‘Question’ column.
In this article, we will see most easiest way to achieve our requirement step by step.
- Create one more column of type calculated column named as ‘Ques’ in same list. You can specify yours name.
- Set below formula in Additional Column Settings. It will format 'Modified' date field and append it to our 'Question' field.
=TEXT(Modified,"yyyy")&TEXT(Modified,"mm")&TEXT(Modified,"dd")&TEXT(Modified,"hh:mm:ss")&Question - Click on Modify View button from top ribbon. You will be redirected to the edit page of view setting.
- Select newly created ‘Ques’ column in Group By node then I have selected Show groups in ascending order. You can select it based on your need and click on OK button to save your changes.
- We will see result something like below. We can take it as a perfect presentation of what we are looking for. But the problem is its showing extra formatted date in front of each question.
- Now we need to deal with that extra text formatted date. Here JQuery comes to show its magic.
- Add Content Editor Web Part on your page and add below JS code block in it. It will remove formatted date from the front of each question.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("tr[id='group0']").each(function(){ var p = $(this); p.html( p.html().replace( /\d{10}:\d{2}:\d{2}/, ' ' ) ) }); }); </script>
- Above trick gives us very grateful result.Finally our output looks like below exactly the same that we are looking for.