Share via

PowerShell PnP sort SharePoint choice column failing

CC 96 Reputation points
2020-10-01T15:27:26.81+00:00

This code worked for about last 2 months, then is now failing:

Connect-PnPOnline -Url "https://myco.sharepoint.com/sites/sandbox" -UseWebLogin

$listsToAdd = @("Structure Reqs","Perm Mods")

ForEach ($listToAdd in $listsToAdd)
{
$field = Get-PnPField -List $listToAdd -Identity "MyTargetColumn"
$ctx = Get-PnPContext

$fieldChoice = [Microsoft.SharePoint.Client.ClientContext].GetMethod("CastTo").MakeGenericMethod([Microsoft.SharePoint.Client.FieldChoice]).Invoke($ctx,$field)

$ctx.Load($fieldChoice)
Invoke-PnPQuery

$choices = $fieldChoice.Choices
$choices += "MyNewValue".ToUpper()

$fieldChoice.Choices = $choices | Sort-Object

$fieldChoice.UpdateAndPushChanges($true)
Invoke-PnPQuery
}

*** Error message is:

Exception calling "Invoke" with "2" argument(s): "Object does not match target type."
At line:14 char:1

  • $fieldChoice = [Microsoft.SharePoint.Client.ClientContext].GetMethod( ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : NotSpecified: (:) [], MethodInvocationException
  • FullyQualifiedErrorId : TargetException

Cannot find an overload for "Load" and the argument count: "1".
At line:16 char:1

  • $ctx.Load($fieldChoice)
  • ~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : NotSpecified: (:) [], MethodException
  • FullyQualifiedErrorId : MethodCountCouldNotFindBest

The property 'Choices' cannot be found on this object. Verify that the property exists and can be set.
At line:22 char:1

  • $fieldChoice.Choices = $choices | Sort-Object
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidOperation: (:) [], RuntimeException
  • FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At line:24 char:1

  • $fieldChoice.UpdateAndPushChanges($true)
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidOperation: (:) [], RuntimeException
  • FullyQualifiedErrorId : InvokeMethodOnNull

Exception calling "Invoke" with "2" argument(s): "Object does not match target type."
At line:14 char:1

  • $fieldChoice = [Microsoft.SharePoint.Client.ClientContext].GetMethod( ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : NotSpecified: (:) [], MethodInvocationException
  • FullyQualifiedErrorId : TargetException

Cannot find an overload for "Load" and the argument count: "1".
At line:16 char:1

  • $ctx.Load($fieldChoice)
  • ~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : NotSpecified: (:) [], MethodException
  • FullyQualifiedErrorId : MethodCountCouldNotFindBest

The property 'Choices' cannot be found on this object. Verify that the property exists and can be set.
At line:22 char:1

  • $fieldChoice.Choices = $choices | Sort-Object
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidOperation: (:) [], RuntimeException
  • FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At line:24 char:1

  • $fieldChoice.UpdateAndPushChanges($true)
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidOperation: (:) [], RuntimeException
  • FullyQualifiedErrorId : InvokeMethodOnNull
Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

3 answers

Sort by: Most helpful
  1. CC 96 Reputation points
    2020-10-03T16:16:03.78+00:00

    Thank you...looks like I had to delete SharePoint GAC, restart, and re-install PnP Online and it started working.

    Was this answer helpful?


  2. CC 96 Reputation points
    2020-10-02T03:12:37.743+00:00

    What PnP version do you have?

    Was this answer helpful?


  3. Amos Wu-MSFT 4,131 Reputation points Moderator
    2020-10-02T03:00:11.167+00:00

    The code you shared works well in my environment.
    29783-image.png
    You could try to create a new list with a choice column,then run the code again.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.