I needed to import some address groups from Outlook into an umbraco installation for a friend last week. It was very simple and I though I'd share the code. Hope you like it - feel free to ask questions in the comments.
The code is quite simple. Our contact list from Outlook was exported to a textfile and consists of a names and emails separated by a tab. Remember to add a decent server timeout if you're importing thousands of contacts as the umbraco API can be a little slow as it's not optimized for bulk operations like this.
using System;
using System.IO;
using System.Web.UI;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.member;
namespace WebApplication1
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Get default user
User u = new User(0);
// Get member type
MemberType mt = MemberType.GetByAlias("newsletter");
// Get the newsletter group
int newsletterGroupId = MemberGroup.GetByName("newsletter").Id;
// Open files
StreamReader re = File.OpenText(Server.MapPath("import1"));
string input = "";
while ((input = re.ReadLine()) != null)
{
string importStatus = "";
string name = "";
string email = "";
try
{
// split the name and email by tab
string[] nameAndEmail = input.Split("\t".ToCharArray());
// Some names exported by Outlook contains the email in brackets, we'll exclude that from the name.
name = nameAndEmail[0];
if (name.IndexOf("(") > -1)
name = name.Substring(0, name.IndexOf("("));
email = nameAndEmail[1];
// Create member
Member m = Member.MakeNew(name, email, mt, u);
// Add new member to newsletter group
m.AddGroup(newsletterGroupId);
// Print import status
importStatus = string.Format("<li>{0}, {1} imported succesfully</li>", name, email);
}
catch (Exception ee)
{
// Print import status on failure
importStatus =
string.Format("<li style=\"color: red;\">{0}, {1} failed with reason: {2}</li>", name, email,
ee.ToString());
}
Response.Write(importStatus);
}
re.Close();
}
}
}