“Group by” on managed metadata column (Taxonomy) column and issues
As a part of regular development I was supposed to create a Managed MetaData columns (MMC) programmatically for the document libraries and programmatically set the document library default view so that all the items are “Group by” on one of the MMC column.
//code to create MMC column
TaxonomySession currentSession = new TaxonomySession(site);
var termStore = currentSession.TermStores["ManagedMetaDataService"];
var group = termStore.Groups["Industry"];
var termSet = group.TermSets["Engagement Phase"];
TaxonomyField field = new TaxonomyField(site.RootWeb.Fields, "TaxonomyFieldTypeMulti", column);
field.SspId = termStore.Id;
field.TermSetId = termSet.Id;
//Based on some configuration file I was setting fields “AllowMultipleValues” property
if (columnNode.Attributes["allowmultiplevalues"].Value.Equals("yes"))
field.AllowMultipleValues = true;
field.Group = MMSConstants.groupName;
rootWeb.Fields.Add(field); // Created a site column and later on programmatically added to content types and to document library
MMC column got added to one of the document library. Below code is do "Group By" on created column for the respective library
//Code to set the document library default view. I was doing “Group By” on created MMC column
SPView allDocumentsView = spList.Views["All Documents"];
SPViewFieldCollection allviewFields = allDocumentsView.ViewFields;
// Setting the “Group by” query
string viewQuery = "<GroupBy Collapse=\"TRUE\" GroupLimit=\"100\"><FieldRef Name=\"Engagement_x0020_Phase\" /></GroupBy>";
allDocumentsView.Query = viewQuery;
allDocumentsView.Scope = SPViewScope.Recursive;
allDocumentsView.Update();
After execution of this code, MMC column got added to document libraries successfully but then I saw strange issue with the “Group by” on created MMC column.
As I understand “Group By” on MMC column with “AllowMultipleValues = true;” will not work properly but I was not setting this property I went to column properties and verified. Allow multiple values checkbox is unchecked.
But still created MMC column was behaving as a column with “Allow multiple values” property turned on.
After some investigation I have realised that we need to explicilty set "Allow multiple values" to "false".
//modified code added else blog
//Based on some configuration file I was setting fields “AllowMultipleValues” property
if (columnNode.Attributes["allowmultiplevalues"].Value.Equals("yes"))
field.AllowMultipleValues = true;
else // added else block
field.AllowMultipleValues = false;
I was thinking that by default “AllowMultipleValues” is set to “false” but that was not the case. And at the end “Group By” worked out properly (see below).
Comments
- Anonymous
October 26, 2011
Thanks a ton it help me to solve my issue