In this post we will learn how to copy list items from one list to another list using Console Application Program.
I have created two Sharepoint sites, first is(source Site) http://URl/rf and the second(destination site) http://URl/rf
I have also created custom list in each site, Source List in site http://URl/rf and Desitination List in http://URl/rf
Create two custom lists Source List and destination List:
Source List Name "NewEmpMasterList" (below are columns and column types)
Employee Id (Single line of text)
Employee Name (Single line of text)
Stages (Choice),Department .Designation ,Date of joining ,Official email ID ,Date of birth , Approver Name ,Permanent address ,Address for communication.........................ext
Destination List "Exit EMP List" (below are columns and column types)
Employee Id (Single line of text)
Employee Name (Single line of text)
Stages (Choice),Department .Designation ,Date of joining ,Official email ID ,Date of birth , Approver Name ,Permanent address ,Address for communication.........................ext
After creating the two lists:
Here in this scenario I have chosen Console Application template because it doens’t contains any input fields to show as webpart and also to avoid GAC registration, safe controls etc., I felt it will be easy to execute the code in Console.
If you have Lookup column in your Source List and want to copy the same data in to Destination List, you have to create an instance for SPFieldLookupValue class like below.
Let’s suppose ‘Employee Name’ column is lookup field, comment or remove the line(string SEmpName = item["Employee Name"].ToString();) in the below code and replace with below code
SPFieldLookupValue SLookupEmpName = new SPFieldLookupValue(item["Employee Name"].ToString());
string SEmpName = SLookupEmpName.LookupId.ToString();
Click here to Download Code
//Copy Sharepoint list items(selected fields) from Source List to Destination
List programmatically(Rajkumar kaki)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace CopyEMPListItems
{
class
CopyEmployeeMasterList
{
static void
Main(string[] args)
{
//Source
Site
using
(SPSite site = new SPSite("http://URl/rf"))
{
using
(SPWeb web = site.OpenWeb())
{
int count = 0;
//Source List Name
SPList mySourceList = web.Lists["NewEmpMasterList"];
foreach (SPListItem item in mySourceList.Items)
{
try
{
string active = item["Active"].ToString();
string stages = item["Stages"].ToString();
if (active == "No"
|| stages == "Stage 4")
{
try
{
string
SourceEmpId = item["Employee ID"].ToString();
string SEmpName =
item["Employee name"].ToString();
string
SDepartment = item["Department"].ToString();
string
SDesi = item["Designation"].ToString();
string SDOJ =
item["Date of joining"].ToString();
string
SEmailID = item["Official email ID"].ToString();
string SDOB
= item["Date of birth"].ToString();
string
SApproverName = item["Approver Name"].ToString();
string
SPerAddress = item["Permanent address"].ToString();
string
SAForCommu = item["Address for communication"].ToString();
if
(item["Category"] == null)
{
if
(SDepartment == "BPO")
item["Category"] = "BPO";
if
(SDepartment == "US Payroll" || SDepartment == "US
Staffing")
item["Category"] = "US";
else
item["Category"] = "Development";
}
string
SCategory = item["Category"].ToString();
if
(item["Employee base"] == null)
{
if
(SDepartment == "BPO")
item["Employee base"] = "ADC";
else
item["Employee base"] = "KDC";
}
string
SEmpBase = item["Employee base"].ToString();
if
(item["Stages"] == null)
item["Stages"] = "Stage 4";
string
SStages = item["Stages"].ToString();
if
(item["Contact number"] == null)
item["Contact number"] = "0000000000";
string SContactNo =
item["Contact number"].ToString();
if
(item["Current designation"] == null)
item["Current designation"] = "Exit Employee";
string SCurDesi =
item["Current designation"].ToString();
if
(item["Education qualification"] == null)
item["Education qualification"] = "Exit Employee";
string
SEducQua = item["Education qualification"].ToString();
if
(item["Approver Email"] == null)
item["Approver Email"] = SApproverName + "@preludesys.com";
string
SAppEmail = item["Approver Email"].ToString();
if
(item["Approver Emp ID"] == null)
item["Approver Emp ID"] = SApproverName;
string
SAppEmpID = item["Approver Emp ID"].ToString();
if
(item["Active"] == null)
item["Active"] = "No";
string SActive =
item["Active"].ToString();
if
(item["Date of confirmation"] == null)
item["Date of confirmation"] = SDOJ;
string SDOC
= item["Date of confirmation"].ToString();
if
(item["Casual Leave"] == null)
item["Casual Leave"] = "0";
string
SCasualL = item["Casual Leave"].ToString();
if
(item["Father Name"] == null)
item["Father Name"] = SEmpName;
string
SFatherName = item["Father Name"].ToString();
//Destination Site
using
(SPSite myDestinationSite = new SPSite("http://URl/rf"))
{
using
(SPWeb myDestinationWeb = myDestinationSite.OpenWeb())
{
try
{
//Destination List
Name
SPList myDestinationList = myDestinationWeb.Lists["Exit EMP
List"];
SPListItem myDestinationListItem = myDestinationList.Items.Add();
myDestinationListItem["Employee ID"] = SourceEmpId;
myDestinationListItem["Employee name"] = SEmpName;
myDestinationListItem["Category"] = SCategory;
myDestinationListItem["Department"] = SDepartment;
myDestinationListItem["Designation"]
= SDesi;
myDestinationListItem["Date of joining"] = SDOJ;
myDestinationListItem["Stages"] = SStages;
myDestinationListItem["Official
email ID"] = SEmailID;
myDestinationListItem["Contact number"] = SContactNo;
myDestinationListItem["Employee base"] = SEmpBase;
myDestinationListItem["Date of birth"] = SDOB;
myDestinationListItem["Current designation"] = SCurDesi;
myDestinationListItem["Education
qualification"] = SEducQua;
myDestinationListItem["Approver Email"] = SAppEmail;
myDestinationListItem["Approver Emp ID"] = SAppEmpID;
myDestinationListItem["Active"] = SActive;
myDestinationListItem["Date of confirmation"] = SDOC;
myDestinationListItem["Father Name"] = SFatherName;
myDestinationListItem["Casual Leave"] = SCasualL;
myDestinationListItem["Approver Name"] = SApproverName;
myDestinationListItem["Address for communication"] =
SAForCommu;
myDestinationListItem["Permanent address"] = SPerAddress;
myDestinationWeb.AllowUnsafeUpdates = true;
myDestinationListItem.Update();
myDestinationWeb.AllowUnsafeUpdates = false;
//Counting No Of List Items
count++;
Console.WriteLine(count + " item(s) copied");
}
catch (Exception){}
}
}
}
catch
(Exception) {}
}
}
catch (Exception) {}
}
}
}
}
}
}
ofter run the program
ofter run the program in my Destination List 383 items copied form Source List
Copy SharePoint List items from Source List to another List(Destination List), using programmatically with Attachments
ReplyDeleteusing (SPSite site = new SPSite("http://URL"))
{
using (SPWeb web = site.OpenWeb())
{
int i = 0;
SPList sourceList = web.Lists["Source List"];
foreach (SPListItem sourceItem in sourceList.Items)
{
SPList destinationList = web.Lists["Destination List"];
if (destinationList != null)
{
SPListItem destItem = destinationList.Items.Add();
foreach (SPField field in sourceItem.Fields)
{
if (!field.ReadOnlyField && !field.Hidden && field.InternalName != "Attachments")
{
if (destItem.Fields.ContainsField(field.InternalName))
{
destItem[field.InternalName] = sourceItem[field.InternalName];
}
}
}
foreach (string fileName in sourceItem.Attachments)
{
SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + fileName);
byte[] imageData = file.OpenBinary();
destItem.Attachments.Add(fileName, imageData);
}
i++;
destItem.Update();
}
}
}