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

This SharePoint csom tutorial, we will discuss, 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:

copy list items from one list to another in sharepoint programmatically

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:

copy list items to another list sharepoint

You may like following SharePoint CSOM tutorials:

This SharePoint tutorial we discussed, how to copy list items from one list to another in sharepoint programmatically using csom (.Net managed object model code).

free sharepoint training

SharePoint Online FREE Training

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

envelope
envelope

Bhawana Rathore

Hello Everyone!! I am Bhawana a SharePoint MVP and having about 8+ years of SharePoint experience as well as in .Net technologies . I have worked in all the versions of SharePoint from wss to Office 365. I have good exposure in Customization and Migration using Nintex, Metalogix tools . Now exploring more in SharePoint 2016 :) Hope here I can contribute and share my knowledge to the fullest. As I believe "There is no wealth like knowledge, and no poverty like ignorance"

  • […] copy items from one list to another list using csom .Net managed object model code in SharePoint […]

  • >