2

I am copying a file. I do not have it on the local machine. Method .CopyTo() and .Move() is out of question. Environment is SharePoint Online. The file is loaded correctly and I access its properties, e.g. Write-Host $sFile.ServerRelativeUrl. However when I try to pass $FileStream to .ContentStream I receive the error:

Cannot convert the "Microsoft.SharePoint.Client.ClientResult'1[System.IO.Stream]" value of type "Microsoft.SharePoint.Client.ClientResult'1[[System.IO.Stream, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]" to type "System.IO.Stream".

[Microsoft.SharePoint.Client.File]$sFile=[Microsoft.SharePoint.Client.File]$litem.File # file is from SharePoint library item; cast is a desperate attempt
    $ctx.Load($sFile)
    $ctx.ExecuteQuery()
    $fci = New-Object Microsoft.SharePoint.Client.FileCreationInformation
    $fci.ContentStream=[System.IO.Stream]$sFile.OPenBinaryStream()
    $fci.Overwrite=$false
    $fci.Url=$urlEnd
    $dFile = $ll2.RootFolder.Files.Add($fci)  
    $dFile.Update()
    $ctx2.ExecuteQuery()

What I tried so far:

$sBytes = [System.IO.File]::ReadAllBytes($sFile)
$fci.Content=$sBytes

Exception calling "ReadAllBytes" with "1" argument(s): "Could not find file 'C:\WINDOWS\system32\Microsoft.SharePoint.Client.File'."

  • FileStream

    $FileStream = New-Object IO.FileStream("https://tenant"+$sFile.ServerRelativeUrl,[System.IO.FileMode]::Open)
    

The given path's format is not supported.

Question:
How can I convert Microsoft.SharePoint.Client.ClientResult[System.IO.Stream] to [System.IO.Stream]?

1 Answer 1

1

Your code returns result in ClientResult format.

Use [System.IO.Stream($file.OPenBinaryStream().Value)

May it helps you.

1
  • No :( At the .ExecutionQuery() I get Exception calling "ExecuteQuery" with "0" argument(s): "parameters.Content, parameters.ContentStream Parameter name: Specified value is not supported for the parameters.Content, parameters.ContentStream parameter."
    – grisha
    Jul 11, 2016 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.