Exception calling “ExecuteQuery” with “0” argument(s): “File

Welcome Forums General PowerShell Q&A Exception calling “ExecuteQuery” with “0” argument(s): “File

Viewing 4 reply threads
  • Author
    Posts
    • #197945
      Participant
      Topics: 4
      Replies: 5
      Points: 62
      Rank: Member

      I am building a PowerShell 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:

      Line #109 is $clientContext.ExecuteQuery()

      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.Content = System.IO.File.ReadAllBytes(filePath)
      $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
      
      cls
      $speed ="{0:n2}" -f ($size/($t2-$t1).TotalSeconds/1mb)
      Write-Host "Medium upload speed was " $speed " MB/sec"
    • #197951
      Participant
      Topics: 4
      Replies: 82
      Points: 251
      Helping Hand
      Rank: Contributor

      What data type is clientContext?  I don’t see it instantiated in your code.

    • #198038
      Participant
      Topics: 4
      Replies: 5
      Points: 62
      Rank: Member

      I should have posted the whole code.  Hope this answers your question.

       

      $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
      $cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $SecurePassword)
      $clientContext.Credentials = $cred
      
      $mylist = $clientContext.web.Lists.GetByTitle($library)
      $clientContext.Load($mylist)
      $clientContext.ExecuteQuery()
      
      $mylist.Title
      
      
      #Get name of folder
      $MainFolder = Split-Path $source_folder -Leaf
      
      #upload folder to sharepoint
      $UploadMainFolder = $mylist.RootFolder.Folders.Add($MainFolder)
      $clientContext.Load($UploadMainFolder)
      $clientContext.ExecuteQuery()
      
      ###################### get folders existing in the SOURCE
      $directories= @()
      
      
      foreach ($file in (Get-ChildItem -Recurse -Path $source_folder -Attributes Directory))
      {
      
      ($file.FullName.ToLower()).replace($source_folder.ToLower()+'\','')
      $directories +=($file.FullName.ToLower()).replace($source_folder.ToLower()+'\','')
      }
      
      
      foreach ($directory in $directories)
      {
      $myfolder = $UploadMainFolder
      $clientContext.Load($myfolder)
      $clientContext.ExecuteQuery()
      $myfolder = $myfolder.Folders.Add($directory.split('\')[0])
      $clientContext.Load($myfolder)
      $clientContext.ExecuteQuery()
      for ($i = 1; $i -le ($directory.split('\').Count-1) ; $i++)
      {
      $directory.split('/')[$i]
      $myfolder = $myfolder.folders.Add(($directory.split('\'))[$i])
      $clientContext.Load($myfolder)
      $clientContext.ExecuteQuery()
      }
      
      }
      
      
      ###################### 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+$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
      
      cls
      $speed ="{0:n2}" -f ($size/($t2-$t1).TotalSeconds/1mb)
      Write-Host "Medium upload speed was " $speed " MB/sec"
    • #198044
      Participant
      Topics: 4
      Replies: 82
      Points: 251
      Helping Hand
      Rank: Contributor

      I think I’m about out of Schlitz on this one.  I checked the documentation on ClientContext class https://docs.microsoft.com/en-us/dotnet/api/microsoft.sharepoint.client.clientcontext and I don’t see a ExecuteQuery method.  Recommend, putting in a debug breakpoint just before that line is called and then inspect the object i.e. $clientContext | get-member and also look at its current values i.e. $clientContext | select *  Hopefully this will help with what is going on.

      Also, reading that error makes me think the object is null.  The reason I say that is in general when you call a method, the first argument (index 0) is the object itself and then subsequent arguments are within the parenthesis.  For example, $clientContext.Load($Upload) $clientContext object is the first argument and $upload is the second.

      • This reply was modified 6 months, 4 weeks ago by Mike R..
    • #198164
      Participant
      Topics: 4
      Replies: 5
      Points: 62
      Rank: Member

      Thanks for the tips.

      After some debugging… I finally discovered that the file path that the files were 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.

Viewing 4 reply threads
  • The topic ‘Exception calling “ExecuteQuery” with “0” argument(s): “File’ is closed to new replies.