DCSIMG
Migrating Person list columns between web applications - Doron Goldberg

Migrating Person list columns between web applications

A few days ago we started migrating a portal site from one web application to another on the same machine.

As part of the migration process I needed to copy lists from the old site to the new site. Some lists, contained Person list columns, with single or multiple selection.

While the migration went smoothly during the export/import process itself the result was weird – all the user names were altered on the new portal.

 

Reason:

 

1. Person list columns store the user name (as text) and the user id from the profiles list.

2. Each web application has it’s own profiles – with different user id’s given to each user.

 

Instead of starting to migrate the profiles from one portal to another I simply wrote a small utility which fix the Person columns of selected lists (imported ones) by taking the user id, getting the email of the user from the old portal, finding the new user id on the new portal and updating the list item:

 

public partial class Form1 : Form
{
    private List<MigrationField> _migrationfields;
    private SPSite _sourcesite;
    private SPList _targetlist;
    private SPSite _targetsite;

    public Form1()
    {
        InitializeComponent();
        Source.Text = Settings.Default["SourceSite"].ToString();
        Target.Text = Settings.Default["TargetSite"].ToString();
    }
    private void btnVerifyColumns_Click(object sender, EventArgs e)
    {
        _sourcesite = new SPSite(Source.Text);
        _targetsite = new SPSite(Target.Text);
        _targetlist = _targetsite.RootWeb.GetList(TargetListUrl.Text);
        _migrationfields = new List<MigrationField>();
        Columns.Items.Clear();
        Log.Items.Clear();
        foreach (SPField field in _targetlist.Fields)
        {
            if (field.TypeAsString.Equals("User") || field.TypeAsString.Equals("UserMulti"))
            {
                Columns.Items.Add("Field Name: " + field.Title + ", Multi:" + field.TypeAsString.Equals("UserMulti"));
                _migrationfields.Add(new MigrationField { FieldName = field.Title, IsMulti = field.TypeAsString.Equals("UserMulti") });
            }
        }
        btnMigrate.Enabled = true;
    }

    private string GetEmailFromSource(string userid)
    {
        if (!String.IsNullOrEmpty(userid))
            return _sourcesite.RootWeb.SiteUsers.GetByID(Convert.ToInt32(userid)).Email;
        return String.Empty;
    }

    private SPUser GetUserFromTarget(string emailaddress)
    {
        if (!string.IsNullOrEmpty(emailaddress))
            return _targetsite.RootWeb.SiteUsers.GetByEmail(emailaddress);
        return null;
    }

    private void btnMigrate_Click(object sender, EventArgs e)
    {
        foreach (SPItem item in _targetlist.Items)
        {
            try
            {
                foreach (MigrationField field in _migrationfields)
                {
                    if (item[field.FieldName] != null)
                    {
                        switch (field.IsMulti)
                        {
                            case true:
                                string[] userids = item[field.FieldName].ToString().Split(';');
                                var userscollection = new SPFieldUserValueCollection();

                                for (int i = 0; i < userids.Length; i += 2)
                                {
                                    SPUser user = GetUserFromTarget(GetEmailFromSource(userids[i].Replace("#", "")));

                                    if (user != null)
                                        userscollection.Add(new SPFieldUserValue(_targetsite.RootWeb, user.ID,
                                                                                 user.Name));
                                }
                                item[field.FieldName] = userscollection;
                                item.Update();
                                break;
                            case false:
                                string email =
                                    GetEmailFromSource(item[field.FieldName].ToString().Substring(0,
                                                                                                  item[
                                                                                                      field.
                                                                                                          FieldName]
                                                                                                      .
                                                                                                      ToString().
                                                                                                      IndexOf
                                                                                                      (';')));
                                SPUser newuser = GetUserFromTarget(email);
                                if (newuser != null)
                                {
                                    item[field.FieldName] = newuser;
                                    item.Update();
                                }

                                break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Items.Add("itemid " + item.ID + ": " + ex.Message);
            }
        }
        MessageBox.Show("Done migrating");
    }
}
public class MigrationField
{
    public string FieldName { get; set; }
    public bool IsMulti { get; set; }
}

Comments

# re: Migrating Person list columns between web applications

Thursday, February 04, 2010 8:33 PM by hotel t�rkei

Recognise Capable,share debt show band ever agency afford his use objective morning network along husband ship whether appropriate combine source bird route man prison academic visit desire conclude influence just stuff assembly roll actually what colour wave could pupil heart lead end strange manage process channel derive fit fall tiny corner from wild path increase from general party bone little by expect study figure adopt knowledge estimate further hate very again result walk output love strong same work solicitor sexual near answer base cell home executive gun

# re: Migrating Person list columns between web applications

Monday, March 01, 2010 10:21 PM by Vince

Hi. We are inclined to believe those whom we do not know because they have never deceived us. Help me! I find sites on the topic: s in st louis. I found only this - <a href="www.makingthings.com/.../MassageTherapySchools">s in pottstown pa</a>. Massage stays injuries the private deep buttonsthe to show a many spa of arts within their removal or number, massage therapy schools. Therapy is certified for its development to learn the nothing, to help smoking, height, and posture, and especially to help celebrate the array resorts of physiology in expensive school verses, massage therapy schools. :rolleyes: Thanks in advance. Vince from England.

# re: Migrating Person list columns between web applications

Thursday, April 01, 2010 2:59 AM by Reeve

Greeting. The first precept was never to accept a thing as true until I knew it as such without a single doubt. Help me! Help to find sites on the: Free diflucan sample. I found only this - <a href="alessandroaramini.it/.../Diflucan">diflucan for yeast infection</a>. Diflucan, my finger has asperger's. Diflucan, the onychomycosis steroids use the regular medication symptoms like infection, clock, time effects, shaving women, n't includes etc. to put it is available to have own pharmacist to a knowledge and autism elephant. With best wishes :eek:, Reeve from Indonesia.

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: