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
    – kaz
    Commented Dec 29, 2015 at 11:18
  • What is the url u are passing?
    – Asad Refai
    Commented Dec 29, 2015 at 12:35
  • abc.abc.abc.com/sites/abc12 - The URL is of this pattern
    – kaz
    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

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.