Moving Documents from One Document Library to Another

As you all know I write a lot of maintenance console applications to perform upgrades on huge subsets of sites.  This week I was asked to create a console application that will cut down six documents libraries into three document libraries.  I wanted to use the CopyTo() Function, but there is a bug.  I did find a nice MoveTo() Function in SPFile.  I ended up using that code instead here is a little sample:

 //copy documents and deletes libraries
            SPList ProposalsWord = Web.Lists["Proposal Template (Word)"];
            foreach (SPListItem Item in ProposalsWord.Items)
            {
                SPFile File = Web.GetFile(Item.Url);
                try
                {
                    File.MoveTo(Web.Url + "/Sample%20Proposals%20PDF/" + Item["Name"].ToString());
                }
                catch (Exception)
                {
                }
            }
            ProposalsWord.Delete();

This piece of code loops through the items on one document library and moves them to another document library.  It is fairly simple and straight forward.  If you have any comments or questions please let me know.

Print | posted on Friday, May 30, 2008 8:57 AM

Feedback

# re: Moving Documents from One Document Library to Another

Left by Nicole at 7/10/2008 8:56 AM
Gravatar Does this move all the metadata associated with the document too? I am trying to move documents that are attached to a content type to another library that has that content type as well....

# re: Moving Documents from One Document Library to Another

Left by MOSSLover at 7/10/2008 10:38 AM
Gravatar The metadata is only moved for Office Documents. It also will not move Modified, Modified By, Created, Created By. So if you are using another document type and a different field type, then you can add the metadata after it is moved.

# re: Moving Documents from One Document Library to Another

Left by Bonney at 7/21/2008 1:48 AM
Gravatar The given code gives the following exception :
The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.

Please help me out!!

# re: Moving Documents from One Document Library to Another

Left by MOSSLover at 7/22/2008 11:14 AM
Gravatar What kind of access do you have for the library? Are you running the whole code, delete and move documents?

# re: Moving Documents from One Document Library to Another

Left by Bonney at 7/29/2008 5:35 AM
Gravatar I have over come the error :)
I had to select the 'On' option of the web application from web application general settings.

Thanx anyway.

# re: Moving Documents from One Document Library to Another

Left by Saul at 11/10/2008 4:44 PM
Gravatar You can move the metadata if you do it manually, adding the properties in a hash table.

# re: Moving Documents from One Document Library to Another

Left by paromita at 6/1/2009 5:24 AM
Gravatar hi
"Modified by" is getting changed to System Account all the time. That's a major drawback.

# re: Moving Documents from One Document Library to Another

Left by mark at 7/14/2009 4:13 PM
Gravatar not sure how / where i use this code. I only have 15 documents to move, but would like to know how anyway

# re: Moving Documents from One Document Library to Another

Left by Styxol at 11/18/2009 6:47 AM
Gravatar This method does not work if you want to move document in a document library on another Web Application for example with different port number. How to move document in this case?

# re: Moving Documents from One Document Library to Another

Left by Subhash at 2/16/2010 7:15 PM
Gravatar i found that this is moving the document but it's not maintaining the version history. Is there a way to maintain the version history as well?

Your comment:





 
 

Copyright © MOSSLover

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski