Copy list item and keep same ID

Retain Item ID while copying item from one list to another list

While copying/migrating item list item id can’t be retained

ID field of list is read only field, that field cannot be edited, id field is auto increment integer value, which will be increment by one after every item was added.

Procedure to retain item id (id of every item in source list should be same as id of corresponding item in destination list)

  1. Create a new list to which you want to copy
  2. Sorted all items from source list in ascending order according to item id. it must be sorted to start from lowest item Id
  3. Let’s there are items with id 1,2,5,7,8 and so on
  4. To maintain id you have to create and delete item until source list item id is not equal to destination list item id.
  5. First create item in destination list ,and first item id will be 1
  6. Compare destination id with source item id, if it is same then continue to create second item
  7. If it is not same then delete that item and create new item and compare both id.
class Program {

static void Main(string[] args)

{

string sourceSiteUrl = "Enter Source Site Url";

string destinationSiteUrl = " Enter Destination Site Url ";

string sourceListName = "Custom List";

string destinationListName = "New List";




// dictionary used to store List item ID and List item

System.Collections.Generic.Dictionary<int, ListItem> _dicListItemColl = new Dictionary<int, ListItem>();

// sorted list is used to sort all list items according to item id in ascending order

List<int> sortedList = new List<int>();




using (ClientContext ctx = new ClientContext(sourceSiteUrl))

{

ctx.Credentials = new System.Net.NetworkCredential("UserName", "Password");

Web web = ctx.Web;

ctx.Load(web, w => w.Lists);

List list = web.Lists.GetByTitle(sourceListName);

ctx.Load(list);

ListItemCollection lic = list.GetItems(new CamlQuery());

ctx.Load(lic, itms => itms.Include(itm => itm.Id,itm=>itm.FieldValuesAsText

));

ctx.ExecuteQuery();

// Store all items and item id

foreach (ListItem item in lic)

{

_dicListItemColl.Add(item.Id, item);

}

// sorted in ascending order

sortedList = _dicListItemColl.Keys.ToList();

sortedList.Sort();

}

using (ClientContext destContext = new ClientContext(destinationSiteUrl))

{

destContext.Credentials = new System.Net.NetworkCredential("Username", "Password");

Web web = destContext.Web;

destContext.Load(web, w => w.Lists);

ListItem srcItem = null;

// Use newly created List Or create here new list,

//here i have created list manually (You can create list programmatically)

List destList = web.Lists.GetByTitle(destinationListName);

destContext.Load(destList);

//Loop each item from source List

foreach (var key in sortedList)

{

srcItem = _dicListItemColl[key];

CopyItem(destContext, web, destList, srcItem);

}

}

}

private static void CopyItem(ClientContext destContext, Web web, List destList, ListItem srcItem)

{

ListItemCreationInformation newItemInfo = null;

ListItem newItem = null;

newItemInfo = new ListItemCreationInformation();

newItem = destList.AddItem(newItemInfo);

newItem.Update();

destContext.ExecuteQuery();

//if source item Id is not equal to destination item id then create and delete

//item until source item id and destination item will not be same

if (newItem.Id != srcItem.Id && newItem.Id < srcItem.Id)

{

newItem.DeleteObject();

for (int iCount = newItem.Id; iCount <= srcItem.Id - 2; iCount++)

{

newItemInfo = new ListItemCreationInformation();

newItem = destList.AddItem(newItemInfo);

newItem.Update();

newItem.DeleteObject();

}

destContext.ExecuteQuery();

newItemInfo = new ListItemCreationInformation();

newItem = destList.AddItem(newItemInfo);

newItem["Title"] = srcItem.FieldValuesAsText["Title"];

newItem.Update();

}

else

{

newItem["Title"] = srcItem.FieldValuesAsText["Title"];

newItem.Update();

}

destContext.ExecuteQuery();

}

}
Tags: , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *