OutLookImporter - Import OutLook Public Folder Contacts

I have created a nice little tool to export OutLook Public folder contacts to CSV file. It basically exports the Master/Detail contact list from OutLook to CSV File Format. Then, we may import the list to SharePoint or Other Third Party Applications. In Outlook, the Details Tab Information [Key Contacts] are basically stored as individual user defined fields for each contact. So, to get the value of User Defined fields we may have to use the following syntax:

ctc.UserProperties["MyCompanyAlbertaRelationshipHolder"]
instead of:
ctc.FullName.ToString();

The following Public Folder Path I used in this example:
\\Public Folders\All Public Folders\MyCompany\MyCompanyAB\Customer Information Folders\All Public Folders\MyCompany\MyCompanyAB\Customer Information and List Name is: Customer Information. So, to get the items of Customer Information we may have to create the instance of the MAPI Folders using following methods:
Microsoft.Office.Interop.Outlook._Application olApp = new Microsoft.Office.Interop.Outlook.ApplicationClass(); Microsoft.Office.Interop.Outlook._NameSpace olNS = olApp.GetNamespace("MAPI"); Microsoft.Office.Interop.Outlook._Folders oFolders; oFolders = olNS.Folders; Microsoft.Office.Interop.Outlook.MAPIFolder oPublicFolder = oFolders["Public Folders"]; oFolders = oPublicFolder.Folders; Microsoft.Office.Interop.Outlook.MAPIFolder oAllPFolder = oFolders["All Public Folders"]; oFolders = oAllPFolder.Folders; Microsoft.Office.Interop.Outlook.MAPIFolder oMyCompanyFolder = oFolders["MyCompany"]; oFolders = oMyCompanyFolder.Folders; Microsoft.Office.Interop.Outlook.MAPIFolder oMyCompanyABFolder = oFolders["MyCompanyAB"]; oFolders = oMyCompanyABFolder.Folders; Microsoft.Office.Interop.Outlook.MAPIFolder oCustInfoFolder = oFolders["Customer Information"]; oFolders = oCustInfoFolder.Folders; Microsoft.Office.Interop.Outlook.MAPIFolder oContactInfoFolder = oFolders["Customer Information"];



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Outlook;
using System.IO;
namespace OutlookImporter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
importOutlook();
}
private void importOutlook()
{
try
{
string _Header = "'FileAs','Customer','Address','Email','Web','FABRelHolder','BusinessPhone','BusinessFax','Cellular','Council','";
_Header = _Header + "Contact1Name','Contact1Position','Contact1Phone','Contact1Fax','Contact1Email','Contact1Comments','";
_Header = _Header + "Contact2Name','Contact2Position','Contact2Phone','Contact2Fax','Contact2Email','Contact2Comments','";
_Header = _Header + "Contact3Name','Contact3Position','Contact3Phone','Contact3Fax','Contact3Email','Contact3Comments','";
_Header = _Header + "Contact4Name','Contact4Position','Contact4Phone','Contact4Fax','Contact4Email','Contact4Comments','";
_Header = _Header + "Contact5Name','Contact5Position','Contact5Phone','Contact5Fax','Contact5Email','Contact5Comments','";
_Header = _Header + "Contact6Name','Contact6Position','Contact6Phone','Contact6Fax','Contact6Email','Contact6Comments','";
_Header = _Header + "Contact11Name','Contact11Position','Contact11Phone','Contact11Fax','Contact11Email','Contact11Comments','";
_Header = _Header + "Contact12Name','Contact12Position','Contact12Phone','Contact12Fax','Contact12Email','Contact12Comments','";
_Header = _Header + "Contact13Name','Contact13Position','Contact13Phone','Contact13Fax','Contact132Email','Contact13Comments'";
logEvents(_Header);
Microsoft.Office.Interop.Outlook._Application olApp = new Microsoft.Office.Interop.Outlook.ApplicationClass();
Microsoft.Office.Interop.Outlook._NameSpace olNS = olApp.GetNamespace("MAPI");
Microsoft.Office.Interop.Outlook._Folders oFolders;
oFolders = olNS.Folders;
Microsoft.Office.Interop.Outlook.MAPIFolder oPublicFolder = oFolders["Public Folders"];
oFolders = oPublicFolder.Folders;
Microsoft.Office.Interop.Outlook.MAPIFolder oAllPFolder = oFolders["All Public Folders"];
oFolders = oAllPFolder.Folders;
Microsoft.Office.Interop.Outlook.MAPIFolder oMyCompanyFolder = oFolders["MyCompany"];
oFolders = oMyCompanyFolder.Folders;
Microsoft.Office.Interop.Outlook.MAPIFolder oMyCompanyABFolder = oFolders["MyCompanyAB"];
oFolders = oMyCompanyABFolder.Folders;
Microsoft.Office.Interop.Outlook.MAPIFolder oCustInfoFolder = oFolders["Customer Information"];
oFolders = oCustInfoFolder.Folders;
Microsoft.Office.Interop.Outlook.MAPIFolder oContactInfoFolder = oFolders["Customer Information"];
Microsoft.Office.Interop.Outlook.Items ctcItems = oContactInfoFolder.Items;
Microsoft.Office.Interop.Outlook.ContactItem ctc;
for (int j = 1; j < (ctcItems.Count + 1); j++)
{
ctc = (Microsoft.Office.Interop.Outlook.ContactItem)ctcItems[j];
string FileAs = "";
string Customer = "";
string Address = "";
string Email = "";
string Web = "";
string FABRelHolder = "";
string BusinessPhone = "";
string BusinessFax = "";
string Cellular = "";
string Council = "";
string Contact1Name = "";
string Contact1Position = "";
string Contact1Phone = "";
string Contact1Fax = "";
string Contact1Email = "";
string Contact1Comments = "";
string Contact2Name = "";
string Contact2Position = "";
string Contact2Phone = "";
string Contact2Fax = "";
string Contact2Email = "";
string Contact2Comments = "";
string Contact3Name = "";
string Contact3Position = "";
string Contact3Phone = "";
string Contact3Fax = "";
string Contact3Email = "";
string Contact3Comments = "";
string Contact4Name = "";
string Contact4Position = "";
string Contact4Phone = "";
string Contact4Fax = "";
string Contact4Email = "";
string Contact4Comments = "";
string Contact5Name = "";
string Contact5Position = "";
string Contact5Phone = "";
string Contact5Fax = "";
string Contact5Email = "";
string Contact5Comments = "";
string Contact6Name = "";
string Contact6Position = "";
string Contact6Phone = "";
string Contact6Fax = "";
string Contact6Email = "";
string Contact6Comments = "";
string Contact11Name = "";
string Contact11Position = "";
string Contact11Phone = "";
string Contact11Fax = "";
string Contact11Email = "";
string Contact11Comments = "";
string Contact12Name = "";
string Contact12Position = "";
string Contact12Phone = "";
string Contact12Fax = "";
string Contact12Email = "";
string Contact12Comments = "";
string Contact13Name = "";
string Contact13Position = "";
string Contact13Phone = "";
string Contact13Fax = "";
string Contact13Email = "";
string Contact13Comments = "";
if (ctc.FullName != null)
{
Customer = ctc.FullName.ToString();
}
if (ctc.BusinessAddress != null)
{
Address = ctc.BusinessAddress.ToString();
}
if (ctc.FileAs != null)
{
FileAs = ctc.FileAs.ToString();
}
if (ctc.Email1Address != null)
{
Email = ctc.Email1Address.ToString();
}
if (ctc.WebPage != null)
{
Web = ctc.WebPage.ToString();
}
if (ctc.BusinessTelephoneNumber != null)
{
BusinessPhone = ctc.BusinessTelephoneNumber.ToString();
}
if (ctc.BusinessFaxNumber != null)
{
BusinessFax = ctc.BusinessFaxNumber.ToString();
}
if (ctc.MobileTelephoneNumber != null)
{
Cellular = ctc.MobileTelephoneNumber.ToString();
}
if (ctc.UserProperties["RB01"] != null)
{
Council = ctc.UserProperties["RB01"].Value.ToString();
}
if (ctc.UserProperties["MyCompanyAlbertaRelationshipHolder"] != null)
{
FABRelHolder = ctc.UserProperties["MyCompanyAlbertaRelationshipHolder"].Value.ToString();
}
if (ctc.UserProperties["Name01"] != null)
{
Contact1Name = ctc.UserProperties["Name01"].Value.ToString();
}
if (ctc.UserProperties["Position01"] != null)
{
Contact1Position = ctc.UserProperties["Position01"].Value.ToString();
}
if (ctc.UserProperties["Phone01"] != null)
{
Contact1Phone = ctc.UserProperties["Phone01"].Value.ToString();
}
if (ctc.UserProperties["Fax01"] != null)
{
Contact1Fax = ctc.UserProperties["Fax01"].Value.ToString();
}
if (ctc.UserProperties["email01"] != null)
{
Contact1Email = ctc.UserProperties["email01"].Value.ToString();
}
if (ctc.UserProperties["contactComment01"] != null)
{
Contact1Comments = ctc.UserProperties["contactComment01"].Value.ToString();
}
if (ctc.UserProperties["Name02"] != null)
{
Contact2Name = ctc.UserProperties["Name02"].Value.ToString();
}
if (ctc.UserProperties["Position02"] != null)
{
Contact2Position = ctc.UserProperties["Position02"].Value.ToString();
}
if (ctc.UserProperties["Phone02"] != null)
{
Contact2Phone = ctc.UserProperties["Phone02"].Value.ToString();
}
if (ctc.UserProperties["Fax02"] != null)
{
Contact2Fax = ctc.UserProperties["Fax02"].Value.ToString();
}
if (ctc.UserProperties["email02"] != null)
{
Contact2Email = ctc.UserProperties["email02"].Value.ToString();
}
if (ctc.UserProperties["contactComment02"] != null)
{
Contact2Comments = ctc.UserProperties["contactComment02"].Value.ToString();
}
if (ctc.UserProperties["Name03"] != null)
{
Contact3Name = ctc.UserProperties["Name03"].Value.ToString();
}
if (ctc.UserProperties["Position03"] != null)
{
Contact3Position = ctc.UserProperties["Position03"].Value.ToString();
}
if (ctc.UserProperties["Phone03"] != null)
{
Contact3Phone = ctc.UserProperties["Phone03"].Value.ToString();
}
if (ctc.UserProperties["Fax03"] != null)
{
Contact3Fax = ctc.UserProperties["Fax03"].Value.ToString();
}
if (ctc.UserProperties["email03"] != null)
{
Contact3Email = ctc.UserProperties["email03"].Value.ToString();
}
if (ctc.UserProperties["contactComment03"] != null)
{
Contact3Comments = ctc.UserProperties["contactComment03"].Value.ToString();
}
if (ctc.UserProperties["Name04"] != null)
{
Contact4Name = ctc.UserProperties["Name04"].Value.ToString();
}
if (ctc.UserProperties["Position04"] != null)
{
Contact4Position = ctc.UserProperties["Position04"].Value.ToString();
}
if (ctc.UserProperties["Phone04"] != null)
{
Contact4Phone = ctc.UserProperties["Phone04"].Value.ToString();
}
if (ctc.UserProperties["Fax04"] != null)
{
Contact4Fax = ctc.UserProperties["Fax04"].Value.ToString();
}
if (ctc.UserProperties["email04"] != null)
{
Contact4Email = ctc.UserProperties["email04"].Value.ToString();
}
if (ctc.UserProperties["contactComment04"] != null)
{
Contact4Comments = ctc.UserProperties["contactComment04"].Value.ToString();
}
if (ctc.UserProperties["Name05"] != null)
{
Contact5Name = ctc.UserProperties["Name05"].Value.ToString();
}
if (ctc.UserProperties["Position05"] != null)
{
Contact5Position = ctc.UserProperties["Position05"].Value.ToString();
}
if (ctc.UserProperties["Phone05"] != null)
{
Contact5Phone = ctc.UserProperties["Phone05"].Value.ToString();
}
if (ctc.UserProperties["Fax05"] != null)
{
Contact5Fax = ctc.UserProperties["Fax05"].Value.ToString();
}
if (ctc.UserProperties["email05"] != null)
{
Contact5Email = ctc.UserProperties["email05"].Value.ToString();
}
if (ctc.UserProperties["contactComment05"] != null)
{
Contact5Comments = ctc.UserProperties["contactComment05"].Value.ToString();
}
if (ctc.UserProperties["Name06"] != null)
{
Contact6Name = ctc.UserProperties["Name06"].Value.ToString();
}
if (ctc.UserProperties["Position06"] != null)
{
Contact6Position = ctc.UserProperties["Position06"].Value.ToString();
}
if (ctc.UserProperties["Phone06"] != null)
{
Contact6Phone = ctc.UserProperties["Phone06"].Value.ToString();
}
if (ctc.UserProperties["Fax06"] != null)
{
Contact6Fax = ctc.UserProperties["Fax06"].Value.ToString();
}
if (ctc.UserProperties["email06"] != null)
{
Contact6Email = ctc.UserProperties["email06"].Value.ToString();
}
if (ctc.UserProperties["contactComment06"] != null)
{
Contact6Comments = ctc.UserProperties["contactComment06"].Value.ToString();
}
if (ctc.UserProperties["name11"] != null)
{
Contact11Name = ctc.UserProperties["name11"].Value.ToString();
}
if (ctc.UserProperties["Position11"] != null)
{
Contact11Position = ctc.UserProperties["Position11"].Value.ToString();
}
if (ctc.UserProperties["Phone11"] != null)
{
Contact11Phone = ctc.UserProperties["Phone11"].Value.ToString();
}
if (ctc.UserProperties["Fax11"] != null)
{
Contact11Fax = ctc.UserProperties["Fax11"].Value.ToString();
}
if (ctc.UserProperties["email11"] != null)
{
Contact11Email = ctc.UserProperties["email11"].Value.ToString();
}
if (ctc.UserProperties["contactComment11"] != null)
{
Contact11Comments = ctc.UserProperties["contactComment11"].Value.ToString();
}
if (ctc.UserProperties["Name12"] != null)
{
Contact12Name = ctc.UserProperties["Name12"].Value.ToString();
}
if (ctc.UserProperties["Position12"] != null)
{
Contact12Position = ctc.UserProperties["Position12"].Value.ToString();
}
if (ctc.UserProperties["Phone12"] != null)
{
Contact12Phone = ctc.UserProperties["Phone12"].Value.ToString();
}
if (ctc.UserProperties["Fax12"] != null)
{
Contact12Fax = ctc.UserProperties["Fax12"].Value.ToString();
}
if (ctc.UserProperties["email12"] != null)
{
Contact12Email = ctc.UserProperties["email12"].Value.ToString();
}
if (ctc.UserProperties["contactComment12"] != null)
{
Contact12Comments = ctc.UserProperties["contactComment12"].Value.ToString();
}
if (ctc.UserProperties["Name13"] != null)
{
Contact13Name = ctc.UserProperties["Name13"].Value.ToString();
}
if (ctc.UserProperties["Position13"] != null)
{
Contact13Position = ctc.UserProperties["Position13"].Value.ToString();
}
if (ctc.UserProperties["Phone13"] != null)
{
Contact13Phone = ctc.UserProperties["Phone13"].Value.ToString();
}
if (ctc.UserProperties["Fax13"] != null)
{
Contact13Fax = ctc.UserProperties["Fax13"].Value.ToString();
}
if (ctc.UserProperties["email13"] != null)
{
Contact13Email = ctc.UserProperties["email13"].Value.ToString();
}
if (ctc.UserProperties["contactComment13"] != null)
{
Contact13Comments = ctc.UserProperties["contactComment13"].Value.ToString();
}
string _Details = "'" + FileAs + "','" + Customer + "','" + Address + "','" + Email + "','" + Web + "','" + FABRelHolder + "','" + BusinessPhone + "','" + BusinessFax + "','" + Cellular + "','" + Council;
_Details = _Details + "','" + Contact1Name + "','" + Contact1Position + "','" + Contact1Phone + "','" + Contact1Fax + "','" + Contact1Email + "','" + Contact1Comments;
_Details = _Details + "','" + Contact2Name + "','" + Contact2Position + "','" + Contact2Phone + "','" + Contact2Fax + "','" + Contact2Email + "','" + Contact2Comments;
_Details = _Details + "','" + Contact3Name + "','" + Contact3Position + "','" + Contact3Phone + "','" + Contact3Fax + "','" + Contact3Email + "','" + Contact3Comments;
_Details = _Details + "','" + Contact4Name + "','" + Contact4Position + "','" + Contact4Phone + "','" + Contact4Fax + "','" + Contact4Email + "','" + Contact4Comments;
_Details = _Details + "','" + Contact5Name + "','" + Contact5Position + "','" + Contact5Phone + "','" + Contact5Fax + "','" + Contact5Email + "','" + Contact5Comments;
_Details = _Details + "','" + Contact6Name + "','" + Contact6Position + "','" + Contact6Phone + "','" + Contact6Fax + "','" + Contact6Email + "','" + Contact6Comments;
_Details = _Details + "','" + Contact11Name + "','" + Contact11Position + "','" + Contact11Phone + "','" + Contact11Fax + "','" + Contact11Email + "','" + Contact11Comments;
_Details = _Details + "','" + Contact12Name + "','" + Contact12Position + "','" + Contact12Phone + "','" + Contact12Fax + "','" + Contact12Email + "','" + Contact12Comments;
_Details = _Details + "','" + Contact13Name + "','" + Contact13Position + "','" + Contact13Phone + "','" + Contact13Fax + "','" + Contact13Email + "','" + Contact13Comments + "'";
logEvents(_Details);
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private static void logEvents(string logMessage)
{
string _fileName=@"C:\Documents and Settings\fchowdhu\Desktop\SP.csv";
StreamWriter streamWriter = new StreamWriter(_fileName, true);
streamWriter.WriteLine(logMessage);
streamWriter.Close();
}
}
}

Comments

  1. Welldone! I like it! you are still in you track my friend. - Biton (bitbit)

    ReplyDelete

Post a Comment

Popular posts from this blog

How to Create SSRS Report on SharePoint List

Database Testing With DBUnit

SAP RFC Integration through BizTalk