Click here to Skip to main content
15,886,422 members
Articles / Productivity Apps and Services / Microsoft Office

Customize the built-in Outlook Select Names dialog (or any other)

Rate me:
Please Sign up or sign in to vote.
4.90/5 (35 votes)
14 Jan 2015CPOL14 min read 167.2K   2.6K   67  
In this article, you will learn how to customize the built-in Select Names dialog and use different external data sources for your own dialog.
using System.Linq;
using System.Data.Linq;
using System.Collections.Generic;
using System;
using System.IO;

namespace CustomAddressDialog.Database
{

    /// <summary>
    /// Represents the Databasecontext for our Contact Database
    /// </summary>
    public class CustomAddressDialogDB : DataContext
    {

        /// <summary>
        /// Construction code.
        /// Checks if the Database exists and if not, create a fresh DB from scratch.
        /// </summary>
        /// <param name="fileOrServerConnection">The full Path to the desired Database or a valid connectionstring.</param>
        public CustomAddressDialogDB(string fileOrServerConnection)
            : base(fileOrServerConnection)
        {
            if (!DatabaseExists())
            {
                CreateDatabase();
                AddCustomer("Ken", "Slovak", "some.address@somedomain.com", "Slovaktech");
                AddCustomer("Sue", "Mosher", "some.address@otherdomain.com", "Turtleflock");
                AddCustomer("Dmitry", "Streblechenko", "another.address@some.otherdomain.com", "Streblechenko");
                AddCustomer("Randy", "Byrne", "unknown@address.com", "Microsoft");
            }
        }

        /// <summary>
        /// Defines the Customer Table object
        /// </summary>
        public Table<Customer> _customerTable;

        /// <summary>
        /// Adds a new customer to the customers table
        /// </summary>
        /// <returns>Returns the new ID of the customer instance.</returns>
        public int AddCustomer(string firstname, string lastname, string emailaddress, string companyname)
        {
            Customer customer = new Customer();
            customer.Firstname = firstname;
            customer.Lastname = lastname;
            customer.Emailaddress = emailaddress;
            customer.Companyname = companyname;
            _customerTable.InsertOnSubmit (customer);
            SubmitChanges();
            return customer.CustomerId;
        }


        /// <summary>
        /// Search for all customers with the query in Lastname, Firstname or emailaddress
        /// </summary>
        /// <param name="query">The criteria</param>
        /// <param name="maxItems">Maximum Items to return</param>
        /// <returns>Returns a generic List of Customer objects.</returns>
        public List<Customer> FindCustomers(string query, int maxItems)
        {
            var q = from customer in _customerTable
                    where customer.Lastname.Contains(query)
                    || customer.Firstname.Contains(query)
                    || customer.Emailaddress.Contains(query)
                    orderby customer.Lastname, customer.Firstname
                    select customer;
            return q.Take(maxItems).ToList<Customer>();
        }

        /// <summary>
        /// Returns a collection of all Customers in the customers table
        /// </summary>
        /// <returns></returns>
        public List<Customer> GetCustomers()
        {
            var q = from customer in _customerTable
                    orderby customer.Lastname, customer.Firstname
                    select customer;
            return q.ToList<Customer>();
        }


    }
}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) X4U electronix
Germany Germany
I'm a 1968 model, made in Germany.
After School transformed into an electronic engineer and started repairing computers in 1986. From PET till now In the IT-world. Currently employed as softwaredeveloper in a company creating solutions for pharmacies.
MCSE2000, MCSD, MCTS - Team Foundation Server, MCTS - Visual Studio Tools for Office.
Specialized in Outlook/Exchange custom development. Languages: german, english, C#, C++, VB.Net, VB6, SQL.
In 2006 received an award as MVP - Visual Developer VSTO by Microsoft.
Homepage: [http://www.x4u.de]

Comments and Discussions