PS脚本可将一个列表的内容复制到另一个列表 [英] PS Script to copy contents of one list to another

查看:124
本文介绍了PS脚本可将一个列表的内容复制到另一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

不确定这是否是正确的论坛,但认为我是从这里开始的.我需要将一个列表的内容复制到同一子站点中的另一个列表.这两个列表都是资产库.我正在使用以下代码($ web设置为子站点的url):     

$ sourceList = $ web.Lists ["Images"]
$ targetList = $ web.Lists ["Images Backup2"]

$ sourceColumns = $ sourceList.Fields
$ sourceItems = $ sourceList.GetItems();



foreach($ sourceItems中的$ sourceItem)
{

$ TargetItem = $ TargetList.AddItem()
  foreach($ sourceColumns中的$ column)
  {
  if($ column.ReadOnlyField -eq $ False -and $ column.InternalName -ne'Attachments')
      {
            $ TargetItem [$($ column.InternalName)] = $ sourceItem [$($ column.InternalName)];


      }

  }

  $ TargetItem.Update()
}

我5次收到以下错误消息:

调用"Update"的异常;带有"0"参数:要将项目添加到文档库,请使用SPFileCollection.Add()"

我不确定它在寻找什么.有想法吗?

解决方案

我认为问题在于您正在使用文档库而不是简单的列表.确实可以按列表访问文档库,但是底层是您正在尝试复制文件.

您可以从以下链接获取示例:http://www.sharepointdiary.com/2016/03/copy-files-between-document-libraries-using-powershell-in-sharepoint.html.

只需根据需要更改参数.


Hello all,

Not sure if this is the correct forum or not but figured I"d start here. I have a need to copy the contents of one list to another in the same sub site. Both lists are Asset Libraries. I am using the following code ($web is set to the url of subsite):     

$sourceList = $web.Lists["Images"]
$targetList = $web.Lists["Images Backup2"]

$sourceColumns = $sourceList.Fields
$sourceItems = $sourceList.GetItems();



foreach($sourceItem in $sourceItems)
{

$TargetItem = $TargetList.AddItem()
    foreach($column in $sourceColumns)
    {
    if($column.ReadOnlyField -eq $False -and $column.InternalName -ne "Attachments")
        {
             $TargetItem[$($column.InternalName)] = $sourceItem[$($column.InternalName)];


        }

    }

    $TargetItem.Update()
}

I receive the following error 5 times:

Exception calling "Update" with "0" argument(s): "To add an item to a document library, use SPFileCollection.Add()"

I'm not sure what it's looking for. Any ideas?

解决方案

Hi,

I think the problem is that you are working with a Document Library not a simply list. It is true that you can access document library as list, however the underlying is you are trying to copy a file.

You can grab the example from this link : http://www.sharepointdiary.com/2016/03/copy-files-between-document-libraries-using-powershell-in-sharepoint.html .

Just change the parameter according to your needs.


这篇关于PS脚本可将一个列表的内容复制到另一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆