Import Members into Umbraco from a CSV file using the MemberService

Posted on Tuesday, July 5, 2016

For one of our projects we had a big amount of Members that needed to be imported into Umbraco. Of course we could have spent a few days adding them one by one but that would have been a waste of time and effort.

I know you can use a package like CMSImport, which is developed by Richard Soeteman, but the free edition is only limited to import 500 records. So we went programming our own code to read a CSV file and create members on the fly.

In the following steps we will define our Member type with extra properties in the Umbraco backoffice, setup the CSV file with all of the necessary data and create a SurfaceController which will handle the import.

 

Define the Member type

The first step is to define our Member Type in the Umbraco backoffice.

We only need one, so we used the default that is already present in Umbraco. The only thing we did is added some extra properties.

Below you can find the definition of our Member Type:

Member type - add extra properties

As you can see we have defined 4 extra properties: the first 2 are TextStrings, the last 2 are Content Pickers.

Another requirement of this project was the ability to have members with the same e-mail address. Standard you can’t do this in Umbraco but you can easily change this configuration in the web.config.

In you go to the web.config and search for ’MemberShipProvider’ you can see 2 providers, one for members ( frontend ) and one for users ( backend ).

You need to set the property requiresUniqueEmail to 'false' of the UmbracoMembershipProvider and hit Save.

 

Setup the CSV file to import

The next step is to setup our CSV file which will contain all of the data to fill in the properties of our members. To do this we will split each property into separated columns.

Below you can find a short example of the CSV file:

CSV file with member data

As you can see we have defined 8 columns:

  • Column A: The custom Code property
  • Column B: The custom Title property
  • Column C: The standard Email property
  • Column D: The standard Username property
  • Column E: The ID of the Branch Content node already present into Umbraco ( manually added into our CSV file )
  • Column F: The ID of the SalesPerson Content node already present into Umbraco ( manually added into our CSV file )
  • Column G: The standard Name property
  • Column H: The password to set for the member

We have saved this CSV file into our project under a new folder called Imports.

 

Create the import function

In the final step we are going to create a SurfaceController to handle the import.

Go to your Visual Studio, right click the Controllers folder and choose Add > Controller.

Add controller in Visual Studio

In the Add Scaffold window choose the MVC 5 Controller – Empty template and click on Add.

Add scaffold - controller

In the Add controller window type in the name of your new controller and click on Add.

Enter name for the new controller

You can remove the Index method because we won’t need it.

 

Now before we start coding we need to know how we can create members into Umbraco programmatically. If you are familiar with Umbraco you may now that Umbraco provides some “gateways” to manipulate data like content, members, media, …

We call them services and they are all available through the ApplicationContext, but because we are using a SurfaceController we can call any service through the Services property.

In our tutorial we are going to use the ‘MemberService’.

If you want more information about the ‘MemberService’ and all of the available methods, go to the Umbraco MemberService documentation.

 

Below you can find the Import method:

public ActionResult ImportMembers()
{
string _line = ""; using (StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("~/Imports/members.csv"))) { do { // Read a line from the csv file _line = sr.ReadLine(); // Check if the line is not empty if (!String.IsNullOrEmpty(_line)) { // Split the line using the ; delimiter string[] _splits = _line.Split(';'); if (_splits.Length == 8) { // Create a new member instance IMember member = Services.MemberService.CreateMember( _splits[3], // Username _splits[2], // Email _splits[6], // Display name "member" // Member type ); // Member is approved to login member.IsApproved = true; // Check is the branch content picker property exists and set the value if (member.HasProperty("branch")) member.SetValue("branch", _splits[4]); // Check is the sales person content picker property exists and set the value if (member.HasProperty("salesPerson") && !String.IsNullOrEmpty(_splits[5])) member.SetValue("salesPerson", _splits[5]); // Check is the code property exists and set the value if (member.HasProperty("code")) member.SetValue("code", _splits[0]); // Check is the title property exists and set the value if (member.HasProperty("title")) member.SetValue("title", _splits[1]); try { // Save the member Services.MemberService.Save(member); // Save the password of the member Services.MemberService.SavePassword(member, _splits[7]); // Assign the correct role Services.MemberService.AssignRole(member.Id, "MemberRole"); } catch { } } } } while (!sr.EndOfStream); } return null;
}

 

You can see we use the .CreateMember(string username, string email, string displayName, string memberTypeAlias) method to create the Member and use the .HasProperty(string propertyTypeAlias) and .SetProperty(string propertyTypeAlias, object value) methods to fill in our custom properties.

 

Now to executed this method and do the import we simply go to the following url:

http://localhost/umbraco/surface/membersurface/importmembers

 

After that login into the backend of Umbraco and go the Members section. You will now see the imported members.

 

I hope this is useful for others that need to import members into their environment.

  • umbraco
  • memberservice
  • members