Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copy list items from one list to another in sharepoint

Tags:

c#

sharepoint

In Sharepoint how can you copy a list item from one list to another list eg copy from "List A" to "List B" (both are at the root of the site)

I want this copying to occur when a new list item is added to "List A"

I tried using the CopyTo() method of an SPListItem inside the ItemAdded event receiver but couldnt figure out the url to copy to.

like image 854
raklos Avatar asked Jul 02 '09 16:07

raklos


Video Answer


4 Answers

Here is the code I use. Pass it a SPlistItem and the name of the destination list as seen in Sharepoint(Not the URL). The only restriction is that both list must be in the same site:

private SPListItem CopyItem(SPListItem sourceItem, string destinationListName) {
        //Copy sourceItem to destinationList
        SPList destinationList = sourceItem.Web.Lists[destinationListName];
        SPListItem targetItem = destinationList.Items.Add();
        foreach (SPField f in sourceItem.Fields) {
            //Copy all except attachments.
            if (!f.ReadOnlyField && f.InternalName != "Attachments"
                && null != sourceItem[f.InternalName])
            {
                targetItem[f.InternalName] = sourceItem[f.InternalName];
            }
        }
        //Copy attachments
        foreach (string fileName in sourceItem.Attachments) {
            SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + fileName);
            byte[] imageData = file.OpenBinary();
            targetItem.Attachments.Add(fileName, imageData);
        }

        return targetItem;
    }
like image 170
Sylvain Perron Avatar answered Oct 24 '22 11:10

Sylvain Perron


Indeed as Lars said, it can be tricky to move items and retain versions and correct userinfo. I have done similar things with that before so if you need some code examples, let me know through a comment and can supply you with some guidance.

The CopyTo method (if you decide to go with that) need an absolute Uri like: http://host/site/web/list/filename.doc

So, if you are performing this in an event receiver you need to concatinate a string containing the elements needed. Something like (note that this can be done in other ways to):

string dest= 
 siteCollection.Url + "/" + site.Name + list.Name + item.File.Name;
like image 5
Johan Leino Avatar answered Oct 24 '22 09:10

Johan Leino


Copying and moving files, items and folders in SharePoint can be tricky if you want to retain all metadata, timestamps, author info and version history. Take a look a CopyMove for SharePoint - it also has a Web Service API.

like image 1
Lars Fastrup Avatar answered Oct 24 '22 09:10

Lars Fastrup


There's many tools on the market for copying a list item to another list (avepoint, metavis, etc.) but they are pretty expensive if you're planning to do this on only one list.

If you can do this manually once a week for example, look at the following tool : http://en.share-gate.com/sharepoint-tools/copy-move-sharepoint-list-items-with-metadata-and-version-history

like image 1
Sebastien Avatar answered Oct 24 '22 11:10

Sebastien