[SharePoint 2010] Copy list item with version history and attachment

 1         private void MoveItem(SPListItem sourceItem, SPListItem destinationItem)
 2         {
 3             if (sourceItem == null || destinationItem == null) return;
 4 
 5             destinationItem = GetDestinationItem(sourceItem.ID, destinationItem);
 6 
 7             SPFieldLookup lkpField = null;
 8             SPFieldLookupValue lkpFieldVal=null;
 9             object fieldVal = null;
10             SPUser systemAccount = destinationItem.Web.AllUsers["SHAREPOINT/system"];
11             SPWeb sourceWeb = sourceItem.Web;
12             SPWeb destWeb = destinationItem.Web;
13 
14             Log("-----Begin of handle item-----");
15 
16             Log(string.Format("----------Source item [{0}/{1}]----------", sourceItem.Title, sourceItem.ID));
17 
18             #region Handle version history
19 
20             for (var i = sourceItem.Versions.Count - 1; i >= 0; i--)
21             {
22                 foreach (SPField sourceField in sourceItem.Fields)
23                 {
24                     SPListItemVersion version = sourceItem.Versions[i];
25                     fieldVal = version[sourceField.InternalName];
26 
27                     Log(string.Format("----------Handle field[{0}/{1}]----------", sourceField.InternalName, sourceField.Type.ToString()));
28 
29                     if (sourceField.Type == SPFieldType.User)
30                     {
31                         fieldVal = GetSPUserFromString(new SPFieldUserValue(sourceWeb, fieldVal.ToString()), destWeb);
32                         Log(string.Format("----------user field[{0}/{1}]----------", sourceField.InternalName, fieldVal));
33                     }
34 
35                     if (sourceField.Id == SPBuiltInFieldId.Created ||
36                        sourceField.Id == SPBuiltInFieldId.Created_x0020_By ||
37                        sourceField.Id == SPBuiltInFieldId.Modified ||
38                        sourceField.Id == SPBuiltInFieldId.Modified_x0020_By ||
39                        sourceField.Id == SPBuiltInFieldId.Author ||
40                        sourceField.Id == SPBuiltInFieldId.Editor)
41                     {
42                         destinationItem[sourceField.InternalName] = fieldVal;
43                     }
44                     else if (!sourceField.ReadOnlyField && sourceField.Type != SPFieldType.Attachments)
45                     {
46                         if (sourceField.Type != SPFieldType.Lookup)
47                         {
48                             destinationItem[sourceField.InternalName] = fieldVal;
49                         }
50                         else
51                         {
52                             lkpField = sourceField as SPFieldLookup;
53 
54                             Log(string.Format("----------source field value[{0}]----------", fieldVal));
55 
56                             if (lkpField != null && fieldVal != null && fieldVal.ToString().Length > 0)
57                             {
58                                 lkpFieldVal = new SPFieldLookupValue(fieldVal.ToString());
59                                 destinationItem[sourceField.InternalName] = lkpFieldVal;
60                             }
61                         }
62                     }
63                 }
64 
65                 //update destination item
66                 destinationItem.Update();
67 
68                 Log("----------End of handle field----------");
69             }

 

 1         private SPListItem GetDestinationItem(int sourceItemID, SPListItem destinationItem)
 2         {
 3             SPList list = DestinationList;
 4 
 5             if (sourceItemID - CurrentItemID > 1)
 6             {
 7                 for (int i = CurrentItemID + 1; i < sourceItemID; i++)
 8                 {
 9                     SPListItem item = list.Items.Add();
10                     item[SPBuiltInFieldId.Title] = "-1";
11 
12                     item.Update();
13                 }
14                 return list.Items.Add();
15             }
16             else
17             {
18                 return destinationItem;
19             }
20         }

 

 1         private SPUser GetSPUserFromString(SPFieldUserValue userVal, SPWeb web)
 2         {
 3             try
 4             {
 5                 if (userVal != null)
 6                     return web.EnsureUser(userVal.User.LoginName);
 7                 else
 8                     return null;
 9             }
10             catch
11             {
12                 return null;
13             }
14         }

 

posted @ 2014-02-21 11:19  一只小小菜鸟  阅读(468)  评论(0编辑  收藏  举报