none
keeping existing custom fields without losing data

    Question

  • Hi

    For a while I have been using a custom page on a contact form to store information about my clients using custom fields in outlook - these it appears are stored in that custom form

    I am now creating a new form region and add-in to take my solution one step further

    My problem is how can I move/copy the existing custom fields and the values to use them in a new outlook form region

    When I create the new form region I can't see the fields to bind them as they obviously don't exist in my outlook file as they are part of an old form I have removed to replace with this new one

    Did I make a mistake when originally creating the form, but creating the custom fields in the form? rather than in outlook? where should I be storing them so no such issue in future?


    Darren Rose

    Sunday, December 23, 2012 11:28 PM

Answers

  • I'd recommend creating the Form Region in Visual Studio.  Then use code to load the custom field values from any given item using the UserProperties collection.  Use UserProperties.Find to look for any of your custom fields - if it exists, read the value from the UserProperty field and populate a control with that value using code.

    If the loaded item does not use a custom form, use UserProperties.Add when saving values from a control to add the custom field to that item.  If the item is using a custom message class, then custom fields defined with that custom form should already be present in the UserProperties collection.  DO NOT add fields to an item based on a custom form if that field isn't published with the custom form as it will create a one-off item and revert to the standard message class.


    Eric Legault MVP (Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    Monday, December 24, 2012 8:34 PM

All replies

  • You really need to locate the original publishing location for the form (i.e. a certain Contacts folder or the Personal Forms library), or the .fdm or .oft file for the form if you saved it to a file.  The Field Chooser dialog will allow you to pick the fields from the current item you are customizing or from any form in the Personal Forms library.

    Saving and Publishing Outlook Forms:
    http://www.outlookcode.com/article.aspx?id=34


    Eric Legault MVP (Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    Monday, December 24, 2012 6:53 PM
  • Hi Eric

    Thanks for reply

    Yes I get I have to open existing form to get the fields - but they then go when I close form - so how do I move them from old form to new form, or from old form into outlook itself - if it makes any difference I use exchange (office 365)


    Darren Rose

    Monday, December 24, 2012 8:05 PM
  • Custom fields work a little different with a Form Region, because a Form Region can be used with multiple types of items that could have different sets of custom fields.  If you create a field via the New button on the Value tab on a custom control's property page while in the Form Region designer, the field will be published to the folder where the form is published (under User-defined Fields in Folder in the Field Chooser dialog), but not within the custom form itself (so you won't see it under User-defined Fields in This Item in the All Fields page).

    If the fields are saved with the form itself and not the Form Region/folder, then you can copy all of them to a new form by simply republishing the original form with a new name.


    Eric Legault MVP (Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    Monday, December 24, 2012 8:20 PM
  • My existing solution is a custom page on a contact form created many years ago

    I am now redesigning it and want to create form region in outlook or perhaps Visual Studio as part of an add-in - and don't want to lose existing information stored in outlook under the current form

    So still not sure how I can re-create or re-use the fields I created as part of old form - in my new form region - without having to keep the old form loaded - which I don't want to do, as want to delete it and use new solution

    Sorry probably not making the best sense here - but hope you understand my issue

    Basically creating a new form region probably in Visual Studio as want to create an add-in to do various tasks using the values in the custom fields

    I have 1000+ contacts all with information already in these custom fields - which I can view fine using existing form, but how do I move the fields and data to new form region or outlook so I can use in new solution and then be able to delete the old custom form I was using?


    Darren Rose

    Monday, December 24, 2012 8:25 PM
  • I'd recommend creating the Form Region in Visual Studio.  Then use code to load the custom field values from any given item using the UserProperties collection.  Use UserProperties.Find to look for any of your custom fields - if it exists, read the value from the UserProperty field and populate a control with that value using code.

    If the loaded item does not use a custom form, use UserProperties.Add when saving values from a control to add the custom field to that item.  If the item is using a custom message class, then custom fields defined with that custom form should already be present in the UserProperties collection.  DO NOT add fields to an item based on a custom form if that field isn't published with the custom form as it will create a one-off item and revert to the standard message class.


    Eric Legault MVP (Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    Monday, December 24, 2012 8:34 PM
  • Hi

    I think we were talking a bit cross purposes here - perhaps me not explaining too well

    The original form had custom fields associated with it that were showing as "user defined fields in this item" - hence why I was getting confused as to why couldn't see existing data when creating fields in new form region

    So I found if I recreated custom field with exact name/type that the data was still okay and field was available again

    So for my new form region - where should I be creating these custom/user-defined fields to get the existing data that is in these fields in my outlook contacts? do I somehow create them in visual studio when creating add-in and form region

    OR

    Do I create them in outlook but this time make sure to create them so they show as "user defined fields in this folder"

    Also then how do I populate the fields on my form region in visual studio to show the data held in these custom fields?


    Darren Rose

    Friday, December 28, 2012 12:13 AM
  • One thing to understand about Form Regions is that they have kind of a loose relationship with custom fields, since you can associate a Form Region with different items that can have different sets of custom fields.  If you are binding controls to a field in Outlook's Form Region designer, you have to have access to that field (or create a new one) whereever it may be at the time of design.  However, when the item is opened and your Form Region is displayed, controls on that Form Region will then look to see if it's associated field is present in the item and display the value for that field if it exists.

    But if you use Visual Studio to design a Form Region, there is no way to bind the controls to a specific field.  You have to read from and write to the fields explicitly using code.

    Does this help a little more?


    Eric Legault MVP (Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    Friday, December 28, 2012 6:01 PM
  • yes that makes sense thanks

    So...

    1) how in code do I populate fields on my form region

    2) do the custom fields need to already have been created again in outlook - bear in mind they already existed with my old form, now removed and contain data already, which I don't want to lose - so do I create them again so they appear in "user defined fields in this folder" for my contact folder? or what?


    Darren Rose

    Friday, December 28, 2012 6:32 PM
  • Retrieve your custom fields by name with ContactItem.UserProperties.Find during the FormRegionInitializing event and write the values of the fields to your controls - see here for a sample:

    Walkthrough: Designing an Outlook Form Region:
    http://msdn.microsoft.com/en-us/library/bb157865.aspx

    If the fields don't exist in the item, then don't write to your controls.  If you want a custom field that doesn't exist added to the Contact, then during ContactItem.Save (or a control/form event) you can call UserProperties.Add to add the field.  Set the AddToFolderFields argument to True to save the field to the folder.

    UserProperties.Add Method (Outlook):
    http://msdn.microsoft.com/en-us/library/ff867389(office.14).aspx

    Note that if the Contact is based on a custom form already, and you add a custom field that DOES NOT exist in the custom form definition, it will create a one-off form and the Contact item will revert to the default message class (IPM.Contact). If you expect custom fields, then you should have that custom form published in a location where items based on that custom form can access it (the Personal Forms Library is a good place to start).


    Eric Legault MVP (Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    Friday, December 28, 2012 6:58 PM
  • Okay thanks - used following code to test retrieving value from a custom field and it seems to work, does that look right to you? :-

            Dim myItem As Outlook.ContactItem = CType(Me.OutlookItem, Outlook.ContactItem)   
            DateTimePicker1.Text = myItem.UserProperties.Find("Mailing Sent").Value

    Another issue is that I often change to phone list view in contact and sort my contacts by a custom field - but obviously I can't now find my custom fields as the form they were created with is no longer used - which brings me back to previous question as to where should these user defined fields now be recreated - remembering they were in a custom form previously - but I now no longer use/want that form - just the custom field names

    Contact is not based on a custom form, I now have no custom forms, just a form region added to bottom of default contact form


    Darren Rose

    Friday, December 28, 2012 8:04 PM
  • Your code has the right idea, but needs error handling.  You can't assume .Find will return a UserProperty object, so use a defined variable for that and only proceed if the var is valid.

    If you want custom fields added to the folder so you can use them in Views, you'll have to use this method at least once for every field per folder:

    UserDefinedProperties.Add Method (Outlook):
    http://msdn.microsoft.com/en-us/library/ff869539(office.14).aspx


    Eric Legault MVP (Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    Friday, December 28, 2012 8:18 PM
  • Thanks - yes will add error handling, was just a quick test

    Couple of issues I now come across though

    1) in outlook the date picker control has a button to choose none rather than a date - but the control used in visual studio doesn't have this option - how can I use a date picker like the outlook one normally used on forms?

    2) this in turn seems to create an issue that for some contacts date fields are set to "none" if the contact doesn't have a particular contract  - but this then makes the date appear as 01 January 4501 rather than saying "none"


    Darren Rose

    Saturday, December 29, 2012 12:11 AM
  • 1. Yeah, the goofy thing is that those olk* controls are only available with the Outlook Forms Designer, not in Visual Studio.  You'll have to use a comparable .NET date-time picker control from Telerik or DevExpress.

    2. Yes, the display of a null date in Outlook is "None" but they're stored as 1/1/4501.  You have to handle that manually if you don't want that goofy date displayed in a non-Outlook control.


    Eric Legault MVP (Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    Saturday, December 29, 2012 6:33 AM
  • that seems a little bit odd - provide a development environment for office/outlook etc in visual studio and then not let you access any of the controls that are normally used

    Had quick look at telerik and devexpress and no obvious sign that they do a date control with a none button - so will have to continue research

    Think its going to be simpler just to go back to designing form or form region in outlook - seems to be that using visual studio just makes it more complex rather than giving you more freedom to enhance it etc

    Thanks for all your help


    Darren Rose

    Saturday, December 29, 2012 12:14 PM
  • LOL if Outlook dev wasn't odd I wouldn't have a career as everybody would know how to do it well!

    Most DevExpress controls have mechanisms for handling null values in any way you want.


    Eric Legault MVP (Outlook)
    About me...
    Outlook Appins: Store Social Media fields in your Outlook Contacts!

    Monday, December 31, 2012 4:01 PM