Thursday, August 14, 2008

How sharepoint determines if a field is "groupable"

Here is one for anyone who ever wondered why a certain field is not showing up in the "Group By" selection box.

You know how you can define in the field definition if the field is "sortable" and "filterable"? Well, what about "groupable"? its not an option! But still, some fields don't show up in the dropdown for grouping in the view settings dialog. How is that set?
The answer is in the aspx file ViewNew.aspx (and ViewEdit.aspx) - these files have server side scripts that has a function "IsGroupable". This function checks if the field should be displayed in the list of fields that you can group by.
Here are the rules:

  1. If the fiels is not "sortable", then it is not groupable
  2. If the field is hidden, then it is not groupable
  3. If the field is set not to display in viewforms, then it is not groupable
  4. If the field is a lookup field and is set to display related items, then it is not groupable
  5. If the field is the built-in file name field, then it is not groupable.
From the list above you can see that it is pretty logical...except #3! Heck! A lot of times you dont want to display the field in views, but you still want to group by it!

So, here you go. If you were puzzled why your field was not showing up - now you know.

Here is the code

protected bool IsGroupable(SPField field)
 if (!field.Sortable ||
  !ShowField(field) ||
  ((field as SPFieldLookup) != null && (field as SPFieldLookup).CountRelated))
  return false;
 if (iBaseType == SPBaseType.DocumentLibrary
  && (field.InternalName == "FileLeafRef"
      || field.InternalName == "LinkFilenameNoMenu"
      || field.InternalName == "LinkFilename"
      || field.InternalName == "NameOrTitle"))
  return false;
 return true;

No comments: