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.
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.
Step 5: Download the references which is required to connect SharePoint online. Below is the screenshot of reference file which is required to download.
Step 6: Next click on the Install button. Once it will be done, we will get a screen to select the project.
Step 7: Once you click on OK button, again you will get a screen to click Accept or Decline. Please click on Accept button to go forward.
Step 8: Once you click on Accept button, Download will start and you will get your all references inside the reference file in your application.
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 = "[email protected]";
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
Destination List
Copy list items from one list to another in sharepoint programmatically
Let us see, how to copy list items from one list to another list in SharePoint programmatically using CSOM.
Here we will use csom .Net managed object model code in SharePoint Online. To do this demo, I have created a console application and we will try to connect to the SharePoint Online site.
To work with .Net managed object model code we need to add below two dlls:
- Microsoft.SharePoint.Client.dll
- Microsoft.SharePoint.Client.Runtime.dll
Here I have a SharePoint list name as SourceList which has 3 columns like below:
- Title (Single line text)
- EmailID (Single line text)
- Address (Multiple line text)
It has few items and the list looks like below:
Here we will move these items to another list in the same site which has the same column names.
Copy list items from one list to another in sharepoint programmatically
Below is the code to copy list items from one list to another in sharepoint programmatically.
public static void CopyItemsFromOneListToAnotherList()
{
using (ClientContext ctx = new ClientContext("https://<tenantname>.sharepoint.com/sites/Bhawana/"))
{
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
ctx.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
ctx.Load(ctx.Web);
ctx.ExecuteQuery();
List sourceList= ctx.Web.Lists.GetByTitle("SourceList");
ctx.Load(sourceList);
ctx.ExecuteQuery();
List destList = ctx.Web.Lists.GetByTitle("DestinationList");
ctx.Load(sourceList);
ctx.ExecuteQuery();
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View/>";
ListItemCollection listItems = sourceList.GetItems(camlQuery);
ctx.Load(listItems);
ctx.ExecuteQuery();
foreach (ListItem item in listItems)
{
ListItemCreationInformation newItemInfo = new ListItemCreationInformation();
ListItem newItem = destList.AddItem(newItemInfo);
newItem["Title"] = item["Title"];
newItem["EmailID"] = item["EmailID"];
newItem["Address"] = item["Address"];
newItem.Update();
}
ctx.ExecuteQuery();
}
}
private static SecureString GetSPOSecureStringPassword()
{
try
{
var secureString = new SecureString();
foreach (char c in "MyPassword")
{
secureString.AppendChar(c);
}
return secureString;
}
catch
{
throw;
}
}
private static string GetSPOAccountName()
{
try
{
return "bijay@<tenantname>@onmicrosoft.com";
}
catch
{
throw;
}
}
Once you run the above code, it will copy the SharePoint list items like below:
You may like the following SharePoint csom tutorials:
- How to Use CAML Query for Boolean field for SharePoint 2013/2016/Online Object Model CSOM
- How to create index in SharePoint online list using CSOM .Net managed object model code?
- SharePoint online Read CSV file from document library using .Net managed client object model csom
- Create Update Delete a list using Client Object Model CSOM in SharePoint 2013
- Different ways to hide/disable quick edit in a list in SharePoint 2013/2016/Online
- 25+ SharePoint .Net Client Object Model Examples
- How to get access token in SharePoint Online using CSOM and use in Postman or Google Rest client
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.
Rajkiran is currently working as a SharePoint Consultant in India . Rajkiran having 7+ 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).