Saturday, February 6, 2016

Fix the error "Cannot find an overload for "Load" and the argument count: "1"" in PowerShell

PowerShell script is very useful for end-user who wants to work with behind SharePoint (both of on-premise and online). However, user usually gets some troubles if it throws the error during executing the script. The difficulties of coding and working with PowerShell script is to hard debugging and checking somehow.

This error is an example:  

Cannot find an overload for "Load" and the argument count: "1".

It's so weird because the code is always correct (in developer though). If you know that this error only come from the unique reason so that you have no strange and surprise with the funny mistake.

Here is code:

$web = $clientContext.Web
$clientContext.Load($web)
$clientContext.ExecuteQuery()


$lists = $web.Lists
$clientContext.Load($lists)
$clientContext.ExecuteQuery()


$lst = $list.GetByTitle("CheckList")
$clientContext.Load($lst)
$clientContext.ExecuteQuery()


$qry = New-Object Microsoft.SharePoint.Client.CamlQuery
$qry.ViewXML = "<View><Query><Where><And><Gt><FieldRef Name='Due_x0020_Date'/><Value Type='DateTime'><Today OffsetDays='0'/></Value></Gt><IsNull><FieldRef Name='Actual_x0020_Completion_x0020_Da'/></IsNull></And></Where></Query></View>"


$items = $lst.GetItems($qry)
$clientContext.Load($items)
$clientContext.ExecuteQuery()


Be sure it will happen error when it is executed. The error looks like:

Cannot find an overload for "Load" and the argument count: "1".
At C:\Users\...\Downloads\PSScript\Retrieve.ps1:55 char:1
+ $clientContext.Load($item)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

The root cause of this error come from retrieving the null-able object. That is the variable as a parameter of Load method is null or delivery of that variable is null.

$a = ()
$b = $a.Z()
$c = $b.get_T()
$context.Load($c)

In this example, if $a or $b or $c is null, the error will happen at $context.Load($c) with the message. above.

To fix this, just only take overview of the code and prepare all variables and make sure the name of methods and properties are not miss-spelled and correctly grammar.

Good luck!

No comments: