2

I'm trying to change the setting of a column to make it not a required field. This is the code I'm using:

$site = new-object Microsoft.SharePoint.SPSite("abc.com")
$web = $site.OpenWeb()

$MyContentType=$web.ContentTypes["ContentType"]
$MyField=$MyContentType.Fields | Where {$_.Id -eq $web.Fields["Column"].id}
$MyContentType.FieldLinks[$MyField.Id].Required=$False 

$MyContentType.Update()

But this is giving error that $MyContentType.FieldLinks is NULL. How can I successfully execute my PowerShell script?

1
  • 5
    Two notes here, a bit off topic: 1. There is no need to open an SPSIte object first - go straight for the SPWeb: $web = Get-SPWeb "example.com" 2. Dispose of opened SPSite and SPWeb objects by calling .Dispose(); Commented Dec 29, 2015 at 10:51

2 Answers 2

3

Try this code.

$web = get-spweb $url
$MyContentType = $web.ContentTypes["ContentType"]
$field = $MyContentType.FieldLinks["Column"]
$field.Required = $False 
$MyContentType.Update()
3
  • $field = $MyContentType.FieldLinks["Column"] This throws cannot index into null array..and the next two lines too Commented Dec 29, 2015 at 11:18
  • What is the url u are passing? Commented Dec 29, 2015 at 12:35
  • abc.abc.abc.com/sites/abc12 - The URL is of this pattern Commented Dec 29, 2015 at 12:45
0

Try this

$site = new-object Microsoft.SharePoint.SPSite("abc.com")
$web = $site.rootweb

$MyContentType=$web.ContentTypes["ContentType"]

$Fields=$MyContentType.Fieldlinks
$Field=$Fields["Internal Name of the Field"] #Type $MyContentType.Fieldlinks to get the details of all the fields.
$Field.Required=$false
$MyContentType.Update()
2
  • Thanks.But I don't want to modify it at root level. Commented Dec 30, 2015 at 7:51
  • ok. please check this link. paulryan.com.au/2013/ct-field-props this may be helpful to you Commented Dec 30, 2015 at 9:45

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.