1

Is there a way in SharePoint 2007 to move a file from one document library to another, in the same site collection programatically? I made this post about this on StackOverflow, with a bounty not too long ago but had no success. I was hoping this community might have some more insight.

1

1 Answer 1

1

Try this,

// Get your source and destination libraries SPList source = web.GetList(web.ServerRelativeUrl + @"/SourceDocLib"); SPList destination = web.GetList(web.ServerRelativeUrl + @"/DestinationDocLib"); // Get the collection of items to move, use source.GetItems(SPQuery) if you want a subset SPListItemCollection items = items = source.Items; // Get the root folder of the destination we'll use this to add the files SPFolder folder = web.GetFolder(destination.RootFolder.Url); // Now to move the files and the metadata foreach (SPListItem item in items) { //Get the file associated with the item SPFile file = item.File; // Create a new file in the destination library with the same properties SPFile newFile = folder.Files.Add(folder.Url + "/" +file.Name, file.OpenBinary(),file.Properties,true); // Optionally copy across the created/modified metadata SPListItem newItem = newFile.Item; newItem["Editor"] = item["Editor"]; newItem["Modified"] = item["Modified"]; newItem["Author"] = item["Author"]; newItem["Created"] = item["Created"]; // UpdateOverwriteVersion() will preserve the metadata added above. newItem.UpdateOverwriteVersion(); } 

If you want to delete the originals as well use for (int i = items.Count -1; i > -1 ; i--) instead of the foreach to iterate backwards through the collection and items[i].Recycle(); or items[i].Delete(); to recycle or delete the original after you have moved it.

To move the items into subfolders replace the line SPFolder folder = web.GetFolder(destination.RootFolder.Url); with code similar to the following:

// Set the name of the new destination subfolder string newFolderName = "Sub Folder 1"; // Try to get the new subfolder so we can test to see if it exists and if not create it SPFolder folder = web.GetFolder(destination.RootFolder.Url + "/" + newFolderName); if (!folder.Exists) { // Add the new folder SPListItem folderItem = destination.Folders.Add("", SPFileSystemObjectType.Folder, newFolderName); folderItem.SystemUpdate(); // Get the folder after creating it folder = web.GetFolder(destination.RootFolder.Url + "/" + newFolderName); Console.WriteLine(@"Created folder ""{0}""", folder.Url); } 

I'll post this answer with your original post on StackOverflow as well in case people look there first.

3
  • Thanks. It's a public holiday here today so I don't have a test machine available but I'll try this at work tomorrow and see how it goes :D Commented Aug 15, 2012 at 7:57
  • This works great - however is there a way to make it move items in subfolders separately? Currently it moves them out of the subfolder (folder within the document library) and places them back in the top level of the new location Commented Aug 29, 2012 at 1:20
  • @Michael I've updated the answer with code to add items to subfolders. Commented Sep 3, 2012 at 8:24

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.