Error while adding user properties to the post item in outlook add

Y

Yuva

Hi,

I got the following error when I am trying to add the user properties to the
post item while developing Outlook 2007 Addin.
My outlook addin connects to TFS, and adds the workitems from the selected
server and project to the local Outlook PST file. The workitems are being
added to the corresponding PST folder as a post item. My code works fine when
I give the columns to be added by default as below:
{
PostItem postItem = folder.Items.Add(OlItemType.olPostItem) as PostItem;
postItem.Subject = workItem.Title;
UserProperty property = postItem.UserProperties.Add("WorkItemType",
OlUserPropertyType.olText,Type.Missing, Type.Missing);
property.Value = workItem.Type.Name;

property = postItem.UserProperties.Add("ID", OlUserPropertyType.olText,
Type.Missing,Type.Missing);
property.Value = workItem.Id;

property = postItem.UserProperties.Add("WorkItemTitle",
OlUserPropertyType.olText, Type.Missing,Type.Missing);
property.Value = workItem.Title;

postItem.Save();
}

but it gives me the error when I try to add the properties dynamically using
foreach loop as below:


foreach (WorkItem workItem in workItems)
{
PostItem postItem = folder.Items.Add(OlItemType.olPostItem) as PostItem;
UserProperty property = null;
foreach (FieldDefinition fieldDefinition in workItems.DisplayFields)
{
property = postItem.UserProperties.Add(fieldDefinition.Name,
OlUserPropertyType.olText,
Type.Missing,
Type.Missing);
property.Value = workItem.Fields[fieldDefinition.ReferenceName].Value;
}
postItem.Save();
}



The error is:




{System.Runtime.InteropServices.COMException (0x8004E002):
A field with this name already exists in the "All Document fields" field
set. Enter a different name.
at Microsoft.Office.Interop.Outlook.UserProperties.Add(String Name,
OlUserPropertyType Type, Object AddToFolderFields, Object DisplayFormat)
at WorkItemFolderView.CreatePostWorkItem(MAPIFolder folder,
WorkItemCollection workItems)


Can anyone help me out with this error. Am not well versed with coding, so
am sorry if there is some basic error. Looking for your help!!!

Thanks in Advance,
Yuva
 
K

Ken Slovak - [MVP - Outlook]

Try setting the AddToFolderFields argument to false on each iteration except
the first one. That way once you've added the property to the folder fields
that for that first one you won't be doing it again in the remainder of the
loop. See if that helps.
 
Y

Yuva

Hi Ken Slovak,

I tried that but that doesn't seem to work. Outlook Post item has some
reserved fields and when am trying to add user properties with the same name,
it gives me the error as the field already exists. To make a quick fix, I
have appended a default string with the field definition name while adding
the user properties and that works fine. Is there any other way to work on.

Thanks,
Yuva

Ken Slovak - said:
Try setting the AddToFolderFields argument to false on each iteration except
the first one. That way once you've added the property to the folder fields
that for that first one you won't be doing it again in the remainder of the
loop. See if that helps.




Yuva said:
Hi,

I got the following error when I am trying to add the user properties to
the
post item while developing Outlook 2007 Addin.
My outlook addin connects to TFS, and adds the workitems from the selected
server and project to the local Outlook PST file. The workitems are being
added to the corresponding PST folder as a post item. My code works fine
when
I give the columns to be added by default as below:
{
PostItem postItem = folder.Items.Add(OlItemType.olPostItem) as PostItem;
postItem.Subject = workItem.Title;
UserProperty property = postItem.UserProperties.Add("WorkItemType",
OlUserPropertyType.olText,Type.Missing, Type.Missing);
property.Value = workItem.Type.Name;

property = postItem.UserProperties.Add("ID", OlUserPropertyType.olText,
Type.Missing,Type.Missing);
property.Value = workItem.Id;

property = postItem.UserProperties.Add("WorkItemTitle",
OlUserPropertyType.olText, Type.Missing,Type.Missing);
property.Value = workItem.Title;

postItem.Save();
}

but it gives me the error when I try to add the properties dynamically
using
foreach loop as below:


foreach (WorkItem workItem in workItems)
{
PostItem postItem = folder.Items.Add(OlItemType.olPostItem) as PostItem;
UserProperty property = null;
foreach (FieldDefinition fieldDefinition in workItems.DisplayFields)
{
property = postItem.UserProperties.Add(fieldDefinition.Name,
OlUserPropertyType.olText,
Type.Missing,
Type.Missing);
property.Value = workItem.Fields[fieldDefinition.ReferenceName].Value;
}
postItem.Save();
}



The error is:




{System.Runtime.InteropServices.COMException (0x8004E002):
A field with this name already exists in the "All Document fields" field
set. Enter a different name.
at Microsoft.Office.Interop.Outlook.UserProperties.Add(String Name,
OlUserPropertyType Type, Object AddToFolderFields, Object DisplayFormat)
at WorkItemFolderView.CreatePostWorkItem(MAPIFolder folder,
WorkItemCollection workItems)


Can anyone help me out with this error. Am not well versed with coding, so
am sorry if there is some basic error. Looking for your help!!!

Thanks in Advance,
Yuva
 
K

Ken Slovak - [MVP - Outlook]

If you're trying to add fields that are reserved then I can see why you're
having a problem. That's not allowed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top