我正在編寫一個腳本,將項目從一個列表複製到另一個SharePoint Server上。我正在使用2013 SharePointPort客戶端對象模型(CSOM)在PowerShell ISE中編寫腳本。這應該是一件容易的事,但事實證明恰恰相反。到目前爲止,我可以使用camlquery來檢索所有項目,我只是試圖將這些項目及其附件複製到另一個列表中。我收到的錯誤是試圖建立一個attachmentCollection檢索所有來自任何項目的附件,這裏是代表問題的腳本的一部分:Sharepoint Online 2013 AttachmentCollection對象的CSOM構造函數錯誤?
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("[email protected]", $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()
}
錯誤消息說:「列表存檔失敗at:with this error message:Constructor not found。Can not find a suitable constructor for type Microsoft.SharePoint.Client.AttachmentCollection。「
如果我嘗試創建新的對象作爲附件,我也會得到相同的錯誤,找不到構造函數。這很奇怪,因爲構造函數應該在client.dll中,但沒有運氣。我甚至嘗試修復我的2013 CSOM文件,沒有發現錯誤。任何幫助表示讚賞,謝謝。