Custom field

  • Thread starter Thread starter Mark Smith
  • Start date Start date
M

Mark Smith

Is there a way to add / change a custom field (or property? Is there a
difference?) for a MailItem using the Outlook Object Model or do I have to
use MAPI to do that? In either case, what method or property would I use?

Mark
 
You can add fields with the UserProperties.Add method in the Outlook object
model.
 
Never mind. I figured it out. When I first looked at the documentation, I
misunderstood it.

Mark
 
This is not working for me. I am creating a new field as follows:

CComPtr<Outlook::UserProperties> pProperties;
if (SUCCEEDED(pMailItem->get_UserProperties(&pProperties)) && pProperties !=
NULL) {
CComPtr<Outlook::UserProperty> pProperty;
if (SUCCEEDED(pProperties->Add(CComBSTR("Saved to PM"), olYesNo,
CComVariant(true), CComVariant(1), &pProperty)) && pProperty != NULL) {
HRESULT hr = pProperty->put_Value(CComVariant(true));
if (FAILED(hr)) {
sprintf(pBuf, "Failed: %d", hr);
errorBox(pBuf);
}
}
}

After executing this code (put_Value does succeed), I look in Outlook, and
the folder containing the MailItem does indeed now have a field named "Saved
to PM". However, when I show the field in the list view, nothing shows up
in it, nor does sorting on that column have any effect.

I have tried different values for the display format and they have no
effect, other than that some values (I think anything that is not 1-4)
causes the Add call to fail.

When I view the properties of the MailItem using OutlookSpy, the count of
custom fields is 0 both before and after executing this code.

Am I missing something important here?

Mark
 
Perhaps a dumb question, but are you also saving the item somewhere later in your code?

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
No, not a dumb question. That was the problem. It didn't even occur to me
to try saving the message.

More questions:
1) Is it possible to make the custom yes/no field editable by the user?
2) When I add the custom field to an e-mail, is there a way to make it
visible in the current view programmatically, or will the user have to
manually add it?

Mark

Perhaps a dumb question, but are you also saving the item somewhere later in
your code?

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
1) Yes, usually with a check box control on a custom form.

2) Yes, in Outlook 2002 or 2003, using the View.XML property of the current view.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Mark Smith said:
No, not a dumb question. That was the problem. It didn't even occur to me
to try saving the message.

More questions:
1) Is it possible to make the custom yes/no field editable by the user?
2) When I add the custom field to an e-mail, is there a way to make it
visible in the current view programmatically, or will the user have to
manually add it?

Mark

Perhaps a dumb question, but are you also saving the item somewhere later in
your code?
 
1) What I was hoping for was just a way for the user to click on the check
box in the explorer and have it toggle. I noticed that you can do this in
Outlook Express for the "Flag" column. Does Outlook support this?

Can a custom form be created by an addin? Or is that something that has to
be created separately and installed?

What about adding items to the context menu for an inspector or explorer?
Can you do that?

I appreciate the help, especially considering I have a lot of beginner
questions. This is getting into areas that I have no experience with.
Mark

1) Yes, usually with a check box control on a custom form.

2) Yes, in Outlook 2002 or 2003, using the View.XML property of the current
view.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Mark Smith said:
No, not a dumb question. That was the problem. It didn't even occur to
me
to try saving the message.

More questions:
1) Is it possible to make the custom yes/no field editable by the user?
2) When I add the custom field to an e-mail, is there a way to make it
visible in the current view programmatically, or will the user have to
manually add it?

Mark

Perhaps a dumb question, but are you also saving the item somewhere later
in
your code?
 
1) A Yes/No field can appear as a check box in the Explorer windows. Why not try it?

Some of the operations needed to modify a standard Outlook item and publish it as a custom form are available programmatically, but not all. Most applications include their custom forms as part of the installation.

Outlook does not directly expose the right-click context menu in its CommandBars collection. You will, however, see a new right-click command is when the item selected is using a custom form that includes one or more custom actions. You can also add a custom action without using a custom form, as demonstrated at http://www.outlookcode.com/codedetail.aspx?id=526

Richard Kagerer has posted a code sample at http://www.outlookcode.com/codedetail.aspx?id=314 that shows how you might trick Outlook into exposing the context menu through Explorer.CommandBars.

The C++ sample add-in at http://www.codeproject.com/atl/outlook2k3addin.asp also shows a technique for working with the context menu.


--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
I am trying to do something similar for Contact in the COntact view. I
would like an addition field to be shown in a certain view. So do I need
to add this using the View.XML or is there an easier way?
Also if I do need to use view.xml when I do this and I use an microsoft
schema as a property it overrides my heading, but if I use a schema that
I just make up then the field shows up but does not work properly?
Any ideas?
 
That's the only way, using View.XML. You need to use the proper schema name for your property, not make something up. Add the field to the view manually and then examine View.XML.
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
So can I create a new view and then immediately change the view.xml?? It
seems that I cannot do these two operations at the same time? Also would
you recommend just feeding the xml stirng or using XmlDocument and
manually changing?
Rog
 
Sue thank you for your time.
I looked at the xml that was returned and then saved the xml and tried
to load it back into the view.xml object with my new field, but my new
field does not show up?
Any ideas?
 
Three steps are actually involved:

1) Add the property to the folder, assuming you haven't already done this.
2) Create a new view, which will set up a lot of defaults that Outlook needs to take care of (and that are not documented).
3) Add a new <column> element to View.XML. For best results, use XMLDocument.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Did you create the field definition in the folder first?

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
You can do it either way. If you want to do it programmatically, create an item in the folder and then add a property to the item with UserProperties.Add. You can then discard the item.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
It seems that if I add the field through outlook maually it creats a new
schema, if I then try to do the same thing using view.xml it will add
the field to the folder view, but the field will nto work. By that I
mean I cannot select the checkbox:
Here is the line I added:
<column>\r\n\t\t<format>boolicon</format>\r\n\t\t<heading>JimSync</heading>\r\n\t\t<prop>http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/JimSync</prop>\r\n\t\t<type>boolean</type>\r\n\t\t<width>50</width>\r\n\t\t<style>padding-left:3px;;text-align:center</style>\r\n\t\t<displayformat>3</displayformat>\r\n\t</column>

and below the is the entire xml.maybe this is b/c I do not add the field
first to the folder, but I am not sure how to do that.


view.XML = "<?xml version=\"1.0\"?>\r\n<view
type=\"table\">\r\n\t<viewname>My
View</viewname>\r\n\t<viewstyle>table-layout:fixed;width:100%;font-family:Tahoma;font-style:normal;font-weight:normal;font-size:8pt;color:Black;font-charset:0</viewstyle>\r\n\t<viewtime>212659370</viewtime>\r\n\t<linecolor>8421504</linecolor>\r\n\t<linestyle>3</linestyle>\r\n\t<gridlines>1</gridlines>\r\n\t<autosizing>0</autosizing>\r\n\t<newitemrow>1</newitemrow>\r\n\t<incelledit>1</incelledit>\r\n\t<usequickflags>0</usequickflags>\r\n\t<collapsestate></collapsestate>\r\n\t<rowstyle>background-color:#FFFFFF</rowstyle>\r\n\t<headerstyle>background-color:#D3D3D3</headerstyle>\r\n\t<previewstyle>color:Blue</previewstyle>\r\n\t<arrangement>\r\n\t\t<autogroup>0</autogroup>\r\n\t\t<collapseclient></collapseclient>\r\n\t</arrangement>\r\n\t<column>\r\n\t\t<name>HREF</name>\r\n\t\t<prop>DAV:href</prop>\r\n\t\t<checkbox>1</checkbox>\r\n\t</column>\r\n\t<column>\r\n\t\t<heading>Full
Name</heading>\r\n\t\t<prop>urn:schemas:contacts:cn</prop>\r\n\t\t<type>string</type>\r\n\t\t<width>50</width>\r\n\t\t<style>padding-left:3px;;text-align:left</style>\r\n\t</column>\r\n\t<column>\r\n\t\t<heading>Account</heading>\r\n\t\t<prop>urn:schemas:contacts:account</prop>\r\n\t\t<type>string</type>\r\n\t\t<width>50</width>\r\n\t\t<style>padding-left:3px;;text-align:left</style>\r\n\t</column>\r\n\t<column>\r\n\t\t<heading>Title</heading>\r\n\t\t<prop>urn:schemas:contacts:personaltitle</prop>\r\n\t\t<type>string</type>\r\n\t\t<width>50</width>\r\n\t\t<style>padding-left:3px;;text-align:left</style>\r\n\t</column>\r\n\t<column>\r\n\t\t<heading>Department</heading>\r\n\t\t<prop>urn:schemas:contacts:department</prop>\r\n\t\t<type>string</type>\r\n\t\t<width>50</width>\r\n\t\t<style>padding-left:3px;;text-align:left</style>\r\n\t</column>\r\n\t<column>\r\n\t\t<heading>Birthday</heading>\r\n\t\t<prop>urn:schemas:contacts:bday</prop>\r\n\t\t<type>datetime</type>\r\n\t\t<width
50</width>\r\n\t\t<style>padding-left:3px;;text-align:left</style>"+
"\r\n\t\t<format>MMMM d,
yyyy</format>\r\n\t\t<displayformat>5</displayformat>\r\n\t\t<bestfitdate>0</bestfitdate>\r\n\t</column>\r\n\t<column>\r\n\t\t<heading>Manager's
Name</heading>\r\n\t\t<prop>urn:schemas:contacts:manager</prop>\r\n\t\t<type>string</type>\r\n\t\t<width>50</width>\r\n\t\t<style>padding-left:3px;;text-align:left</style>\r\n\t</column>\r\n\t<column>\r\n\t\t<heading>Business
Phone</heading>\r\n\t\t<prop>urn:schemas:contacts:officetelephonenumber</prop>\r\n\t\t<type>string</type>\r\n\t\t<width>50</width>\r\n\t\t<style>padding-left:3px;;text-align:left</style>\r\n\t\t<shortheading>Business</shortheading>\r\n\t</column>\r\n\t<column>\r\n\t\t<heading>Mobile
Phone</heading>\r\n\t\t<prop>http://schemas.microsoft.com/mapi/proptag/0x3a1c001f</prop>\r\n\t\t<type>string</type>\r\n\t\t<width>50</width>\r\n\t\t<style>padding-left:3px;;text-align:left</style>\r\n\t\t<shortheading>Mobile</shortheading>\r\n\t</column>\r\n\t<column>\r\n\t\t<heading>Home
Phone</heading>\r\n\t\t<prop>urn:schemas:contacts:homePhone</prop>\r\n\t\t<type>string</type>\r\n\t\t<width>50</width>\r\n\t\t<style>padding-left:3px;;text-align:left</style>\r\n\t\t<shortheading>Home</shortheading>\r\n\t</column>\r\n\t<column>\r\n\t\t<heading>Business
Fax</heading>\r\n\t\t<prop>urn:schemas:contacts:facsimiletelephonenumber</prop>\r\n\t\t<type>string</type>\r\n\t\t<width>50</width>\r\n\t\t<style>padding-left:3px;;text-align:left</style>\r\n\t</column>\r\n\t<column>\r\n\t\t<heading>Other
Phone</heading>\r\n\t\t<prop>urn:schemas:contacts:otherTelephone</prop>\r\n\t\t<type>string</type>\r\n\t\t<width>50</width>\r\n\t\t<style>padding-left:3px;;text-align:left</style>\r\n\t\t<shortheading>Other</shortheading>\r\n\t</column>\r\n\t<column>\r\n\t\t<heading>Assistant's
Name</heading>\r\n\t\t<prop>urn:schemas:contacts:secretarycn</prop>\r\n\t\t<type>string</type>\r\n\t\t<width>50</width>\r\n\t\t<style>padding-left:3px;;text-align:left</style>\r\n\t</column>\r\n\t<column>\r\n\t\t<heading>Assistant's
Phone</heading>\r\n\t\t<prop>"+

"urn:schemas:contacts:secretaryphone</prop>\r\n\t\t<type>string</type>\r\n\t\t<width>50</width>\r\n\t\t<style>padding-left:3px;;text-align:left</style>\r\n\t\t<shortheading>Assistant</shortheading>\r\n\t</column>\r\n\t<column>\r\n\t\t<heading>Business
Address</heading>\r\n\t\t<prop>urn:schemas:contacts:workaddress</prop>\r\n\t\t<type>string</type>\r\n\t\t<width>50</width>\r\n\t\t<style>padding-left:3px;;text-align:left</style>\r\n\t\t<shortheading>Business</shortheading>\r\n\t</column>\r\n\t<column>\r\n\t\t<heading>Other
Address</heading>\r\n\t\t<prop>urn:schemas:contacts:otherpostaladdress</prop>\r\n\t\t<type>string</type>\r\n\t\t<width>50</width>\r\n\t\t<style>padding-left:3px;;text-align:left</style>\r\n\t\t<shortheading>Other</shortheading>\r\n\t</column>\r\n\t<column>\r\n\t\t<heading>E-mail</heading>\r\n\t\t<prop>http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/8084001f</prop>\r\n\t\t<type>string</type>\r\n\t\t<width>50</width>\r\n\t\t<style>padding-left:3px;;text-align:left</style>\r\n\t</column>\r\n\t<column>\r\n\t\t<heading>E-mail
2</heading>\r\n\t\t<prop>http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/8094001f</prop>\r\n\t\t<type>string</type>\r\n\t\t<width>50</width>\r\n\t\t<style>padding-left:3px;;text-align:left</style>\r\n\t</column>\r\n\t<column>\r\n\t\t<format>boolicon</format>\r\n\t\t<heading>JimSync</heading>\r\n\t\t<prop>http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/JimSync</prop>\r\n\t\t<type>boolean</type>\r\n\t\t<width>50</width>\r\n\t\t<style>padding-left:3px;;text-align:center</style>\r\n\t\t<displayformat>3</displayformat>\r\n\t</column>\r\n\t"+
"<column>\r\n\t\t<heading>Customer
ID</heading>\r\n\t\t<prop>urn:schemas:contacts:customerid</prop>\r\n\t\t<type>string</type>\r\n\t\t<width>50</width>\r\n\t\t<style>padding-left:3px;;text-align:left</style>\r\n\t</column>\r\n\t<orderby>\r\n\t\t<order>\r\n\t\t\t<heading>File
As</heading>\r\n\t\t\t<prop>urn:schemas:contacts:fileas</prop>\r\n\t\t\t<type>string</type>\r\n\t\t\t<sort>asc</sort>\r\n\t\t</order>\r\n\t</orderby>\r\n\t<multiline>\r\n\t\t<width>0</width>\r\n\t</multiline>\r\n\t<groupbydefault>0</groupbydefault>\r\n\t<previewpane>\r\n\t\t<markasread>0</markasread>\r\n\t</previewpane>\r\n\t</column>\r\n</view>\r\n";
 
You must add the field to the folder first. Think about if: If you don't, what is Outlook going to display when you add the column?

Did you try UserProperties.Add as I suggested?

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Thank you Sue, I got it working now. One other question I have is in
creating a new view and then defining it.
It seems if I create a new view and then immediately after I create it I
try to modify the View.XML it does not work, but if I create the view
and then close the app and then restart and on restart get the view I
just created and then modify the View.xml it seems to work. Have you
seen this and no anyway around this so I can create the view and then
modify the xml all at the same time?
Thanks,
Rog
 
Back
Top