Programmatically show User Property column in Outlook folder

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

Programmatically show User Property column in Outlook folder
How do I programmatically show a User Property column in an Outlook Folder? 
bhodso




Posts: 6
Joined: 2013-01-14
I have created a user defined column value and associated it to a mail object in Outlook using code similar to this:


var prop = mailItem.UserProperties.Find("inteum", true);
if (prop == null)
{
    prop = mailItem.UserProperties.Add("inteum", Outlook.OlUserPropertyType.olText);
    prop.Value = dlgDropAction.SelectedRemarkFk.ToString();
}
else
{
    prop.Value = dlgDropAction.SelectedRemarkFk.ToString();
}


I know I can right-click on the Outlook column headers and show the column but I want to know how to do this programmatically. How can I via code, add this User Property and show it in the visible columns automatically like the screenshot shown here:

User added an image
Posted 14 Jan, 2013 15:36:41 Top
Andrei Smolin


Add-in Express team


Posts: 18822
Joined: 2006-05-11
Hello,

Unless the UserProperty is added to the folder fields you can't use it in a view. You add the UserPorperty to the folder fields by using the third parameter of the UserProperties.Add method (see http://msdn.microsoft.com/en-us/library/office/ff867389.aspx).

Then you add that folder field to the view. At http://msdn.microsoft.com/en-us/library/office/ff860939.aspx, they suggest that you use the the ViewFields.Add method.

See also http://social.msdn.microsoft.com/Forums/sr-Latn-CS/outlookdev/thread/12bc197a-30a8-4aa6-8784-442ea5cac3e5.

Hope this helps.


Andrei Smolin
Add-in Express Team Leader
Posted 15 Jan, 2013 02:41:25 Top
bhodso




Posts: 6
Joined: 2013-01-14
The way I solved this (based on your tips above) was first getting the current folder that was selected like this:


// get explorer instance
Outlook.Explorer explorerInstance = explorer as Outlook.Explorer;

// get current folder
Outlook.MAPIFolder folder = null;
folder = explorerInstance.CurrentFolder;

// make sure folder is a tableView
if (folder != null && folder.CurrentView.ViewType == Outlook.OlViewType.olTableView)
{
     
}


After I got the folder and made sure it is a tableview type, I checked if there are any mail items in it:


if (folder.Items != null && folder.Items.Count > 0)
{
     if (folder.Items[1] is Outlook._MailItem)
     {
         mailItem = folder.Items[1] as Outlook._MailItem;

         // now I add the property to this mail Item if it has not already been added using code similar to this:
         // SEE link at bottom of post

         // finally I set the current view of the folder to show this new column
         tableView = folder.CurrentView as Outlook.TableView;
         viewField = tableView.ViewFields.Add("Exact Property Name I Added to MailItem above");
         columnFormat = viewField.ColumnFormat;
         columnFormat.Align = Outlook.OlAlign.olAlignLeft;
         columnFormat.Width = 10;
         tableView.Save();
         tableView.Apply();
     }
}


Here is the link that shows how to add a property to a mailItem:

http://www.add-in-express.com/creating-addins-blog/2011/08/19/how-to-add-a-custom-property-to-the-userproperties-collection-of-an-e-mail-item-in-outlook/
Posted 17 Jan, 2013 14:07:09 Top
Andrei Smolin


Add-in Express team


Posts: 18822
Joined: 2006-05-11
Many thanks for sharing the solution with forum readers!


Andrei Smolin
Add-in Express Team Leader
Posted 18 Jan, 2013 01:13:33 Top