SharePoint Online copy list items to another list programmatically (C#.net)

This SharePoint tutorial explains SharePoint online copy list items to another list programmatically. We will see how to CSOM SharePoint to copy list items to another list in SharePoint online.

In this particular example, we will do this inside 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

Copy SharePoint list items to another list

Here I have a 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:

sharepoint 2013 copy items from one list to another csom
SharePoint Copy items

Here we will move these items to another list in the same site which has same column names.

Below is the code to copy SharePoint list items to another list programmatically using C#.Net.

public static void CopyItemsFromOneListToAnotherList()
{

using (ClientContext ctx = new ClientContext(“https://onlysharepoint2013.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 string GetSPOAccountName()
{
try
{
return ConfigurationManager.AppSettings[“SPOAccount”];
}
catch
{
throw;
}
}
private static SecureString GetSPOSecureStringPassword()
{
try
{
var secureString = new SecureString();
foreach (char c in ConfigurationManager.AppSettings[“SPOPassword”])
{
secureString.AppendChar(c);
}


return secureString;
}
catch
{
throw;
}
}

Once you run the above code, it will copy the items like below:

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

Read some SharePoint CSOM examples:

I hope this will be helpful to you to copy SharePoint list items to another list programmatically using CSOM in SharePoint Online/2013/2016.

  • copy list items to another list sharepoint online
  • copy list items to another list sharepoint 2013
  • copy list items to another list c#
  • copy list items to another list sharepoint
  • sharepoint online copy list items to another list programmatically
>