How to migrate Sitecore users custom properties- PART 2

This is the second part of my post on migrating Sitecore users’ custom properties. In the first part, I’d explained how we can export all the custom properties to CSV file. In this part let’s see how we can import these custom profile properties to target Sitecore instance.

Crete an aspx page in your Sitecore destination root folder  . If you are doing it for a production environment then create this file under /Sitecore/Admin folder of your CM instance. I named this file as ImportUserCustomProperties.aspx.Paste below given code into it and change profile custom properties based on your requirement

<%@ Import Namespace=”System” %>
<%@ Import Namespace=”System.Linq” %>
<%@ Import Namespace=”System.IO” %>
<%@ Import Namespace=”System.Text” %>
<%@ Import Namespace=”System.Data” %>
<%@ Import Namespace=”System.Collections.Generic” %>
<%@ Import Namespace=”Toshiba.Core.Agents” %>
<%@ Import Namespace=”CsvHelper” %>

<%@ Page Language=”C#” Debug=”true” %>
<HTML>

string log = string.Empty;
string csvPath = @ “C:\inetpub\wwwroot\website\stringbuilder.csv”;
public void Page_Load(object sender, EventArgs e) {

ImportUserProfileFromCSV(csvPath);
}

public void ImportUserProfileFromCSV(string csvPath) {
//Response.Write(“message1”);
List userPrfileList = LoadCsv(csvPath);
foreach(UserProfile userProfile in userPrfileList) {
var user = Sitecore.Security.Accounts.User.FromName(userProfile.UserName, true);
if (user != null) {
var profile = user.Profile;
Sitecore.Data.Items.Item purchaseType = null;
Sitecore.Data.Items.Item industry = null;
Sitecore.Data.Items.Item jobTitle = null;
if (!string.IsNullOrEmpty(userProfile.PurchaseType))
purchaseType = Sitecore.Context.Database.GetItem(string.Format(“/sitecore/content/Data/Form Field Data Sources/Purchased From/{0}”, userProfile.PurchaseType));

if (!string.IsNullOrEmpty(userProfile.Industry))
industry = Sitecore.Context.Database.GetItem(string.Format(“/sitecore/content/Data/Form Field Data Sources/Industries/{0}”, userProfile.Industry));

if (!string.IsNullOrEmpty(userProfile.JobTitle))
jobTitle = Sitecore.Context.Database.GetItem(string.Format(“/sitecore/content//Data/Form Field Data Sources/Job Titles/{0}”, userProfile.JobTitle));

profile[“first_name”] = userProfile.FirstName;
profile[“last_name”] = userProfile.LastName;
profile[“email_address”] = userProfile.Email;
profile[“user_type”] = userProfile.UserType;
profile[“title”] = userProfile.Title;
profile[“industry”] = industry != null ? industry.ID.ToString() : “”;
profile[“job_title”] = jobTitle != null ? jobTitle.Name.ToString() : “”;
profile[“company_name”] = userProfile.CompanyName;
profile[“street_address”] = userProfile.Street;
profile[“suburb”] = userProfile.Suburb;
profile[“region”] = userProfile.Region;
profile[“postcode”] = userProfile.Postcode;
profile[“country”] = userProfile.Country;
profile[“tel_number”] = userProfile.Telephone;
profile[“purchase_type”] = purchaseType != null ? purchaseType.ID.ToString() : “”;
profile[“subscribed”] = userProfile.Subscribed;

profile.Save();
DoLog(profile[“first_name”]);
} else
DoLog(userProfile.UserName + ” user not found”);
//break;
}

}

List LoadCsv(string csvPath) {
var records = new List();
FileInfo file = new FileInfo(csvPath);
using(TextReader reader = file.OpenText()) {
using(var csv = new CsvReader(reader)) {
//csv.Configuration.RegisterClassMap();
records = csv.GetRecords().ToList();
}
}

return records;

}

public void DoLog(string message) {
Response.Write(message + ”
“);
}

public class UserProfile {
public string UserName {
get;
set;
}
public string FirstName {
get;
set;
}
public string LastName {
get;
set;
}
public string Email {
get;
set;
}
public string UserType {
get;
set;
}
public string Title {
get;
set;
}
public string Industry {
get;
set;
}
public string JobTitle {
get;
set;
}
public string CompanyName {
get;
set;
}
public string Street {
get;
set;
}
public string Suburb {
get;
set;
}
public string Region {
get;
set;
}
public string Postcode {
get;
set;
}
public string Country {
get;
set;
}
public string Telephone {
get;
set;
}
public string PurchaseType {
get;
set;
}
public string Subscribed {
get;
set;
}
}

<body>
</body>
</HTML>

Once you browse this page using a web browser all users’ custom properties will be migrated to your new Sitecore instance. Please don’t forget to edit this code to add your custom properties.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s