Monday, October 01, 2007 by Niels Hartvig

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.

Before you begin

  1. This code assumes that you have a MemberType called "newsletter" and a MemberGroup named "newsletter" as well.
  2. Create a web application project and add references to businesslogic.dll and cms.dll
  3. Override the web.config in the new project with the original one from your umbraco installation and then remove all "ConfigSource" references in the web.config. Also remove the reference to the UrlReWriter in the httpModules section. If you're running umbraco v3.0.3 (latest) this new Web Application project will be able to work fully with the API as if it was running inside your umbraco installation. If you're working with an umbraco version lower than 3.0.3 you'll need to create an "umbraco" directory and copy the "version.xml" file.

The code - default.asp.cs

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(); } } }

4 comment(s) for “Code Sample: Importing members from outlook”

  1. Douglas Robar Says:

    Very nice. Thanks for sharing!

    I just love how umbraco is so easy to extend without needing to mess with the application's source code. 50 lines in a new project and you're done. Beautiful.

  2. Rune Says:

    Will this code duplicate members if importing more than one time?

  3. Hartvig Says:

    Yes, it'll duplicate as it doesn't check if the member already exists - that's just one line of code though :)

  4. Joitmecepooma Says:

    http://free-xxx-porn.quotaless.com - free porn
    http://toon-porn.bidsex.net - online porn
    http://download-porn.my2gig.com - sex porn
    http://hentai-porn.free-site-host.com - free porn
    http://hot-porn.freehostingz.com - sexx
    http://stawebnice.volny.cz - link
    http://home-porn.5gbfree.com - sex
    http://porn-star-book.678host.com - sex link
    http://free-black-porn.8000web.com - cool
    http://russian-porn.hostistry.com - aaa
    http://hard-porn.freeblog.hu - link
    http://narutoporn.freemyforumadult.com - ooo
    http://free-porn-vids.angelfire.com - eee
    http://free-mature-porn.tripod.com - uuu

Leave comment:

Comments are disabled for older posts