1
votes

I'm struggling since a couple of days to upload files to Sharepoint 2010 with powershell. I'm on a win7 machine with powershell v2 trying to upload to a SP 2010 site.

I'm having 2 major issues

  1. $Context.web value is always empty even after Executequery() and no error is shown. My $Context variable gets the server version (14.x.x.x.x) but nothing more
  2. $Context.Load($variable) which always returns the error Cannot find an overload for "Load" and the argument count: "1".

I copied Sharepoint DLLs to my Win7 machine and I import the reference to my script. The below script is a mix of many parts I took from the net.

I'v already tried unsuccessfully to add an overload on the clientcontext defining Load method without Type parameter suggested in the following post http://soerennielsen.wordpress.com/2013/08/25/use-csom-from-powershell/

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")

$site = "https://Root-of-my-site"
$listname = "My-folder"

$context = New-Object Microsoft.SharePoint.Client.ClientContext($site)

[Microsoft.SharePoint.Client.Web]$web = $context.Web
[Microsoft.SharePoint.Client.List]$list = $web.Lists.GetByTitle($listName)

$Folder = "C:\temp\Certificates"
$List = $Context.Web.Lists.GetByTitle($listname)

Foreach ($File in (dir $Folder))
{
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.Content = get-content -encoding byte -path $File.Fullname
$FileCreationInfo.URL = $File
$Upload = $List.RootFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()
}

The error is

Cannot find an overload for "Load" and the argument count: "1". At C:\temp\uploadCertToSharepoint.ps1:48 char:14 + $Context.Load <<<< ($Upload) + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBest

Can someone please help me sorting this issue? I'll need to upload around 400 files with ad-hoc fields to a sharepoint site in a couple of weeks and at the moment I'm completely stuck. Running the script server side is unfortunately not possible.

Thanks, Marco

2
I do not get specified error when i use your script. I changed 1 line in it and it works as expected. Maybe you get some other error?Yevgeniy.Chernobrivets
Oops, sorry, tested it on SharePoint 2013 and Powershell 3. :)Yevgeniy.Chernobrivets

2 Answers

1
votes

This error occurs since ClientRuntimeContext.Load is a Generics Method:

public void Load<T>(
    T clientObject,
    params Expression<Func<T, Object>>[] retrievals
)
where T : ClientObject

and Generics methods are not supported natively in PowerShell (V1, V2) AFAIK.

The workaround is to invoke a generic methods using MethodInfo.MakeGenericMethod method as described in article Invoking Generic Methods on Non-Generic Classes in PowerShell

In case of ClientRuntimeContext.Load method, the following PS function could be used:

Function Invoke-LoadMethod() {
param(
   $clientObjectInstance = $(throw “Please provide an Client Object instance on which to invoke the generic method”)
) 
   $ctx = $clientObjectInstance.Context
   $load = [Microsoft.SharePoint.Client.ClientContext].GetMethod("Load") 
   $type = $clientObjectInstance.GetType()
   $clientObjectLoad = $load.MakeGenericMethod($type) 
   $clientObjectLoad.Invoke($ctx,@($clientObjectInstance,$null))
}

Then, in your example the line:

$Context.Load($Upload)

could be replaced with this one:

Invoke-LoadMethod -clientObjectInstance $Upload

References

0
votes

It throws the error because in powershell 2.0 you cannot call generic method directly. You need to create closed method using MakeGenericMethod. Try to use code below.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")

$site = "http://server"
$listname = "listName"
$Folder = "C:\PS\Test"

$context = New-Object Microsoft.SharePoint.Client.ClientContext($site)

[Microsoft.SharePoint.Client.Web]$web = $context.Web
[Microsoft.SharePoint.Client.List]$list = $web.Lists.GetByTitle($listName)

$method = $Context.GetType().GetMethod("Load")
$closedMethod = $method.MakeGenericMethod([Microsoft.SharePoint.Client.File])

Foreach ($File in (dir $Folder))
{
    $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
    $FileCreationInfo.Overwrite = $true
    $FileCreationInfo.Content = (get-content -encoding byte -path $File.Fullname)
    $FileCreationInfo.URL = $File
    $Upload = $List.RootFolder.Files.Add($FileCreationInfo)
    $closedMethod.Invoke($Context, @($Upload, $null) )
    $Context.ExecuteQuery()
}