Copy/Move items from one list to another list in sharepoint
Scenario:
List AA fields: Name, Title, User Name, Religion, Qualification.
List BB fields: Name, Title, User Name, Qualification, Designation, Salary and Description
Common field names: Name, Title, user name, Qualification
Now, I want to copy the common field values from List AA to List BB.
Approach:
//Read the list item values by ID and add the common field values as new item into the another list
SPListItem spLstItemsAA = listNameAA.GetItemById(ItemID); //ItemID of List AA
SPListItem spLstItemBB = list.Items.Add();
foreach (SPListItem spLstItemAA in spLstItemsAA.ListItems)
{
for (int i = 0; i < spLstItemAA.Fields.Count; i++)
{
if ((!spLstItemBB.Fields[i].Hidden) && (!spLstItemBB.Fields[i].ReadOnlyField) &&
!(customfields.Contains(item.Fields[i].Title)))
{
spLstItemBB [item.Fields[i].Title] = spLstItemAA [item.Fields[i].Title];
}
}
}
spLstItemBB ["Salary"] = "50k";spLstItemBB ["Description"] = "sharepointquicksolutions.blogspot.in ";spLstItemBB.Update();
spLstItemBB ["Salary"] = "50k";spLstItemBB ["Description"] = "sharepointquicksolutions.blogspot.in ";spLstItemBB.Update();
Condition#1: !spLstItemBB.Fields[i].ReadOnlyField
Condition#2: !spLstItemBB.Fields[i].Hidden
The above 2 conditions are used for to skip the read only and hidden fields.
Note: By default, every sharepoint list and library will have the below fields.
No comments:
Post a Comment