Copy list items from one site collection to another programmatically using CSOM in SharePoint Online

sharepoint online copy list to another site collection

In this SharePoint tutorial, I am going to explain, how to copy list items from one site collection to another site collection programmatically using .Net client object model (csom) in SharePoint Online.

Before we start, we need to create a console application in Visual Studio. Here I am using Visual Studio 2010 but you can do the same thing in higher version of Visual Studio.


SharePoint Online copy list to another site collection using CSOM

Step 1: Open your Visual Studio

Step 2: Create a new console application and enter your solution Name and save it in your any drive location.

sharepoint online copy list items to another site collection
sharepoint online copy list to another site collection

Step 3: Before we start , we need to add the SharePoint client object model dll in our application. For adding the dll we can add the dll manually under references or we can download it from NuGet gallery.

Step 4: For download the dll from NuGet , we have to go the manage NuGet packages under Tool in Visual Studio.

sharepoint online copy list items to another site collection programmatically
sharepoint online copy list to another site collection programmatically

Step 5: Download the references which is required to connect SharePoint online. Below is the screenshot of reference file which is required to download.

sharepoint online copy list to another site collection csom
sharepoint online copy list to another site collection csom

Step 6: Next click on Install button. Once it will done, we will get an screen to select the project.

sharepoint online copy list to another site collection programmatically csom
sharepoint online copy list to another site collection programmatically csom

Step 7: Once you click on OK button , again you will get an screen to click Accept or Decline. Please click on Accept button to go forward.

sharepoint copy list items to another list
sharepoint copy list items to another list

Step 8: Once you click on Accept button, Download will start and you will get your all references inside reference file in your application.

sharepoint copy list items to another list
sharepoint copy list items to another list

Step 9: Next open your Program.cs file and write the below code under Main function.

using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace getDataFromSharePointOnline
{
    class Program
    {
        static void Main(string[] args)
        {
            string userName = "rswain@consultants.km.qa";
            Console.WriteLine("Enter Your Password Please  ------- ");
            SecureString password = GetPasswordOfYourSite();
            // ClienContext - Get the context for the SharePoint Online Site  
            ClientContext contextDestination = new ClientContext("https://pikasha12.sharepoint.com/sites/Project/");
            List destList = contextDestination.Web.Lists.GetByTitle("DestTrackingList");
            ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                       
            using (var clientContext = new
            ClientContext("https://pikasha12.sharepoint.com/sites/DLH/"))
            {
                // SharePoint Online Credentials  
                clientContext.Credentials = new SharePointOnlineCredentials(userName, password);
                // Get the SharePoint web  
                Web web = clientContext.Web;
                List lst = web.Lists.GetByTitle("SourceTrackingList");
                CamlQuery cq = CamlQuery.CreateAllItemsQuery(100);
                ListItemCollection lic = lst.GetItems(cq);
                clientContext.Load(lic);
                // Load the Web properties            
                clientContext.ExecuteQuery();
                foreach (ListItem SourcelistItem in lic)
                {
                    // We have all the list item data. For example, Title.      
                    ListItem DestnewItem = destList.AddItem(itemCreateInfo);
                    DestnewItem["Title"] = SourcelistItem["Title"];
					DestnewItem["Name"] = SourcelistItem["Name"];
					DestnewItem["PhNo"] = SourcelistItem["PhNo"];
                  
                    DestnewItem.Update();
                   
                }
                contextDestination.ExecuteQuery();
            }
           
        }
       
        private static SecureString GetPasswordOfYourSite()
        {
            ConsoleKeyInfo info;
            //Get the user's password as a SecureString  
            SecureString securePassword = new SecureString();
            do
            {
                info = Console.ReadKey(true);
                if (info.Key != ConsoleKey.Enter)
                {
                    securePassword.AppendChar(info.KeyChar);
                }
            }
            while (info.Key != ConsoleKey.Enter);
            return securePassword;
        }
    }
}

Step 10 : Test the application:

Hit F5 to run the application. Enter the password and then click on Enter button.

Source List

sharepoint online copy list to another site collection
sharepoint online copy list to another site collection

Destination List

sharepoint online copy list to another site collection
sharepoint online copy list to another site collection

You may like following SharePoint csom tutorials:

Hopefully, this SharePoint online tutorial helps to learn how to copy list items from one list to another list programmatically using csom in SharePoint online.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Rajkiran Swain

Rajkiran is currently working as a SharePoint Consultant in QATAR . Rajkiran having 6 + years of experience in Microsoft Technologies such as SharePoint 2019/2016/2013/2010, MOSS 2007,WSS 3.0, Migration, Asp.Net, C#.Net, Sql Server, Ajax, jQuery etc.He is C#Corner MVP (2 Times).

View all posts by Rajkiran Swain →