Guest User

Untitled

a guest
May 25th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. public void CopyList(SPList src)
  2. {
  3. //Copy items from source List to Destination List
  4. foreach (SPListItem item in src.Items)
  5. {
  6. if(isUnique(item.UniqueId))
  7. {
  8. foreach (SPField field in src.Fields)
  9. {
  10. try
  11. {
  12. if (!field.ReadOnlyField)
  13. newDestItem = DestinationList.Items.Add();
  14. newDestItem[field.Id] = item[field.Id];
  15. newDestItem.Update();
  16. }
  17. catch (Exception ex)
  18. {
  19. ex.ToString();
  20. }
  21. }
  22. //newDestItem["wrkspace"] = src.ParentWeb.Name;
  23. // newDestItem.Update();
  24. }
  25. DestinationList.Update();
  26. }
  27. // DestinationList.Update();
  28. }
  29.  
  30. // ** Copy the fields
  31. foreach(SPField field in sourceItem.Fields)
  32. {
  33. if (newItem.Fields.ContainsField(field.InternalName) == true &&
  34. field.ReadOnlyField == false && field.InternalName != "Attachments")
  35. {
  36. newItem[field.InternalName] = sourceItem[field.InternalName];
  37. }
  38. }
  39.  
  40. // ** Delete any existing attachments in the target item
  41. for (int i = newItem.Attachments.Count; i > 0; i-- )
  42. {
  43. newItem.Attachments.Delete(newItem.Attachments[i-1]);
  44. }
  45.  
  46. // ** Copy any attachments
  47. foreach (string fileName in sourceItem.Attachments)
  48. {
  49. SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix +
  50. fileName);
  51. byte[] imageData = file.OpenBinary();
  52. newItem.Attachments.Add(fileName, imageData);
  53. }
  54.  
  55. // ** Remember where the original was copied from so we can update it in the future
  56. newItem["_M_CopySource"] = sourceItem["FileRef"];
  57.  
  58. newItem.Update();
  59.  
  60. public void CopyList(SPList src)
  61. {
  62. //Copy items from source List to Destination List
  63. foreach (SPListItem item in src.Items)
  64. {
  65. if(isUnique(item.UniqueId))
  66. {
  67. newDestItem = DestinationList.Items.Add();
  68.  
  69. foreach (SPField field in src.Fields)
  70. {
  71. try
  72. {
  73. if ((!field.ReadOnlyField) && (field.InternalName!="Attachments"))
  74. newDestItem[field.InternalName] = item[field.InternalName];
  75. }
  76. catch (Exception ex)
  77. {
  78. //you should save the "ex" somewhere to see its outputs
  79. ex.ToString();
  80. }
  81. }
  82. newDestItem.Update(); //only now you call update!
  83. }
  84. }
  85. }
Add Comment
Please, Sign In to add comment