Home Page Link
  Skip Navigation Links
Mirrored Blogs
Information Worker > Blogs > Mirrored Blogs > Posts > When CopyTo() and CopyFrom() just don't cut it anymore
When CopyTo() and CopyFrom() just don't cut it anymore

When it come to copying of items to and from different locations (in SharePoint) by means of using SharePoint object model, first obvious methods you find (and try using) are CopyTo() and CopyFrom(). They all seem nice and dandy if you’re simply just copying an item, but when it comes to some slightly more complex cases, such as, copying an item that contains major/minor versions, with check in/check out functionality or even copying to a folder where an item like this already exists, you soon realise that the above mentioned methods are sooo rubbish/useless. You don’t believe me?? Use a Reflector tool to find out, it will be far clearer.

In my case, one small part of the required functionality as per our design of the SharePoint 2007 solution required copying of a file to a document library where that file might already exists and the copied file must then be version on top of that existing file, we’re using major versions here only (confused, I apologise, try and read it again slowly, I’m too lazy to re-phrase). Anyway so basically what needed to be done is to first CheckOut the item if the item exists (and if it requires checking out) and then copy a file from the origin to a destination file as a new version (one would expect this to be done by default).

Then I started exploring all the alternatives, until my friend Rupert pointed out the OpenBinary and SaveBinary functionality and it worked, just as expected, I never turned back.

Here’s pretty much what the code looked like:

//getting the list object of the target list

                SPList nextList = web.Lists[destinationListNameValue]; //web is SPWeb web

               

                SPListItemCollection newListItems = nextList.Items;

                foreach (SPListItem newListItem in newListItems)

                {

//looking for a list item that is named the same way as the document name of the document that is being copied (documentName) 

                    if (documentName == newListItem["Name"].ToString())

                    {

//impersonatedItem is the impersonated (I had my reasons for needing to use impersonation here, you probably won’t have to) SPListItem of the document that is being copied and newListItem is the SPListItem of the target document 

                        Byte[] fileContent = impersonatedItem.File.OpenBinary();

                        newListItem.File.SaveBinary(fileContent);

                    }

                }

(Please Note that you need to have Microsoft.SharePoint referenced in your project and include – using Microsoft.SharePoint)

 

Here’s someone else also dealing with the whole CopyTo() and CopyFrom() issue:

 

http://k2distillery.blogspot.com/2007/10/copy-version-history-with_5.html

 

While on this topic, here’s also a useful article on Copying over a SharePoint list from source site to destination site:

http://blah.winsmarts.com/2007-5-Copying_over_a_SharePoint_list_from_source_site_to_destination_site.aspx

By the way, don’t get me wrong, the versatility given by CopyTo() in which you can just copy a file in a SP system  by giving full url of the file can be quick/easy and useful in some cases, but still very few.

Comments

There are no comments yet for this post.

RSS Feed RSS 2.0 Feed



1
1
1
1
Tags
1
1
1
1

1
1
1
1
1
1
1
1

Feedback