1

What I'm trying to accomplish is referencing a calendar on every site designated, adding a named text column to that calendar, then hiding that created column from the New and Edit form. I think I'm just about there but I'm not great with PowerShell so I think I've got some pieces mixed up.

Edit: An additional part to this would be great if I could include the Calendar on the default page "home.aspx".

Add-PsSnapin Microsoft.SharePoint.PowerShell
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 
$siteNumbers = "03", "14", "22035", "22036", "22040", "22041", "22042", "22043", "22044", "2210", "31"

Foreach($wnl in $siteNumbers)
    {
    $rootWeb = (Get-SPWeb ("http://site/path/" + $wnl))

#Get an reference to the  calendar
$calendarlist=$rootWeb.Lists["Calendar"]                           
#Get a reference to a text field 
$fieldtype=[Microsoft.SharePoint.SPFieldType]::Text                 
#Add a new test text field to the calendar 
$calendarlist.Fields.Add("GCID", $fieldtype, $false)  
#Get a reference to the created field
$CalField = $calendarlist.Fields["GCID"]
#Hide the column in New and Edit Mode
$CalField.ShowInEditForm = $false
$CalField.ShowInNewForm = $false        
#Push Update
$CalField.Update()
$calendarlist.Update() 

The above code referenced the calendar on each site added the column "GCID" but it did not hide it. Do I need to create the column, do the list.update, then hide the column from new/edit, then do the column.update?

1 Answer 1

2

Yes, you should perform an independent field update as the following

  • The below line will actually add the column to the list

     $calendarlist.Fields.Add("GCID", $fieldtype, $false)
    
  • Then run the below cmdled to update the list, no need to perform $CalField.Update()

    $calendarlist.Update() 
    
  • Then Get your field again

    $CalField = $calendarlist.Fields["GCID"]
    
  • Then perform your Hide setting

    $CalField.ShowInEditForm = $false
    $CalField.ShowInNewForm = $false 
    
  • Then update your column field

    $CalField.Update()
    
  • Optionally you can run list update again.

    $calendarlist.Update() 
    

The Final script should look like

$rootWeb = Get-SPWeb http://your site
#Get an reference to the  calendar
$calendarlist=$rootWeb.Lists["your list"]                           
#Get a reference to a text field 
$fieldtype=[Microsoft.SharePoint.SPFieldType]::Text                 
#Add a new test text field to the calendar 
$calendarlist.Fields.Add("GCID", $fieldtype, $false)  
#Push list Update
$calendarlist.Update() 
#Get a reference to the created field
$CalField = $calendarlist.Fields["GCID"]
#Hide the column in New and Edit Mode
$CalField.ShowInEditForm = $false
$CalField.ShowInNewForm = $false        
#Push field Update
$CalField.Update()
1
  • 1
    OK, so my issue was I had the $calendarlist.Update() out of order. Thanks for the help! Marked as answer.
    – NMJohns
    Commented Aug 23, 2017 at 19:26

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.