{"id":206,"date":"2015-12-29T07:28:26","date_gmt":"2015-12-29T07:28:26","guid":{"rendered":"http:\/\/www.softreeconsulting.com\/?p=206"},"modified":"2015-12-29T07:28:26","modified_gmt":"2015-12-29T07:28:26","slug":"retain-item-id-while-copying-item-from-one-list-to-another-list","status":"publish","type":"post","link":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retain-item-id-while-copying-item-from-one-list-to-another-list\/","title":{"rendered":"Copy list item and keep same ID"},"content":{"rendered":"<p><span style=\"color: #000000;\"><u>Retain Item ID while copying item from one list to another list<\/u><\/span><\/p>\n<p><span style=\"color: #000000;\">While copying\/migrating item list item id can&#8217;t be retained<\/span><\/p>\n<p><span style=\"color: #000000;\">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.<\/span><\/p>\n<p><span style=\"color: #000000;\">Procedure to retain item id (id of every item in source list should be same as id of corresponding item in destination list)<\/span><\/p>\n<ol>\n<li><span style=\"color: #000000;\">Create a new list to which you want to copy<\/span><\/li>\n<li><span style=\"color: #000000;\">Sorted all items from source list in ascending order according to item id. it must be sorted to start from lowest item Id<\/span><\/li>\n<li><span style=\"color: #000000;\">Let&#8217;s there are items with id 1,2,5,7,8 and so on<\/span><\/li>\n<li><span style=\"color: #000000;\">To maintain id you have to create and delete item until source list item id is not equal to destination list item id.<\/span><\/li>\n<li><span style=\"color: #000000;\">First create item in destination list ,and first item id will be 1<\/span><\/li>\n<li><span style=\"color: #000000;\">Compare destination id with source item id, if it is same then continue to create second item<\/span><\/li>\n<li><span style=\"color: #000000;\">If it is not same then delete that item and create new item and compare both id.<\/span><\/li>\n<\/ol>\n\n\n<pre class=\"wp-block-code\"><code>class Program {\n\nstatic void Main(string[] args)\n\n{\n\nstring sourceSiteUrl = \"Enter Source Site Url\";\n\nstring destinationSiteUrl = \" Enter Destination Site Url \";\n\nstring sourceListName = \"Custom List\";\n\nstring destinationListName = \"New List\";\n\n\n\n\n\/\/ dictionary used to store List item ID and List item\n\nSystem.Collections.Generic.Dictionary&lt;int, ListItem> _dicListItemColl = new Dictionary&lt;int, ListItem>();\n\n\/\/ sorted list is used to sort all list items according to item id in ascending order\n\nList&lt;int> sortedList = new List&lt;int>();\n\n\n\n\nusing (ClientContext ctx = new ClientContext(sourceSiteUrl))\n\n{\n\nctx.Credentials = new System.Net.NetworkCredential(\"UserName\", \"Password\");\n\nWeb web = ctx.Web;\n\nctx.Load(web, w => w.Lists);\n\nList list = web.Lists.GetByTitle(sourceListName);\n\nctx.Load(list);\n\nListItemCollection lic = list.GetItems(new CamlQuery());\n\nctx.Load(lic, itms => itms.Include(itm => itm.Id,itm=>itm.FieldValuesAsText\n\n));\n\nctx.ExecuteQuery();\n\n\/\/ Store all items and item id\n\nforeach (ListItem item in lic)\n\n{\n\n_dicListItemColl.Add(item.Id, item);\n\n}\n\n\/\/ sorted in ascending order\n\nsortedList = _dicListItemColl.Keys.ToList();\n\nsortedList.Sort();\n\n}\n\nusing (ClientContext destContext = new ClientContext(destinationSiteUrl))\n\n{\n\ndestContext.Credentials = new System.Net.NetworkCredential(\"Username\", \"Password\");\n\nWeb web = destContext.Web;\n\ndestContext.Load(web, w => w.Lists);\n\nListItem srcItem = null;\n\n\/\/ Use newly created List Or create here new list,\n\n\/\/here i have created list manually (You can create list programmatically)\n\nList destList = web.Lists.GetByTitle(destinationListName);\n\ndestContext.Load(destList);\n\n\/\/Loop each item from source List\n\nforeach (var key in sortedList)\n\n{\n\nsrcItem = _dicListItemColl[key];\n\nCopyItem(destContext, web, destList, srcItem);\n\n}\n\n}\n\n}\n\nprivate static void CopyItem(ClientContext destContext, Web web, List destList, ListItem srcItem)\n\n{\n\nListItemCreationInformation newItemInfo = null;\n\nListItem newItem = null;\n\nnewItemInfo = new ListItemCreationInformation();\n\nnewItem = destList.AddItem(newItemInfo);\n\nnewItem.Update();\n\ndestContext.ExecuteQuery();\n\n\/\/if source item Id is not equal to destination item id then create and delete\n\n\/\/item until source item id and destination item will not be same\n\nif (newItem.Id != srcItem.Id &amp;&amp; newItem.Id &lt; srcItem.Id)\n\n{\n\nnewItem.DeleteObject();\n\nfor (int iCount = newItem.Id; iCount &lt;= srcItem.Id - 2; iCount++)\n\n{\n\nnewItemInfo = new ListItemCreationInformation();\n\nnewItem = destList.AddItem(newItemInfo);\n\nnewItem.Update();\n\nnewItem.DeleteObject();\n\n}\n\ndestContext.ExecuteQuery();\n\nnewItemInfo = new ListItemCreationInformation();\n\nnewItem = destList.AddItem(newItemInfo);\n\nnewItem[\"Title\"] = srcItem.FieldValuesAsText[\"Title\"];\n\nnewItem.Update();\n\n}\n\nelse\n\n{\n\nnewItem[\"Title\"] = srcItem.FieldValuesAsText[\"Title\"];\n\nnewItem.Update();\n\n}\n\ndestContext.ExecuteQuery();\n\n}\n\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Retain Item ID while copying item from one list to another list While copying\/migrating item list item id can&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":394,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[62,63,64,65,66,67,68,69],"class_list":["post-206","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sharepoint","tag-copy-list-item-and-keep-same-id","tag-how-to-retain-listitem-id-csom","tag-how-to-retain-listitem-id-using-client-object-model","tag-itemid","tag-listitem","tag-retain-listitem-id","tag-retain-listitem-id-client-object-model","tag-retain-listitem-id-csom"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Copy list item and keep same ID - Softree Technology<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/softreetechnology.com\/blog\/sharepoint\/retain-item-id-while-copying-item-from-one-list-to-another-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Copy list item and keep same ID - Softree Technology\" \/>\n<meta property=\"og:description\" content=\"Retain Item ID while copying item from one list to another list While copying\/migrating item list item id can&#8217;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 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/softreetechnology.com\/blog\/sharepoint\/retain-item-id-while-copying-item-from-one-list-to-another-list\/\" \/>\n<meta property=\"og:site_name\" content=\"Softree Technology\" \/>\n<meta property=\"article:published_time\" content=\"2015-12-29T07:28:26+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Copy list item and keep same ID - Softree Technology","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retain-item-id-while-copying-item-from-one-list-to-another-list\/","og_locale":"en_US","og_type":"article","og_title":"Copy list item and keep same ID - Softree Technology","og_description":"Retain Item ID while copying item from one list to another list While copying\/migrating item list item id can&#8217;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 [&hellip;]","og_url":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retain-item-id-while-copying-item-from-one-list-to-another-list\/","og_site_name":"Softree Technology","article_published_time":"2015-12-29T07:28:26+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retain-item-id-while-copying-item-from-one-list-to-another-list\/#article","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retain-item-id-while-copying-item-from-one-list-to-another-list\/"},"author":{"name":"admin","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b"},"headline":"Copy list item and keep same ID","datePublished":"2015-12-29T07:28:26+00:00","mainEntityOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retain-item-id-while-copying-item-from-one-list-to-another-list\/"},"wordCount":194,"commentCount":0,"publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retain-item-id-while-copying-item-from-one-list-to-another-list\/#primaryimage"},"thumbnailUrl":"","keywords":["Copy list item and keep same ID","how to Retain Listitem id csom","how to Retain Listitem id using client object model","Itemid","ListItem","Retain Listitem id","Retain Listitem id client object model","Retain Listitem id csom"],"articleSection":["SharePoint"],"inLanguage":"en","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/softreetechnology.com\/blog\/sharepoint\/retain-item-id-while-copying-item-from-one-list-to-another-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retain-item-id-while-copying-item-from-one-list-to-another-list\/","url":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retain-item-id-while-copying-item-from-one-list-to-another-list\/","name":"Copy list item and keep same ID - Softree Technology","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retain-item-id-while-copying-item-from-one-list-to-another-list\/#primaryimage"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retain-item-id-while-copying-item-from-one-list-to-another-list\/#primaryimage"},"thumbnailUrl":"","datePublished":"2015-12-29T07:28:26+00:00","breadcrumb":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retain-item-id-while-copying-item-from-one-list-to-another-list\/#breadcrumb"},"inLanguage":"en","potentialAction":[{"@type":"ReadAction","target":["https:\/\/softreetechnology.com\/blog\/sharepoint\/retain-item-id-while-copying-item-from-one-list-to-another-list\/"]}]},{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retain-item-id-while-copying-item-from-one-list-to-another-list\/#primaryimage","url":"","contentUrl":""},{"@type":"BreadcrumbList","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/retain-item-id-while-copying-item-from-one-list-to-another-list\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/softreetechnology.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Copy list item and keep same ID"}]},{"@type":"WebSite","@id":"https:\/\/softreetechnology.com\/blog\/#website","url":"https:\/\/softreetechnology.com\/blog\/","name":"Softree Technology","description":"Celebrating 10+ Years in SharePoint Consulting !","publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/softreetechnology.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en"},{"@type":"Organization","@id":"https:\/\/softreetechnology.com\/blog\/#organization","name":"Softree Technology","url":"https:\/\/softreetechnology.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","contentUrl":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","width":844,"height":230,"caption":"Softree Technology"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b","name":"admin","image":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/softreeconsulting.com"],"url":"https:\/\/softreetechnology.com\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=206"}],"version-history":[{"count":0,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/206\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}