Geeks With Blogs

News
About Me:
I am a Female SharePoint Developer (a platinum unicorn). I have been working with SharePoint since I attended the Portal University in 2005. I hold a BA in Computer Science from the University of Missouri - Kansas City. I love playing Rockband, organizing user group meetings, working with code, attending events as a speaker or organizer, and having bizarre conversations about geek things with cool people. If you have any comments or questions fill out the contact form and I will try my best to help.



The Guide of a MOSSLover Becky Isserman's Blog

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.

Posted on Friday, May 30, 2008 8:57 AM MOSS | Back to top


Comments on this post: Moving Documents from One Document Library to Another

# re: Moving Documents from One Document Library to Another
Requesting 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....
Left by Nicole on Jul 10, 2008 8:56 AM

# re: Moving Documents from One Document Library to Another
Requesting 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.
Left by MOSSLover on Jul 10, 2008 10:38 AM

# re: Moving Documents from One Document Library to Another
Requesting 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!!
Left by Bonney on Jul 21, 2008 1:48 AM

# re: Moving Documents from One Document Library to Another
Requesting Gravatar...
What kind of access do you have for the library? Are you running the whole code, delete and move documents?
Left by MOSSLover on Jul 22, 2008 11:14 AM

# re: Moving Documents from One Document Library to Another
Requesting 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.
Left by Bonney on Jul 29, 2008 5:35 AM

# re: Moving Documents from One Document Library to Another
Requesting Gravatar...
You can move the metadata if you do it manually, adding the properties in a hash table.

Left by Saul on Nov 10, 2008 4:44 PM

# re: Moving Documents from One Document Library to Another
Requesting Gravatar...
hi
"Modified by" is getting changed to System Account all the time. That's a major drawback.
Left by paromita on Jun 01, 2009 5:24 AM

# re: Moving Documents from One Document Library to Another
Requesting Gravatar...
not sure how / where i use this code. I only have 15 documents to move, but would like to know how anyway
Left by mark on Jul 14, 2009 4:13 PM

# re: Moving Documents from One Document Library to Another
Requesting 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?
Left by Styxol on Nov 18, 2009 6:47 AM

# re: Moving Documents from One Document Library to Another
Requesting 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?
Left by Subhash on Feb 16, 2010 7:15 PM

# re: Moving Documents from One Document Library to Another
Requesting Gravatar...
i found this http://dandd.codeplex.com solution and it may helps!
Left by Guest on Mar 26, 2011 9:57 AM

Your comment:
 (will show your gravatar)


Copyright © MOSSLover | Powered by: GeeksWithBlogs.net