0
votes

I'm writing a script to copy items from one list to another on a sharepoint online server. I'm using the 2013 sharepoint Client Side Object Model (CSOM) to script this in powershell ISE. This should be an easy task, but it's proving just the opposite. So far I can retreive all the items using camlquery and I'm just trying to duplicate those items and their attachments to another list. The error I receive is from trying to establish an attachmentCollection to retrieve all of the attachments from any item, here is a portion of the script that represents the problem:

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()
}

The error message says: "The List Archive Failed at: with this error message: Constructor not found. Cannot find an appropriate constructor for type Microsoft.SharePoint.Client.AttachmentCollection."

I get the same error if I try to create new-object as Attachment as well, can't find constructor. This is odd, as the constructor should be in the client.dll, but no luck. I've even tried repairing my 2013 CSOM files, no errors were found there. Any help on this is appreciated, thank you.

1

1 Answers

0
votes

After a hellish amount of trial and error, I discovered that you did not need to declare a new-object when dealing with the attachmentCollection objects. You can simply set a variable up like so:

$attachments = $item.AttachmentFiles

$attachments is now an array of attachment objects.

However, there is still a huge issue of copying/adding attachments to new items, since sharepoint has a horrible system for managing these attachments and does not initially have a folder to store them, nor can you create a folder directly. I'm still having trouble copying attachments between items, if anyone has knowledge of how to accomplish this, I would love help on that as well.

The main problem in adding attachments to AttachmentFiles property is that it uses the $item.AttachmentFiles.Add() method, which requires the parameter to be a AttachmentCreationInformation Object, not an attachment Object. I have no idea how to make this function as I intend so that I can add a pre-existing attachment to a new item.