0

I am building a script to essentially copy an entire directory to a SharePoint online library. I have gotten as far as building the folder directory on SharePoint but cannot seem to load any files onto the online directory. I believe I am really close to cracking this beast, I just need to solve one last error. The full error and my code are listed below:

I have bolded line #109

Exception calling "ExecuteQuery" with "0" argument(s): "File Not Found." At line:109 char:5 + $clientContext.ExecuteQuery() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ServerException

...

 ###################### copy files 
cls
$t1  = get-date
$i=1
$count = ((Get-ChildItem -File  -Recurse -Path $source_folder) | Measure-Object -Property FullName ).Count
foreach ($file in (Get-ChildItem -File  -Recurse -Path $source_folder))
{
    $t01  = get-date
    #$url_dest = $url_sharepoint+$Url_site+$file.FullName.Substring($file.FullName.IndexOf($MainFolder)-1).ToLower().Replace('\','/')
    #$url_dest = $url_sharepoint+$Url_site+'/'+$library+(($file.FullName.ToLower()).Replace($source_folder.ToLower(),'')).Replace('\','/')
    $url_dest = $url_sharepoint+$Url_site+$library+$file.FullName.Substring($file.FullName.IndexOf($MainFolder)-1).ToLower().Replace('\','/')
    $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
    $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
    $FileCreationInfo.Overwrite = $true
    $FileCreationInfo.ContentStream = $FileStream
    $FileCreationInfo.URL = $url_dest
    $Upload = $mylist.RootFolder.Files.Add($FileCreationInfo)
    $listItem = $upload.ListItemAllFields
    $listItem['Title']=($file.Name).split('.')[0]
    Write-Host " Uploading file $i/$count $url_dest"
    $listItem.update()
    $clientContext.Load($Upload)
    **$clientContext.ExecuteQuery()**
    $t02  = get-date
    $speed ="{0:n2}" -f ($file.Length/($t02-$t01).TotalSeconds/1mb)
    Write-Host "...................upload speed was " $speed " MB/sec"
    $i++

    $FileStream.Close()
}
$mylist.Title

$t2=get-date
$size = "{0:n2}" -f (gci -path $source_folder -recurse | measure-object -property length -sum).sum
  • try $FileCreationInfo.Content = System.IO.File.ReadAllBytes(filePath); instead of contentstream – Mike Jan 14 at 1:51
0

After some debugging... I finally discovered that the file path that the files where trying to upload to was wrong. My path was sharepoint.com/documents/mainfolder/subfolder, but it needed to be sharepoint.com/Shared%20Documents/mainfolder/subfolder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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