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:40

Copy List Items from one SharePoint List or library to Another SharePoint list or library using c# server side code

//Itecollection is a collection of data from source list

 public void CopyItemsFromOneListToAnotherList(SPListItemCollection itemCollection)
 {  
 using (SPSite site = new SPSite(siteUrl))
 {
  using (SPWeb web = site.OpenWeb())
  {
     //Get destination list/library
     //destListName - Destination list/library name
   SPList destList = web.Lists.TryGetList(destListName);

   foreach (SPListItem sourceItem in itemCollection)
   {
    //Add new Item to list
    SPListItem destItem = destList.Items.Add();

    foreach (SPField field in sourceItem.Fields)
    {
     if (!field.ReadOnlyField && !field.Hidden && field.InternalName != "Attachments")
     {
      if (destItem.Fields.ContainsField(field.InternalName))
      {
       //Copy item to  destination library
         destItem[field.InternalName] = sourceItem[field.InternalName];
      }
     }
    }
    //Update item in destination  library or list
    destItem.Update();
    Console.WriteLine("Copied " + sourceItem["ID"] + "to destination list/library");
   }
  }
 }

 }
查看更多
小情绪 Triste *
3楼-- · 2020-02-08 18:42

Make sure you call CopyTo(url) method on SPFile, not on SPListItem. for example:

ItemUpdated(SPItemEventProperties properties)
{ 
  //...
  string url = properties.Web.Site.Url + "/" + properties.Web.Name + "Lists/ListName/" + properties.ListItem.File.Name;
  //properties.ListItem.File.MoveTo(url);
  properties.ListItem.File.CopyTo(url);
  //...
}
查看更多
做自己的国王
4楼-- · 2020-02-08 18:43

So, the lists have the exact same or similar columns? Either way, you could create a simple workflow that runs automatically when an item is created in "List A". Since the workflow in question is relatively simple, I'd recommend using SharePoint Designer (which is free) to create it, since you can easily match up the columns from the two lists. The walk through below should be able to help you get started.

Create a Workflow - SharePoint Designer

查看更多
smile是对你的礼貌
5楼-- · 2020-02-08 18:48

I had the same problem.

After experimenting a bit instead of

targetItem[f.InternalName] = sourceItem[f.InternalName];

I used:

targetItem[childField.Title] = sourceItem[parentField.Title];

查看更多
够拽才男人
6楼-- · 2020-02-08 18:49

Here is a powershell equivalent of Sylvian's that does allow for cross-site copy. His code could be modified similarly as well...

param([string]$sourceWebUrl, [string]$sourceListName, [string]$destWebUrl, [string]$destListName)

$sourceWeb = get-spweb $sourceWebUrl;
$sourceList = $sourceWeb.Lists[$sourceListName];
$destWeb = get-spweb $destWebUrl;
$destList = $destWeb.Lists[$destListName];
$sourceList.Items |%{
$destItem = $destList.Items.Add();
$sourceItem = $_;
$sourceItem.Fields |%{
    $f = $_;
    if($f.ReadOnlyField -eq $false -and $f.InternalName -ne "Attachments" -and $sourceItem[$f.InternalName] -ne $null){
        $destItem[$f.InternalName] = $sourceItem[$f.InternalName];
    }
}
$destItem.Update();
}

To use, copy and past to a file copy-listitems.ps1 and run using Sharpoint powerhsell commandline...

查看更多
登录 后发表回答