{"id":6139,"date":"2020-06-08T09:42:27","date_gmt":"2020-06-08T09:42:27","guid":{"rendered":"https:\/\/www.softreetechnology.com\/?p=6139"},"modified":"2020-06-08T09:42:27","modified_gmt":"2020-06-08T09:42:27","slug":"copying-list-list-fields-and-list-items-using-csom","status":"publish","type":"post","link":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copying-list-list-fields-and-list-items-using-csom\/","title":{"rendered":"Copying List, list fields and list Items using CSOM"},"content":{"rendered":"\n<p>       In this SharePoint blog, we are going to program how we can clone\/copy a list, list fields and list Items within a single site programmatically using .Net client object model (CSOM) in SharePoint Online.<\/p>\n\n\n\n<p>      Before we start, we need to create a console application in Visual Studio. We must refer the piece of code &amp; snapshot provided below for better understanding of the operation of program.<\/p>\n\n\n\n<p><strong>Step-1:<\/strong>  First we need to input Site URL &amp; the credentials for site and the List name which we want to copy.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.softreetechnology.com\/wp-content\/uploads\/2020\/06\/1.png\" alt=\"\" class=\"wp-image-6140\" width=\"702\" height=\"257\"\/><\/figure>\n\n\n\n<p><strong>Step-2:<\/strong>  Set the credentials for site.<\/p>\n\n\n\n<p><strong>Step-3:<\/strong>  Creating the destination List by referring the base ID of source list.<\/p>\n\n\n\n<p><strong>Step-4: <\/strong> Creating the field for destination list and adding the field value from the field collection of source list.<\/p>\n\n\n\n<p><strong>Step-5:<\/strong>  Retrieve all the items from the source list and adding it to the corresponding field of destination list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>      static void Main(string[] args)\n        {\n            #region [Variables]\n            string siteURL = string.Empty;\n            string username = string.Empty;\n            string password = string.Empty;\n            string sorList = string.Empty;\n            string newList = string.Empty;\n            #endregion #region [Constant Variables]\n            const string ctSiteURL = \"Provide a valid source Site URL: \";\n            const string ctUsername = \"Enter username: \";\n            const string ctPassword = \"Enter password: \";\n            const string ctSorList = \"Enter the exist source Listname: \";\n            const string ctNewList = \"Enter the new Listname you want to create: \";\n            #endregion\n\n            #region [SP Variables]\n            Web spWeb = null;\n            List spSrcList = null;\n            List spNewList = null;\n            FieldCollection spFieldColl = null;\n            #endregion\n\n            #region [ConsoleMessages]\n            Console.Write(ctSiteURL);\n            siteURL = Console.ReadLine();\n\n            Console.Write(ctUsername);\n            username = Console.ReadLine();\n\n            Console.Write(ctPassword);\n            password = Console.ReadLine();\n\n            Console.Write(ctSorList);\n            sorList = Console.ReadLine();\n\n            Console.Write(ctNewList);\n            newList = Console.ReadLine();\n            #endregion\n\n        using (ClientContext ctx = new ClientContext(siteURL))\n        {\n            SecureString securstr = new SecureString();\n            foreach (char ch in password)\n            {\n                securstr.AppendChar(ch);\n            }\n            ctx.Credentials = new SharePointOnlineCredentials(username, securstr);\n\n            spWeb = ctx.Web;\n\n            ctx.Load(ctx.Web);\n            spSrcList = ctx.Web.Lists.GetByTitle(sorList);\n\n            ctx.Load(spSrcList);\n            ctx.ExecuteQuery();\n\n            spNewList = CreateSPList(ctx, spWeb, spSrcList, newList);\n\n            spFieldColl = CopySPListFields(ctx, spSrcList, spNewList);\n\n            CreateSPListItems(ctx, spSrcList, spNewList, spFieldColl);\n\n            Console.WriteLine(\"The list\" + \" \" + newList + \" \" + \"Translated Successfully\");\n            Console.ReadLine();\n        }\n    }\n\n    \/\/\/ &lt;summary>\n    \/\/\/ This function is used to create the list in destination site \n    \/\/\/ based on source site list information\n    \/\/\/ &lt;\/summary>\n    \/\/\/ &lt;param name=\"ctx\">&lt;\/param>\n    \/\/\/ &lt;param name=\"web\">&lt;\/param>\n    \/\/\/ &lt;param name=\"srcList\">&lt;\/param>\n    \/\/\/ &lt;param name=\"newList\">&lt;\/param>\n    \/\/\/ &lt;returns>&lt;\/returns>\n    private static List CreateSPList(ClientContext ctx, Web web, List srcList, string newList)\n    {\n        #region [Variables]\n        ListCreationInformation destList = null;\n        List spNewList = null;\n\n        int baseid = -1;\n        #endregion\n\n        try\n        {\n            baseid = srcList.BaseTemplate;\n\n            destList = new ListCreationInformation();\n            destList.Title = newList;\n            destList.TemplateType = baseid;\n\n            web.Lists.Add(destList);\n            spNewList = ctx.Web.Lists.GetByTitle(destList.Title);\n\n            ctx.Load(srcList);\n            ctx.ExecuteQuery();\n\n        }\n        catch (Exception ex)\n        {\n            Console.WriteLine(ex.Message);\n        }\n\n        return spNewList;\n    }\n\n    \/\/\/ &lt;summary>\n    \/\/\/ This function is used to copy the fields\/columns from source list to\n    \/\/\/ destination list\n    \/\/\/ &lt;\/summary>\n    \/\/\/ &lt;param name=\"ctx\">&lt;\/param>\n    \/\/\/ &lt;param name=\"spSrcList\">&lt;\/param>\n    \/\/\/ &lt;param name=\"spNewList\">&lt;\/param>\n    \/\/\/ &lt;returns>&lt;\/returns>\n    private static FieldCollection CopySPListFields(ClientContext ctx, List spSrcList, List spNewList)\n    {\n        #region [Variables]\n        FieldCollection spFieldColl = null;\n        string xmlValue = string.Empty;\n        #endregion\n\n        try\n        {\n            spFieldColl = spSrcList.Fields;\n\n            ctx.Load(spFieldColl);\n            ctx.ExecuteQuery();\n\n            foreach (Field field in spFieldColl)\n            {\n                try\n                {\n                    if (!field.Hidden)\n                    {\n                        xmlValue = field.SchemaXml;\n                        spNewList.Fields.AddFieldAsXml(xmlValue, true, AddFieldOptions.AddToDefaultContentType);\n                        spNewList.Update();\n                        ctx.ExecuteQuery();\n                    }\n                }\n                catch (Exception e)\n                {\n                    Console.WriteLine(e);\n                }\n            }\n        }\n        catch (Exception ex)\n        {\n            Console.WriteLine(ex);\n        }\n\n        return spFieldColl;\n    }\n\n    \/\/\/ &lt;summary>\n    \/\/\/ This function is used to create the List items in destination list\n    \/\/\/ &lt;\/summary>\n    \/\/\/ &lt;param name=\"ctx\">&lt;\/param>\n    \/\/\/ &lt;param name=\"spSrcList\">&lt;\/param>\n    \/\/\/ &lt;param name=\"spNewList\">&lt;\/param>\n    \/\/\/ &lt;param name=\"spFieldColl\">&lt;\/param>\n    private static void CreateSPListItems(ClientContext ctx, List spSrcList, List spNewList, FieldCollection spFieldColl)\n    {\n        #region [Variables]\n        CamlQuery spCamlQuery = null;\n        ListItemCollection spListItems = null;\n        ListItemCreationInformation spItemCreateInfo = null;\n        ListItem spNewItem = null;\n        #endregion\n\n        try\n        {\n            spCamlQuery = new CamlQuery();\n            spCamlQuery.ViewXml = \"&lt;View\/>\";\n\n            spListItems = spSrcList.GetItems(spCamlQuery);\n            ctx.Load(spListItems);\n            ctx.ExecuteQuery();\n\n            foreach (ListItem item in spListItems)\n            {\n                try\n                {\n                    spItemCreateInfo = new ListItemCreationInformation();\n                    spNewItem = spNewList.AddItem(spItemCreateInfo);\n                    spNewItem[\"Title\"] = item[\"Title\"];\n                    spNewItem.Update();\n                    ctx.ExecuteQuery();\n                    foreach (Field field in spFieldColl)\n                    {\n                        try\n                        {\n                            if (!field.Hidden)\n                            {\n                                spNewItem[field.Title] = item[field.Title];\n                                spNewItem.Update();\n                            }\n                        }\n                        catch (Exception e)\n                        {\n                            Console.WriteLine(e);\n                        }\n                    }\n                    ctx.ExecuteQuery();\n                }\n                catch (Exception e)\n                {\n                    Console.WriteLine(e);\n                }\n            }            \n        }\n        catch (Exception ex)\n        {\n            Console.WriteLine(ex);\n        }\n     }\n  }\n}<\/code><\/pre>\n\n\n\n<p>     After running the code, we can see the destination list is created in the site content of the Site which we have provided while running the program. <\/p>\n\n\n\n<p><strong><span style=\"text-decoration: underline;\"><em>Figure 1<\/em><\/span><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.softreetechnology.com\/wp-content\/uploads\/2020\/06\/2.png\" alt=\"\" class=\"wp-image-6141\" width=\"545\" height=\"323\"\/><\/figure>\n\n\n\n<p><strong><em>Figure 2<\/em><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.softreetechnology.com\/wp-content\/uploads\/2020\/06\/3.png\" alt=\"\" class=\"wp-image-6142\" width=\"622\" height=\"330\"\/><\/figure>\n\n\n\n<p><strong>Figure1:<\/strong> &#8211; This figure shows how the source list was created.<\/p>\n\n\n\n<p><strong>Figure2: <\/strong>-This figure shows how the destination list is created after running the above code.<\/p>\n\n\n\n<p><span style=\"text-decoration: underline;\"><strong><em>Keywords:<\/em><\/strong><\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Copying List, list fields and list Items using CSOM.<\/li><li>How to copy a List within a single site using CSOM.<\/li><li>Copying a list, list items &amp; list fields of SharePoint site using CSOM.<\/li><li>Clone a list within a SharePoint site using .Net client object model (CSOM). <\/li><li>Programmatically how to copy list, list items &amp; list field using CSOM.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this SharePoint blog, we are going to program how we can clone\/copy a list, list fields and list Items within a single site programmatically using .Net client object model (CSOM) in SharePoint Online. Before we start, we need to create a console application in Visual Studio. We must refer the piece of code &amp; snapshot provided below for better [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-6139","post","type-post","status-publish","format-standard","hentry","category-sharepoint"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Copying List, list fields and list Items using CSOM - 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\/copying-list-list-fields-and-list-items-using-csom\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Copying List, list fields and list Items using CSOM - Softree Technology\" \/>\n<meta property=\"og:description\" content=\"In this SharePoint blog, we are going to program how we can clone\/copy a list, list fields and list Items within a single site programmatically using .Net client object model (CSOM) in SharePoint Online. Before we start, we need to create a console application in Visual Studio. We must refer the piece of code &amp; snapshot provided below for better [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/softreetechnology.com\/blog\/sharepoint\/copying-list-list-fields-and-list-items-using-csom\/\" \/>\n<meta property=\"og:site_name\" content=\"Softree Technology\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-08T09:42:27+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=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Copying List, list fields and list Items using CSOM - 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\/copying-list-list-fields-and-list-items-using-csom\/","og_locale":"en_US","og_type":"article","og_title":"Copying List, list fields and list Items using CSOM - Softree Technology","og_description":"In this SharePoint blog, we are going to program how we can clone\/copy a list, list fields and list Items within a single site programmatically using .Net client object model (CSOM) in SharePoint Online. Before we start, we need to create a console application in Visual Studio. We must refer the piece of code &amp; snapshot provided below for better [&hellip;]","og_url":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copying-list-list-fields-and-list-items-using-csom\/","og_site_name":"Softree Technology","article_published_time":"2020-06-08T09:42:27+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copying-list-list-fields-and-list-items-using-csom\/#article","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copying-list-list-fields-and-list-items-using-csom\/"},"author":{"name":"admin","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b"},"headline":"Copying List, list fields and list Items using CSOM","datePublished":"2020-06-08T09:42:27+00:00","mainEntityOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copying-list-list-fields-and-list-items-using-csom\/"},"wordCount":269,"commentCount":0,"publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copying-list-list-fields-and-list-items-using-csom\/#primaryimage"},"thumbnailUrl":"https:\/\/www.softreetechnology.com\/wp-content\/uploads\/2020\/06\/1.png","articleSection":["SharePoint"],"inLanguage":"en","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/softreetechnology.com\/blog\/sharepoint\/copying-list-list-fields-and-list-items-using-csom\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copying-list-list-fields-and-list-items-using-csom\/","url":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copying-list-list-fields-and-list-items-using-csom\/","name":"Copying List, list fields and list Items using CSOM - Softree Technology","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copying-list-list-fields-and-list-items-using-csom\/#primaryimage"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copying-list-list-fields-and-list-items-using-csom\/#primaryimage"},"thumbnailUrl":"https:\/\/www.softreetechnology.com\/wp-content\/uploads\/2020\/06\/1.png","datePublished":"2020-06-08T09:42:27+00:00","breadcrumb":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copying-list-list-fields-and-list-items-using-csom\/#breadcrumb"},"inLanguage":"en","potentialAction":[{"@type":"ReadAction","target":["https:\/\/softreetechnology.com\/blog\/sharepoint\/copying-list-list-fields-and-list-items-using-csom\/"]}]},{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copying-list-list-fields-and-list-items-using-csom\/#primaryimage","url":"https:\/\/www.softreetechnology.com\/wp-content\/uploads\/2020\/06\/1.png","contentUrl":"https:\/\/www.softreetechnology.com\/wp-content\/uploads\/2020\/06\/1.png"},{"@type":"BreadcrumbList","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/copying-list-list-fields-and-list-items-using-csom\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/softreetechnology.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Copying List, list fields and list Items using CSOM"}]},{"@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\/6139","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=6139"}],"version-history":[{"count":0,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/6139\/revisions"}],"wp:attachment":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=6139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=6139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=6139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}