How copy items from one list to another list using csom .Net managed object model code in SharePoint online?

copy items from one list to another list using csom

Here we will discuss how to copy items from one list to another list using csom .Net managed object model code in SharePoint online. Here we will do this inside a console application and we will try to connect to 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


Details:
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 online copy items from one list to another list using csom
SharePoint online copy items from one list to another list using csom

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

Below is the code:

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:

copy items from one list to another list using csom
copy items from one list to another list using csom

Read some sharepoint online tutorials:

Hope this will be helpful.

Check out Best Alternative to InfoPath -> Try Now

EnjoySharePoint YouTube Channel

About 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"

View all posts by Bhawana Rathore →