0

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

2 Answers 2

0

I tested the PnP PowerShell in my side:

$creds = (New-Object System.Management.Automation.PSCredential "[email protected]",(ConvertTo-SecureString "yourpassword" -AsPlainText -Force))
Connect-PnPOnline -Url https://tenant.sharepoint.com -Credentials $creds
 
$listsToAdd = @("MyList2","MyList3")

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
}

It's working to add the new option and sort order:

enter image description here

I'm using the latest PnP version.

0

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

1
  • Great to hear it's starting to be working in your side, you can accept this as answer so that it could also help others in the forum.
    – Jerry
    Commented Oct 5, 2020 at 2:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.