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?

  • 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(); – Paul Strupeikis Dec 29 '15 at 10:51
3

Try this code.

$web = get-spweb $url
$MyContentType = $web.ContentTypes["ContentType"]
$field = $MyContentType.FieldLinks["Column"]
$field.Required = $False 
$MyContentType.Update()
  • $field = $MyContentType.FieldLinks["Column"] This throws cannot index into null array..and the next two lines too – kaz Dec 29 '15 at 11:18
  • What is the url u are passing? – Asad Refai Dec 29 '15 at 12:35
  • abc.abc.abc.com/sites/abc12 - The URL is of this pattern – kaz Dec 29 '15 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()

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.