Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

It is possible to get sorting on Managed metadata column? I get grouping and filtering on the columns working but not sorting.

Any help is appreciated

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

By default this will not work. So you have to extend content query webpart and use in CreateChildControls you have to add following two lines:

this.ProcessDataDelegate = new ProcessData(ProcessItems);
base.CreateChildControls();

next is to implement ProcessItems

protected DataTable ProcessItems(DataTable dt)
{
            //Code to add custom sorting logic goes here.
            DataView dv = new DataView(dt);
            dv.Sort = "BusinessUnit ASC, Title ASC";
            return CreateTable(dv);
        }

CreateTable(dv) is a function that convert DataView to DataTable.

public static DataTable CreateTable(DataView obDataView)
        {
            if (null == obDataView)
            {
                throw new ArgumentNullException
                ("DataView", "Invalid DataView object specified");
            }

            DataTable obNewDt = obDataView.Table.Clone();
            int idx = 0;
            string[] strColNames = new string[obNewDt.Columns.Count];
            foreach (DataColumn col in obNewDt.Columns)
            {
                strColNames[idx++] = col.ColumnName;
            }

            IEnumerator viewEnumerator = obDataView.GetEnumerator();
            while (viewEnumerator.MoveNext())
            {
                DataRowView drv = (DataRowView)viewEnumerator.Current;
                DataRow dr = obNewDt.NewRow();
                try
                {
                    foreach (string strName in strColNames)
                    {
                        dr[strName] = drv[strName];
                    }
                }
                catch (Exception ex)
                {
                    //Trace.WriteLine(ex.Message);
                }
                obNewDt.Rows.Add(dr);
            }

            return obNewDt;
        }

Well that's it, you are done.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.