Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
    
See this related question: sharepoint.stackexchange.com/questions/10823/… –  goofface Aug 15 '12 at 8:26

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
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 –  ElvisLikeBear Aug 15 '12 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 –  ElvisLikeBear Aug 29 '12 at 1:20
    
@Michael I've updated the answer with code to add items to subfolders. –  Aquila Sands Sep 3 '12 at 8:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.