Adding Custom tabs to Outlook application.
In this section let us design the UI to display the phone details of a contact person. This Custom Tab will contain read-only fields.
In addition, it shows the following:
Creating a New Form Region
using System.Linq; using System.Text; using Office = Microsoft.Office.Core; using Outlook = Microsoft.Office.Interop.Outlook; using Microsoft.Office.Interop.Outlook; using CRMContacts.Outlook.Common; using CRMContacts.Outlook.Common.ExceptionUtility; using CRMContacts.Outlook.Common.UIResources; using System.Windows.Forms; using System.Drawing;
// Occurs before the form region is initialized. // To prevent the form region from appearing, set e.Cancel to true. // Use e.OutlookItem to get a reference to the current Outlook item. private void PhoneDetailsTabFactory_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e) { Logger.Log(Severity.Verbose, Categories.Outlook_CustomTab, "PhoneDetailsTabFactory::PhoneDetailsTabFactory_FormRegionInitializing()"); ContactItem outlookItem = e.OutlookItem as ContactItem; UserProperties userProperties = outlookItem.UserProperties; string projectName = (System.IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location)); projectName = projectName.Remove(projectName.IndexOf(".")); if (!OutlookUtilities.IsSAPItem(userProperties) || OutlookUtilities.GetApplicationName(userProperties) != "Template1" || OutlookUtilities.EscapeFileName(projectName) != userProperties[Constants.PROJECTNAME].Value) { e.Cancel = true; } else { Logger.Log(Severity.Info, Categories.Outlook_CustomTab, "Enabling Custom Properties Tab for business application Template1 for outlook item entry Id {0}", outlookItem.EntryID); } OutlookUtilities.CleanUpResources(userProperties); }
/// <summary> /// This method will load the UserPropertiesTab Region with the custom properties of the item selected /// </summary> private void LoadCustomFieldsDataGrid() { Logger.Log(Severity.Verbose, Categories.Outlook_CustomTab, "PhoneDetailsTabFactory::LoadCustomFieldsDataGrid()"); UserProperties userProperties = null; ContactItem outlookItem = null; try { outlookItem = this.OutlookItem as ContactItem; userProperties = outlookItem.UserProperties; bool itemCreate = string.IsNullOrEmpty(outlookItem.EntryID); this.AddPropertyToDataGrid("MobileCallerNumber", userProperties, itemCreate); this.AddPropertyToDataGrid("MobileCountryCode", userProperties, itemCreate); this.AddPropertyToDataGrid("MobileNumber", userProperties, itemCreate); this.AddPropertyToDataGrid("MobileSequenceNumber", userProperties, itemCreate); this.AddPropertyToDataGrid("TelephoneCountryCode", userProperties, itemCreate); this.AddPropertyToDataGrid("TelephoneExtension", userProperties, itemCreate); this.AddPropertyToDataGrid("TelephoneNumberWithExtension", userProperties, itemCreate); this.AddPropertyToDataGrid("TelephoneSequenceNumber", userProperties, itemCreate); } catch (System.Exception ex) { if (ExceptionHandler.IsFatalException(ex)) { throw; } ExceptionHandler.ShowMessage(ResourceManager.GetLocalizedText("UserProperties_LoadGrid", Constants.ErrorMessages)); Logger.Log(Severity.Error, Categories.Outlook_CustomTab, "Error while Loading the grid on the PhoneDetailsTab region"); Logger.LogException(Severity.Error, Categories.Outlook_CustomTab, ex); } finally { OutlookUtilities.CleanUpResources(userProperties); OutlookUtilities.CleanUpResources(outlookItem); } }
/// <summary> /// This method will set the data grid row style to read only /// </summary> /// <param name="dataGridCell">datagrid cell whose value is to be set</param> /// <param name="dataGridRow">datagrid row being set</param> private static void SetDataGridStyleToReadOnly(DataGridViewTextBoxCell dataGridCell, DataGridViewRow dataGridRow) { dataGridCell.ReadOnly = true; dataGridRow.DefaultCellStyle.BackColor = SystemColors.Control; } /// <summary> /// This method will add the property to the data grid /// </summary> /// <param name="propertyName">property name</param> /// <param name="userProperties">user properties name</param> /// <param name="isItemCreate">is item create</param> private void AddPropertyToDataGrid(string propertyName, UserProperties userProperties, bool isItemCreate) { Logger.Log(Severity.Verbose, Categories.Outlook_CustomTab, "PhoneDetailsTabFactory::AddPropertyToDataGrid(propertyName:{0},isItemCreate:{1})", propertyName, isItemCreate); DataGridViewRow dataGridRow = null; DataGridViewTextBoxCell dataGridCell = null; PropertyMetadata propertyMetadata = null; UserProperty userProperty = null; dataGridRow = new DataGridViewRow(); dataGridCell = new DataGridViewTextBoxCell(); if (CRMContacts.contact.Contact.SAPPropertyAttributes.ContainsKey(propertyName)) { propertyMetadata = CRMContacts.contact.Contact.SAPPropertyAttributes[propertyName]; } dataGridCell.Value = (propertyMetadata == null || string.IsNullOrEmpty(propertyMetadata.Label)) ? propertyName : propertyMetadata.Label; dataGridCell.Tag = propertyName; if (propertyMetadata != null && propertyMetadata.Mandatory) { dataGridCell.Value += "*"; } dataGridRow.Cells.Add(dataGridCell); dataGridCell = new DataGridViewTextBoxCell(); userProperty = userProperties[OutlookUtilities.GetUserPropertyName(propertyName)]; if (userProperty != null && userProperty.Value != null) { dataGridCell.Value = userProperty.Value; } dataGridRow.Cells.Add(dataGridCell); OutlookUtilities.CleanUpResources(userProperty); SetDataGridStyleToReadOnly(dataGridCell, dataGridRow); dgvPhoneDetails.Rows.Add(dataGridRow); }