Trick : SharePoint Group by View with Order by on DateTime Field

Trick : SharePoint Group by View with Order by on DateTime Field

Hello Folks,

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?

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.


Hope it helps!!!!

Best Regards,
Joydeep K.

Leave a Comment
  • Please add 2 and 1 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Richard Mueller edited Revision 1. Comment: Changed tag "Has Images" to "Has Image", added tag "Has Code", removed space in tag "SharePoint  2010", added language tag

  • Maheshkumar S Tiwari edited Original. Comment: Added Tag

Page 1 of 1 (2 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Richard Mueller edited Revision 1. Comment: Changed tag "Has Images" to "Has Image", added tag "Has Code", removed space in tag "SharePoint  2010", added language tag

  • Maheshkumar S Tiwari edited Original. Comment: Added Tag

Page 1 of 1 (2 items)