Learn About Copying List, List Fields And List Items Using CSOM

In this SharePoint blog, we are going to see 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 to the piece of code & snapshot provided below for better understanding of the operation of program.
 
Step 1
 
First we need to input Site URL & the credentials for site and the List name which we want to copy.
 
Learn About Copying List, List Fields And List Items Using CSOM
 
Step 2
 
Set the credentials for site.
 
Step 3
 
Creating the destination List by referring to the base ID of source list.
 
Step 4
 
Creating the field for destination list and adding the field value from the field collection of source list.
 
Step 5
 
Retrieving all the items from the source list and adding it to the corresponding field of destination list.
  1. static void Main(string[] args) {  
  2.     #region[Variables]  
  3.     string siteURL = string.Empty;  
  4.     string username = string.Empty;  
  5.     string password = string.Empty;  
  6.     string sorList = string.Empty;  
  7.     string newList = string.Empty;  
  8.     #endregion  
  9.     #region[Constant Variables]  
  10.     const string ctSiteURL = "Provide a valid source Site URL: ";  
  11.     const string ctUsername = "Enter username: ";  
  12.     const string ctPassword = "Enter password: ";  
  13.     const string ctSorList = "Enter the exist source Listname: ";  
  14.     const string ctNewList = "Enter the new Listname you want to create: ";  
  15.     #endregion  
  16.     #region[SP Variables]  
  17.     Web spWeb = null;  
  18.     List spSrcList = null;  
  19.     List spNewList = null;  
  20.     FieldCollection spFieldColl = null;  
  21.     #endregion  
  22.     #region[ConsoleMessages]  
  23.     Console.Write(ctSiteURL);  
  24.     siteURL = Console.ReadLine();  
  25.     Console.Write(ctUsername);  
  26.     username = Console.ReadLine();  
  27.     Console.Write(ctPassword);  
  28.     password = Console.ReadLine();  
  29.     Console.Write(ctSorList);  
  30.     sorList = Console.ReadLine();  
  31.     Console.Write(ctNewList);  
  32.     newList = Console.ReadLine();  
  33.     #endregion  
  34.     using(ClientContext ctx = new ClientContext(siteURL)) {  
  35.         SecureString securstr = new SecureString();  
  36.         foreach(char ch in password) {  
  37.             securstr.AppendChar(ch);  
  38.         }  
  39.         ctx.Credentials = new SharePointOnlineCredentials(username, securstr);  
  40.         spWeb = ctx.Web;  
  41.         ctx.Load(ctx.Web);  
  42.         spSrcList = ctx.Web.Lists.GetByTitle(sorList);  
  43.         ctx.Load(spSrcList);  
  44.         ctx.ExecuteQuery();  
  45.         spNewList = CreateSPList(ctx, spWeb, spSrcList, newList);  
  46.         spFieldColl = CopySPListFields(ctx, spSrcList, spNewList);  
  47.         CreateSPListItems(ctx, spSrcList, spNewList, spFieldColl);  
  48.         Console.WriteLine("The list" + " " + newList + " " + "Translated Successfully");  
  49.         Console.ReadLine();  
  50.     }  
  51. }  
  52. /// <summary>  
  53. /// This function is used to create the list in destination site  
  54. /// based on source site list information  
  55. /// </summary>  
  56. /// <param name="ctx"></param>  
  57. /// <param name="web"></param>  
  58. /// <param name="srcList"></param>  
  59. /// <param name="newList"></param>  
  60. /// <returns></returns>  
  61. private static List CreateSPList(ClientContext ctx, Web web, List srcList, string newList) {  
  62.     #region[Variables]  
  63.     ListCreationInformation destList = null;  
  64.     List spNewList = null;  
  65.     int baseid = -1;  
  66.     #endregion  
  67.     try {  
  68.         baseid = srcList.BaseTemplate;  
  69.         destList = new ListCreationInformation();  
  70.         destList.Title = newList;  
  71.         destList.TemplateType = baseid;  
  72.         web.Lists.Add(destList);  
  73.         spNewList = ctx.Web.Lists.GetByTitle(destList.Title);  
  74.         ctx.Load(srcList);  
  75.         ctx.ExecuteQuery();  
  76.     } catch (Exception ex) {  
  77.         Console.WriteLine(ex.Message);  
  78.     }  
  79.     return spNewList;  
  80. }  
  81. /// <summary>  
  82. /// This function is used to copy the fields/columns from source list to  
  83. /// destination list  
  84. /// </summary>  
  85. /// <param name="ctx"></param>  
  86. /// <param name="spSrcList"></param>  
  87. /// <param name="spNewList"></param>  
  88. /// <returns></returns>  
  89. private static FieldCollection CopySPListFields(ClientContext ctx, List spSrcList, List spNewList) {  
  90.     #region[Variables]  
  91.     FieldCollection spFieldColl = null;  
  92.     string xmlValue = string.Empty;  
  93.     #endregion  
  94.     try {  
  95.         spFieldColl = spSrcList.Fields;  
  96.         ctx.Load(spFieldColl);  
  97.         ctx.ExecuteQuery();  
  98.         foreach(Field field in spFieldColl) {  
  99.             try {  
  100.                 if (!field.Hidden) {  
  101.                     xmlValue = field.SchemaXml;  
  102.                     spNewList.Fields.AddFieldAsXml(xmlValue, true, AddFieldOptions.AddToDefaultContentType);  
  103.                     spNewList.Update();  
  104.                     ctx.ExecuteQuery();  
  105.                 }  
  106.             } catch (Exception e) {  
  107.                 Console.WriteLine(e);  
  108.             }  
  109.         }  
  110.     } catch (Exception ex) {  
  111.         Console.WriteLine(ex);  
  112.     }  
  113.     return spFieldColl;  
  114. }  
  115. /// <summary>  
  116. /// This function is used to create the List items in destination list  
  117. /// </summary>  
  118. /// <param name="ctx"></param>  
  119. /// <param name="spSrcList"></param>  
  120. /// <param name="spNewList"></param>  
  121. /// <param name="spFieldColl"></param>  
  122. private static void CreateSPListItems(ClientContext ctx, List spSrcList, List spNewList, FieldCollection spFieldColl) {  
  123.     #region[Variables]  
  124.     CamlQuery spCamlQuery = null;  
  125.     ListItemCollection spListItems = null;  
  126.     ListItemCreationInformation spItemCreateInfo = null;  
  127.     ListItem spNewItem = null;  
  128.     #endregion  
  129.     try {  
  130.         spCamlQuery = new CamlQuery();  
  131.         spCamlQuery.ViewXml = "<View/>";  
  132.         spListItems = spSrcList.GetItems(spCamlQuery);  
  133.         ctx.Load(spListItems);  
  134.         ctx.ExecuteQuery();  
  135.         foreach(ListItem item in spListItems) {  
  136.             try {  
  137.                 spItemCreateInfo = new ListItemCreationInformation();  
  138.                 spNewItem = spNewList.AddItem(spItemCreateInfo);  
  139.                 spNewItem["Title"] = item["Title"];  
  140.                 spNewItem.Update();  
  141.                 ctx.ExecuteQuery();  
  142.                 foreach(Field field in spFieldColl) {  
  143.                     try {  
  144.                         if (!field.Hidden) {  
  145.                             spNewItem[field.Title] = item[field.Title];  
  146.                             spNewItem.Update();  
  147.                         }  
  148.                     } catch (Exception e) {  
  149.                         Console.WriteLine(e);  
  150.                     }  
  151.                 }  
  152.                 ctx.ExecuteQuery();  
  153.             } catch (Exception e) {  
  154.                 Console.WriteLine(e);  
  155.             }  
  156.         }  
  157.     } catch (Exception ex) {  
  158.         Console.WriteLine(ex);  
  159.     }  
  160.   }  
  161.  }  
  162. }  
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.
 
Figure-1
Learn About Copying List, List Fields And List Items Using CSOM 
Figure-2
 
Learn About Copying List, List Fields And List Items Using CSOM
 
Figure 1 - This figure shows how the source list was created.
 
Figure 2 - This figure shows how the destination list is created after running the above code.