copy list items from one list to another in sharep

2020-02-08 18:12发布

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.

标签: c# sharepoint
11条回答
神经病院院长
2楼-- · 2020-02-08 18:24
private void CopyAttachmentsToList(SPListItem srcItem, SPListItem tgtItem)
{
    try
    {
        //get source item attachments from the folder
        SPFolder srcAttachmentsFolder =
            srcItem.Web.Folders["Lists"].SubFolders[srcItem.ParentList.Title].SubFolders["Attachments"].SubFolders[srcItem.ID.ToString()];

        //Add items to the target item
        foreach (SPFile file in srcAttachmentsFolder.Files)
        {
            byte[] binFile = file.OpenBinary();
            tgtItem.Update();
            tgtItem.Attachments.AddNow(file.Name, binFile);
            tgtItem.Update();
        }
    }
    catch
    {
        //exception message goes here
    }
    finally
    {
        srcItem.Web.Dispose();
    }
}

Don't forget to add this line, tgtItem.Update();, else you will get an err.

查看更多
ら.Afraid
3楼-- · 2020-02-08 18:29

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

查看更多
倾城 Initia
4楼-- · 2020-02-08 18:30

How to copy field and save versions:

public static SPListItem CopyItem(SPListItem sourceItem, SPList destinationList)
            {
                SPListItem targetItem = destinationList.AddItem();

                //loop over the soureitem, restore it
                for (int i = sourceItem.Versions.Count - 1; i >= 0; i--)
                {
                    //set the values into the archive 
                    foreach (SPField sourceField in sourceItem.Fields)
                    {
                        SPListItemVersion version = sourceItem.Versions[i];

                        if ((!sourceField.ReadOnlyField) && (sourceField.InternalName != "Attachments"))
                        {
                            SetFields(targetItem, sourceField, version);
                        }
                    }

                    //update the archive item and 
                    //loop over the the next version
                    targetItem.Update();
                }

                foreach (string fileName in sourceItem.Attachments)
                {
                    SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + fileName);
                    targetItem.Attachments.Add(fileName, file.OpenBinary());
                }

                targetItem.SystemUpdate();
                return targetItem;
            }

            private static bool SetFields(SPListItem targetItem, SPField sourceField, SPListItemVersion version)
            {
                try
                {
                    targetItem[sourceField.InternalName] = version.ListItem[sourceField.InternalName];
                    return true;
                }
                catch (System.ArgumentException)//field not filled
                {
                    return false;
                }
                catch (SPException)//field not filled
                {
                    return false;
                }
            }
查看更多
beautiful°
5楼-- · 2020-02-08 18:36

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.

查看更多
我命由我不由天
6楼-- · 2020-02-08 18:38

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;
查看更多
Anthone
7楼-- · 2020-02-08 18:39

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;
    }
查看更多
登录 后发表回答