×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

PowerShell PnP sort SharePoint choice column failing

PowerShell PnP sort SharePoint choice column failing

PowerShell PnP sort SharePoint choice column failing

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close