Restoring Group By Content Type to SharePoint 2010 List Views

SharePoint 2007 had a commonly used feature that enabled users to create views on lists that grouped by Content Type.

For some reason, this feature was removed from the user interface in SharePoint 2010.

Solution 1: The Easy Method

If you wish to do this today, you can do this using SharePoint Designer to create a view and then change the Xsl to specify a different field name to group by (e.g. “ContentType”).

Overriding the Field Used for Grouping

Solution 2: The Better Method

Alternative, we could try and get our options added to the ViewEdit.aspx page. Options aren’t great for this as it’s a _layouts (application) page, and therefore we can’t just edit it in the browser or SharePoint Designer.

You could add a piece of JavaScript to do this though. Plan a) would be to add this to the bottom of you v4.master, and customise this for the whole site/site collection.

Plan b) would be to create something like a sandbox solution that deploys a “scriptlink” element, placing the script on every page that gets rendered.

I’ve create a CodePlex project for plan b). Here’s some of the code. Firstly, here’s the JavaScript I’d like to run on every page. It simply creates a new <option> tag in HTML and adds it to the drop down list if it exists on the page. Let’s call it “ListViewEdit.js”.

_spBodyOnLoadFunctionNames.push("jbCTFix");

function jbCTFix() {
    jbCTKludge('idGroupField1');
    jbCTKludge('idGroupField2');
}
function jbCTKludge(selName) {
    var sel = document.getElementById(selName);
    if (sel) {
        if (sel.selectedIndex >= 0) {

            var o = document.createElement('option');
            o.text = 'Content Type';
            o.value = 'tp_ContentType';

            var prev = sel.options[sel.selectedIndex];
            try {
                sel.add(o, prev);
            }
            catch (ex) {
                sel.add(o, sel.selectedIndex);
            }
        }
    }
}

Next, here’s the element manifest to apply this on each page in the site collection.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="Ribbon.Library.Actions.Scripts" 
              Location ="ScriptLink"
              ScriptSrc="~site/ListViewEdit/ListViewEdit.js" />
  <Module Name="ListViewEdit">
    <File Path="ListViewEdit\ListViewEdit.js" Url="ListViewEdit/ListViewEdit.js" />
  </Module>
</Elements>

You can download the full project and source code for the SharePoint 2010 ViewEdit Group by Content Type project from the CodePlex project here: sp10ctgrouping.codeplex.com

Comments

  1. Mark says

    How do you deploy this solution on sharepoint online? I added and activated the solution but the options for group by content type does not show….
    Thanks,
    Mark

    • says

      Hi Mark!

      I tried this on Office 365′s SharePoint Online just now. It all seems to work. Did you activate the “joelblogs.co.uk List View Group by Content Type” site feature?

      Hope this helps!

      joel

  2. Furiant says

    I’ve added, installed, and activated the feature and still see no “Content Type” option in the Group By select box. This is a list that uses multiple content types. Not sure what I could be doing wrong, but it’s simply not working.

    • Furiant says

      Okay, I found it. The issue is that when you click the select box, it defaults to the “None” option, which is at the first index in the otherwise alphabetically-sorted list. Your script injects the “Content Type” option above that option (not in alphabetical order), and ends up being hidden. You might want to mention this quirk on this blog and your CodePlex site, as it is very easy to miss.

      • says

        Hi Furiant,

        Many thanks for your comments. Hope you find my code to be of use.

        Please note that the second screenshot on the CodePlex page (http://sp10ctgrouping.codeplex.com/) shows this. Thank you for flagging this up. I have now added an extra item to the Known Issues list on the page in an attempt to make that clearer.

        Kind regards,

        Joel

  3. Jerry says

    Such a great article! I go with two approaches and they all fine :)

    However, there is an issue still remain, the content types cannot be sorted. Seem that they are sorted by “ContentTypeID” instead of “ContentType”. Anyone experience on this, please advice me

    Thanks

  4. Rene says

    Hi Joel,

    Great component that solves a common issue with SP. Question — have you tried using this with SP 2013? I was able to compile and deploy the solution with no issues (after recompiling for SP 2013 with MS VS 2012, but when I go to create a new view the “Content Type” option does not appear in the list. I’ve tried “scrolling above the None” entry, but there is nothing listed above “None”.

    Is there some other change in SP 2013 that you are aware of which might cause this not to work?

    Thanks

    rene

    • Rene says

      Actually — nevermind. Got it working. Turns out that (for some reason), when I access the Create View page, I have to press F5 to refresh the page for the Content Type value to appear. When you first load the page it does not show up, but once refreshed it does.

  5. Ronald Laan says

    Hi Joel,

    Do you know why grouping on content type is removed by Microsoft? There must be some explonation. I have tried implementing this as well and since I have done that I experience problems creating and modifying views in the same library..

  6. Naychi says

    Hi Joel

    I was uploaded your wsp to my sharepoint site Galleries -> solutions.
    But the Activate button is still disable(gray color). I am not able to active.
    How can I resolved ?

    Regards
    Naychi

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>