Move a SPListItem without loosing its ItemId – Custom List

Once I got a requirement to move a list item from one folder location to another within the same list (e.g. move the item to a sibling) while retaining the same ItemID.

Example:

Here is the original structure...

List 1
Folder 1
Item A
Folder 2

We need to move Item A to Folder 2 also the ItemID need be retained.

List 1
Folder 1
Folder 2
Item A

If it is a custom list item ( in list template ID 100 – custom list) then through code we can’t copy the item using SPListItem.CopyTo() method and that information is added in the community content in our MSDN SDK article : http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.copyto.aspx , we can see some people already mentioned about that concern so I hope this solution will be a work-around for that method as well.

We can work-around it in another way , export the Item A from the List and then import it to the same list after deleting & re-parenting the item. You can copy paste this code to a .NET console based application and modify the necessary stuffs. Also this way you can retain the ID of the list item as well.

    1: using System;
    2: using System.Collections.Generic;
    3: using System.Text;
    4: using Microsoft.SharePoint;
    5: using Microsoft.SharePoint.Deployment;
    6: namespace MoveItem
    7: {
    8:     class Program
    9:     {
   10:         static string siteUrl = "http://site";
   11:  
   12:         static void Main(string[] args)
   13:         {
   14:             string exportFolder = @"C:\export\";
   15:             string exportFileName = "Export_File.cmp";
   16:  
   17:             using (SPSite site = new SPSite(siteUrl))
   18:             {
   19:                 using (SPWeb oWeb = site.OpenWeb())
   20:                 {
   21:                     SPList oList = oWeb.Lists["MyList"];
   22:  
   23:                     SPListItem oItemA = oList.GetItemById(7);// ItemA
   24:  
   25:                     SPExportObject exportObject = new SPExportObject();
   26:                     exportObject.Id = oItemA.UniqueId;
   27:                     exportObject.Type = SPDeploymentObjectType.ListItem;
   28:  
   29:                     SPExportSettings exportSettings = new SPExportSettings();
   30:                     exportSettings.LogExportObjectsTable = true;
   31:                     exportSettings.LogFilePath = @"C:\export\log.log";
   32:                     exportSettings.SiteUrl = siteUrl;
   33:                     exportSettings.ExportMethod = SPExportMethodType.ExportAll;
   34:                     exportSettings.FileLocation = exportFolder;
   35:                     exportSettings.BaseFileName = exportFileName;                    
   36:                     exportSettings.ExportObjects.Add(exportObject);
   37:  
   38:                     SPExport export = new SPExport(exportSettings);
   39:                     export.Run();
   40:  
   41:                     // delete the item from Folder 1
   42:                     oItemA.Delete();                    
   43:  
   44:                     SPImportSettings settings = new SPImportSettings();
   45:                     settings.SiteUrl = siteUrl;
   46:                     settings.WebUrl = siteUrl;
   47:                     settings.FileLocation = @"c:\export";
   48:                     settings.LogFilePath = @"C:\export\log.log";
   49:                     settings.RetainObjectIdentity = false;
   50:                     settings.BaseFileName = exportFileName;                    
   51:  
   52:                     SPImport import = new SPImport(settings);
   53:   // handle the import event to re-parent the item
   54:                     import.Started += new EventHandler<SPDeploymentEventArgs>(import_Started);
   55:                     import.Run();
   56:                 }
   57:             }
   58:                 
   59:             }
   60:  
   61:         static void import_Started(object sender, SPDeploymentEventArgs e)
   62:         {
   63:           
   64:             using (SPSite site = new SPSite(siteUrl))
   65:             {
   66:                 using (SPWeb oWeb = site.OpenWeb())
   67:                 {
   68:                     SPList oList = oWeb.Lists["MyList"];
   69:  
   70:                     foreach (SPImportObject io in e.RootObjects) 
   71:                    { 
   72:                       if (io.Type == SPDeploymentObjectType.ListItem)               
   73:                       {
   74:                           io.TargetParentUrl = oList.RootFolder.ServerRelativeUrl + "/Folder2/";  
   75:                           
   76:                       }                                                                                                                                           
   77:                    } 
   78:  
   79:                 }
   80:             }
   81:         }
   82:     }
   83: }
   84: