Adding Custom Columns to Outlook Explorer

Add-in Express™ Support Service
That's what is more important than anything else

Adding Custom Columns to Outlook Explorer
 
Leon H H




Posts: 17
Joined: 2010-04-06
I followed these articles to add a custom column to Outlook Explorer, but no custom column gets displayed.
I run Outlook 365 Desktop Edition, and user the latest Add-In Express version on Win11.

[CODE] private void AddinModule_AddinStartupComplete(object sender, EventArgs e)
{
List<Outlook.MailItem> mailList = GetMailList();
}

private void AddinModule_AddinBeginShutdown(object sender, EventArgs e)
{

}

private void PJRibbonBtn_OnClick(object sender, IRibbonControl control, bool pressed)
{

}

private List<Outlook.MailItem> GetMailList()
{
List<Outlook.MailItem> mailItemsList = null;
Outlook.Items folderItems = null;
Outlook.NameSpace ns = null;
Outlook.MAPIFolder folderInbox = null;
object itemObj = null;

try
{
mailItemsList = new List<Outlook.MailItem>();
ns = OutlookApp.GetNamespace("MAPI");

folderInbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
folderItems = folderInbox.Items;
for (int i = 1; i <= folderItems.Count; i++)
{
itemObj = folderItems[i];
if (itemObj is Outlook.MailItem)
AddUserProperties(itemObj as Outlook.MailItem);

if (itemObj != null)
Marshal.ReleaseComObject(itemObj);
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
finally
{
if (folderItems != null)
Marshal.ReleaseComObject(folderItems);
if (folderInbox != null)
Marshal.ReleaseComObject(folderInbox);
if (ns != null)
Marshal.ReleaseComObject(ns);
}

return mailItemsList;
}

private void AddUserProperties(Outlook.MailItem mailItem)
{
Outlook.UserProperties mailUserProperties = null;
Outlook.UserProperty mailUserProperty = null;
try
{
mailUserProperties = mailItem.UserProperties;
mailUserProperty = mailUserProperties.Add("CustomField", Outlook.OlUserPropertyType.olText, true, Outlook.OlFormatText.olFormatTextText); // mailUserProperties[1]

mailUserProperty.Value = "CustomValue";
mailItem.Save();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
finally
{
if (mailUserProperty != null) Marshal.ReleaseComObject(mailUserProperty);
if (mailUserProperties != null) Marshal.ReleaseComObject(mailUserProperties);
}
}

I also update the function with the following, based on the article:
https://www.add-in-express.com/forum/read.php?FID=5&TID=11356

but it did not help:

private List<Outlook.MailItem> GetMailList()
{
List<Outlook.MailItem> mailItemsList = null;
Outlook.Items folderItems = null;
Outlook.NameSpace ns = null;
Outlook.MAPIFolder folderInbox = null;
object itemObj = null;
Outlook.TableView tableView = null;
Outlook.ViewField viewField = null;
Outlook.ColumnFormat columnFormat = null;

try
{
mailItemsList = new List<Outlook.MailItem>();
ns = OutlookApp.GetNamespace("MAPI");

folderInbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
if (folderInbox != null && folderInbox.CurrentView.ViewType == Outlook.OlViewType.olTableView)
{
folderItems = folderInbox.Items;
for (int i = 1; i <= folderItems.Count; i++)
{
itemObj = folderItems[i];
if (itemObj is Outlook.MailItem)
AddUserProperties(itemObj as Outlook.MailItem);

if (itemObj != null)
Marshal.ReleaseComObject(itemObj);
}

tableView = folderInbox.CurrentView as Outlook.TableView;
viewField = tableView.ViewFields.Add("CustomField");
columnFormat = viewField.ColumnFormat;
columnFormat.Align = Outlook.OlAlign.olAlignLeft;
columnFormat.Width = 20;
tableView.Save();
tableView.Apply();
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
finally
{
if (tableView != null)
Marshal.ReleaseComObject(tableView);
if (viewField != null)
Marshal.ReleaseComObject(viewField);
if (columnFormat != null)
Marshal.ReleaseComObject(columnFormat);
if (folderItems != null)
Marshal.ReleaseComObject(folderItems);
if (folderInbox != null)
Marshal.ReleaseComObject(folderInbox);
if (ns != null)
Marshal.ReleaseComObject(ns);
}

return mailItemsList;
}
Posted 08 Aug, 2022 19:30:45 Top
Andrei Smolin


Add-in Express team


Posts: 18338
Joined: 2006-05-11
Hello Leon,

Below is a raw sketch.

folderInbox.CurrentView as Outlook.TableView in the code above returns null if the current view is not a TableView and folderInbox.CurrentView is left unreleased in this case.


public static void AddField(Outlook.MAPIFolder folder, Outlook.TableView tableView, 
    string propertyName, Outlook.OlUserPropertyType propertyType) {
    
    Outlook.UserDefinedProperties userDefinedProperties = folder.UserDefinedProperties;
    Outlook.UserDefinedProperty userDefinedProperty = null;
    try {
        userDefinedProperty = userDefinedProperties[propertyName];
    } catch { }
    if (userDefinedProperty == null)
        userDefinedProperty = userDefinedProperties.Add(propertyName, propertyType);

    if (userDefinedProperty != null) Marshal.ReleaseComObject(userDefinedProperty); userDefinedProperty = null;
    if (userDefinedProperties != null) Marshal.ReleaseComObject(userDefinedProperties); userDefinedProperties= null;    

    Outlook.ViewFields viewFields = tableView.ViewFields;
    Outlook.ViewField viewField = viewFields.Add(propertyName);
    tableView.Save();
    if (viewField != null) Marshal.ReleaseComObject(viewField); viewField = null;
    if (viewFields != null) Marshal.ReleaseComObject(viewFields); viewFields = null;
}


Regards from Poland (CEST),

Andrei Smolin
Add-in Express Team Leader
Posted 09 Aug, 2022 03:27:45 Top
Leon H H




Posts: 17
Joined: 2010-04-06
I implemented your code with some minor changes, but still no custom column is displayed in Explorer.
I am going to send you this project to your support email. There are some other implementations in this project but only a single function is executed (based on your code above).
Posted 09 Aug, 2022 11:46:01 Top
Andrei Smolin


Add-in Express team


Posts: 18338
Joined: 2006-05-11
Try to save the view after that function is called.

Regards from Poland (CEST),

Andrei Smolin
Add-in Express Team Leader
Posted 09 Aug, 2022 11:52:24 Top
Leon H H




Posts: 17
Joined: 2010-04-06
As you can see from the code the last line of that function is tableView.Save()
Posted 09 Aug, 2022 12:06:10 Top
Leon H H




Posts: 17
Joined: 2010-04-06
Did you see my project that I emailed you?
Posted 09 Aug, 2022 14:15:33 Top
Leon H H




Posts: 17
Joined: 2010-04-06
I experimented and managed to add column to Explorer.
My question now is it possible to edit a cell content inline in this column.
Posted 09 Aug, 2022 16:33:53 Top
Andrei Smolin


Add-in Express team


Posts: 18338
Joined: 2006-05-11
Leon H H writes:
I experimented and managed to add column to Explorer.


What did you change in your project?

Leon H H writes:
My question now is it possible to edit a cell content inline in this column.


It is possible to allow inline editing for all cells in the view. First off, get View.XML for your view and paste it somewhere. Then enable in-cell editing following the instructions in section 'In an Outlook View' at https://www.slipstick.com/outlook/email/change-the-subject-of-an-incoming-message/. Then get View.XML once again and compare it with the unmodified XML. This shows you the option you need to set in the XML. When you setup the view, set also its XML to enable the in-cell edition option.

Regards from Poland (CEST),

Andrei Smolin
Add-in Express Team Leader
Posted 10 Aug, 2022 06:11:04 Top
Leon H H




Posts: 17
Joined: 2010-04-06
This link in your email leads to some non-coding article, and I did not find any connection to my question.
Can you please point me how to work with View.XML?
Posted 10 Aug, 2022 13:38:52 Top
Andrei Smolin


Add-in Express team


Posts: 18338
Joined: 2006-05-11
To enable the in-cell editing in code, you need to research the structure of the XML that the View.XML property returns (and sets). To do this, compare View.XML when the in-cell editing is enabled and disabled: that option can be set in the Outlook UI and this is exactly what you see in section 'In an Outlook View' at https://www.slipstick.com/outlook/email/change-the-subject-of-an-incoming-message/.

So, get View.XML, enable in-cell editing following instructions on that page, get View.XML, find the difference between the XMLs, and implement modifying View.XML in that fashion. Make sure you save the view after modifying it.

Regards from Poland (CEST),

Andrei Smolin
Add-in Express Team Leader
Posted 10 Aug, 2022 13:54:05 Top