The below code copies entire source list item including your permissions to target list
public static SPListItem CopyItem(SPListItem sourceItem, string destinationListName)
{
//copy sourceItem to destinationList
SPList destinationList = sourceItem.Web.Lists[destinationListName];
SPListItem targetItem = destinationList.Items.Add();
foreach (SPField f in sourceItem.Fields)
{
if (!f.ReadOnlyField && f.InternalName != "Attachments")
{
targetItem[f.InternalName] = sourceItem[f.InternalName];
}
}
//copy attachments
foreach (string fileName in sourceItem.Attachments)
{
SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + fileName);
byte[] imageData = file.OpenBinary();
targetItem.Attachments.Add(fileName, imageData);
}
targetItem.SystemUpdate(false);
//Break the permissions inheritance from parent
targetItem.BreakRoleInheritance(false);
//removing permissions
while (targetItem.RoleAssignments.Count > 0)
{
targetItem.RoleAssignments.Remove(0);
}
//copying the permission set from source item
foreach (SPRoleAssignment role in sourceItem.RoleAssignments)
{
targetItem.RoleAssignments.Add(role);
}
targetItem.SystemUpdate(false);
return targetItem;
}
No comments:
Post a Comment