【问题标题】:Sharepoint Online 2013 CSOM constructor error for AttachmentCollection object?Sharepoint Online 2013 AttachmentCollection 对象的 CSOM 构造函数错误?
【发布时间】:2013-06-26 16:53:06
【问题描述】:

我正在编写一个脚本来将项目从一个列表复制到共享点在线服务器上的另一个。我正在使用 2013 sharepoint 客户端对象模型 (CSOM) 在 powershell ISE 中编写脚本。这应该是一件容易的事,但事实恰恰相反。到目前为止,我可以使用 camlquery 检索所有项目,我只是试图将这些项目及其附件复制到另一个列表。我收到的错误是尝试建立一个 attachmentCollection 以从任何项目中检索所有附件,这是代表问题的脚本的一部分:

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

$siteURL = "https://mysite.sharepoint.com"
$password = Read-Host -Prompt "Enter Password" -AsSecureString
$ctx = New-Object Microsoft.Sharepoint.Client.ClientContext($siteURL)
$credentials = New-Object Microsoft.Sharepoint.Client.SharepointOnlineCredentials("admin@mysite.sharepoint.com", $password)
$ctx.Credentials = $credentials


#...Bunch of code that establishes/loads web/lists/items, all works fine
function CopyItem $itemToCopy


function CopyItem ($oldItem)
{
    Write-Host "Copying item" $oldItem.ID
    $newItemCI = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
    $newItem = $archList.AddItem($newItemCI)
    $ctx.load($newItem)
    #Update fields
    $ctx.load($sourceList.Fields)
    $ctx.ExecuteQuery()
    foreach($field in $sourceList.Fields)
    {
        $newItem[$field.InternalName] = $oldItem[$field.InternalName]
    }
    $attachments = New-Object Microsoft.SharePoint.Client.AttachmentCollection  #ERROR HERE
    $attachments = $oldItem.AttachmentFiles
    $ctx.load($attachments)
    $newItem.AttachmentFiles.Add($attachments)
    $newItem.Update()
    $ctx.load($newItem)
    $ctx.ExecuteQuery()
}

错误消息显示:“列表存档失败:出现此错误消息:找不到构造函数。找不到 Microsoft.SharePoint.Client.AttachmentCollection 类型的合适构造函数。”

如果我也尝试将新对象创建为附件,我会得到同样的错误,找不到构造函数。这很奇怪,因为构造函数应该在 client.dll 中,但没有运气。我什至尝试修复我的 2013 CSOM 文件,但没有发现错误。感谢您对此的任何帮助,谢谢。

【问题讨论】:

    标签: sharepoint powershell csom


    【解决方案1】:

    经过大量的试验和错误后,我发现在处理 attachmentCollection 对象时不需要声明新对象。您可以像这样简单地设置一个变量:

    $attachments = $item.AttachmentFiles
    

    $attachments 现在是一个附件对象数组。

    但是,将附件复制/添加到新项目仍然是一个大问题,因为 sharepoint 有一个糟糕的系统来管理这些附件,并且最初没有文件夹来存储它们,您也不能直接创建文件夹。我仍然无法在项目之间复制附件,如果有人知道如何完成此操作,我也很乐意提供帮助。

    向 AttachmentFiles 属性添加附件的主要问题是它使用了 $item.AttachmentFiles.Add() 方法,该方法要求参数是 AttachmentCreationInformation 对象,而不是附件对象。我不知道如何按照我的意图制作此功能,以便可以将预先存在的附件添加到新项目。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2015-03-28
      • 2013-06-03
      • 2021-02-27
      • 1970-01-01
      相关资源
      最近更新 更多