0

I have the following script :

$site = new-object Microsoft.SharePoint.SPSite("http://TEST123")
$web = $site.OpenWeb()
$ct=$web.ContentTypes["MyCT"]  
$field = $ct.Fields.GetFieldByInternalName("AddTESTColumn")                            
$ct.FieldLinks[$field.id].DisplayName ="AddTESTColumn"     
$ct.Update();

I have used the above to update the display name of a column that is associated to CT.

But now I want to update a choice field column so will be adding a new choice "away" to the current choices tried to use the choices.add but keep getting error message

I used the following :

$site = Get-SPSite -Identity "http://TEST123"

$web = $site.RootWeb
$field = 
$web.Fields["AddTESTColumn"];
$field.choices.add("away")
$field.update();

and get the following message:

You cannot call a method on a null-valued expression.

the issue is with $field = $web.Fields["AddTESTColumn"]; as its not recognising the field name even though it exist. I have used both internal and display name, same issue.

  • please add the code you tried for adding the choice. – harshal gite Sep 6 '19 at 12:45
  • what's the error message that you are getting ? – Gautam Sheth Sep 6 '19 at 12:48
  • Your code looks fine. Please remember that the field names are case sensitive. – Mike2500 Sep 6 '19 at 13:06
  • Yes made sure of that, just tested it with another choice field in the content type works ok for that something with that column its just not liking the name – John Smith Sep 6 '19 at 13:11
  • Just to confirm, if you run the following, you can find your column, and the Title matches what you're using in the script? $web.fields | select title,internalname | sort title | ft – Mike2500 Sep 6 '19 at 13:19
1

If it is a Site Column then your code is fine.

$site = Get-SPSite -Identity "http://TEST123"    
$web = $site.RootWeb
$field = $web.Fields["SiteColumnInternalName"];
$field.choices.add("away") 
$field.update();

If it is a list column and not a site column, then try the code below.

$site = Get-SPSite -Identity "http://TEST123"    
$web = $site.RootWeb
$List = $web.Lists["ListName"]
$field = $list.Fields.GetField("ColumnInternalName");
$field.Choices.Add("away") 
$field.update();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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