none
Sorting and grouping emails by user defined fields - Inbox RRS feed

  • Question

  • Hi,

    I have an COM shared Add-in developed in C# which shows user defined fields as columns in the Inbox.
    Now when I click on the header of the column its sorts the emails and the emails are not grouped.(same behavior is noticed when user defined columns are added manually by the View Settings dialog)
    But when I click on already available column headers(non user defined columns) such as Subject, From, Received, etc.. the emails gets grouped and ordered.
    My requirement is to group and order the emails when clicked on user defined column headers as non user defined columns.

    I am using MS Outlook 2010.

    Also I noticed,

    • MS Outlook groups the emails rather than sorting, when clicked on non user defined text column headers such as Subject, From, etc.... This could be viewed from the View Settings dialog by right clicking on the column header.
    • When the order and/or group is set from the View Settings dialog for the user defined columns it behaves as non user defined columns without any issues.

    An alternative solution would be, to programmatically set the grouping when clicked on the user defined text columns as MS Outlook does. Therefore how can I capture the Click event of the column header? Any code sample or any other suggestion would be appreciated. Please advice.


    • Edited by Damian Muller Friday, September 5, 2014 5:08 AM Formatting of text
    Friday, September 5, 2014 5:07 AM

Answers

  • Hello Eugene,

    Thanks for the information.
    Even after defining the user properties as above it did not sort as per non user defined columns.

    After trying out for some time figured out the following can be done as an alternative and may be helpful for someone.
    The following is called from Microsoft.Office.Interop.Outlook.Explorer.SelectionChange event.

    // Microsoft.Office.Interop.Outlook.TableView tblView
    
    Microsoft.Office.Interop.Outlook.OrderFields thisSort = tblView.SortFields;
    Microsoft.Office.Interop.Outlook.OrderFields thisGroup = tblView.GroupByFields;
    
    if (thisSort.Count == 1)
    {
    	if (tblView.AutomaticGrouping == true)
            {
            	if (thisGroup.Count == 0)
                    {
                    	string propName = thisSort[1].ViewXMLSchemaName;
                            int PropnameSeparator = propName.LastIndexOf("/");
    
    			if (PropnameSeparator >= 0)
    			{
    				string OnlyPropName = propName.Substring(PropnameSeparator + 1);
                                    Microsoft.Office.Interop.Outlook.UserDefinedProperty SortProperty = currentFolder.UserDefinedProperties[OnlyPropName];
                                    if (SortProperty != null)
                                    {
                                    	if (SortProperty.Type == Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText)
                                            {
                                            	thisGroup.Add(propName, thisSort[1].IsDescending);
                                                    thisSort.Remove(1);
                                                    tblView.Apply();
                                                    tblView.Save();
                                             }
                                     }
                             }
                     }
              }
    }

    Tuesday, September 9, 2014 12:08 PM

All replies

  • Hello Damian,

    The Outlook object model doesn't provide any events for this.

    The Sorting Fields in a View page in MSDN states the following:

    Outlook items can be sorted by adding one or more Outlook item properties to the OrderFields collection. The OrderFields collection for those views can be retrieved by calling the SortFields property of the appropriate view object. The Add method of the OrderFields collection is used to create an OrderField object that represents the Outlook item property to be sorted.

    You can add either built-in or custom Outlook item properties to the OrderFields collection. The order in which the properties are included in the OrderFields collection determines the order in which the properties are sorted, while the IsDescending property of the OrderField object which represents an Outlook item property determines whether the values of that property are sorted in ascending or descending order.

    The following guidelines should be used when specifying custom properties:

    • The custom property must be available in the UserDefinedProperties collection for the parent Folder object.

    • Custom properties should be specified by property name (for example, "[Shoe Size]").

    • Custom property names are not case-sensitive, can include spaces, and should be enclosed in square brackets ([]) if they contain spaces.

    Also you may find the Filtering and Specifying Fields in a View article in MSDN helpful.

    Friday, September 5, 2014 8:55 AM
  • Hello Eugene,

    Thanks for the information.
    Even after defining the user properties as above it did not sort as per non user defined columns.

    After trying out for some time figured out the following can be done as an alternative and may be helpful for someone.
    The following is called from Microsoft.Office.Interop.Outlook.Explorer.SelectionChange event.

    // Microsoft.Office.Interop.Outlook.TableView tblView
    
    Microsoft.Office.Interop.Outlook.OrderFields thisSort = tblView.SortFields;
    Microsoft.Office.Interop.Outlook.OrderFields thisGroup = tblView.GroupByFields;
    
    if (thisSort.Count == 1)
    {
    	if (tblView.AutomaticGrouping == true)
            {
            	if (thisGroup.Count == 0)
                    {
                    	string propName = thisSort[1].ViewXMLSchemaName;
                            int PropnameSeparator = propName.LastIndexOf("/");
    
    			if (PropnameSeparator >= 0)
    			{
    				string OnlyPropName = propName.Substring(PropnameSeparator + 1);
                                    Microsoft.Office.Interop.Outlook.UserDefinedProperty SortProperty = currentFolder.UserDefinedProperties[OnlyPropName];
                                    if (SortProperty != null)
                                    {
                                    	if (SortProperty.Type == Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText)
                                            {
                                            	thisGroup.Add(propName, thisSort[1].IsDescending);
                                                    thisSort.Remove(1);
                                                    tblView.Apply();
                                                    tblView.Save();
                                             }
                                     }
                             }
                     }
              }
    }

    Tuesday, September 9, 2014 12:08 PM
  • Damian,

    Thank you for sharing the solution for other forum readers.

    P.S. Don't forget to release underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more about this in the Systematically Releasing Objects article.

    Tuesday, September 9, 2014 12:33 PM
  • Thanks.

    Wednesday, September 10, 2014 3:36 AM