Friday, 20 August 2010

Show and hide columns in SharePoint list forms with PowerShell

One of the great things about PowerShell with SharePoint 2010 is the ability to configure settings that are not available through the browser interface by writing a simple script, rather than having to load up Visual Studio and develop a custom application to access the Object Model. This article covers how to set properties on columns in order to show or hide them from specific list forms.

The following table taken from MSDN shows the properties available to us:

ShowInDisplayForm Gets or sets a Boolean value that specifies whether the field is displayed in the form for displaying list items.
ShowInEditForm Gets or sets a Boolean value that specifies whether the field is displayed in the form that is used to edit list items.
ShowInListSettings Gets or sets a Boolean value that specifies whether the field is displayed in the page for customizing list settings.
ShowInNewForm Gets or sets a Boolean value that specifies whether the field is displayed in the form that is used to create list items.
ShowInVersionHistory Gets or sets a Boolean value that specifies whether the field is displayed in the page for viewing list item versions.
ShowInViewForms Gets or sets a Boolean value that specifies whether the field is displayed in pages that are used to view list data.

For this example, I am going to disable the ShowInEditForm properties for a column called “Test Column” that I have created directly in a document library (I’ll deal with site columns later). Here is a screenshot of the edit form when I upload a new document to the library or edit the properties of an existing document:

TestColumnThere

I then run the following PowerShell Script:

#Get the web, list and column objects
$web = Get-SPWeb http://portal
$list = $web.Lists["Shared Documents"]
$column = $list.Fields["Test Column"]

#Change the ShowInEditForm property and update objects
$column.ShowInEditForm = $false
$column.Update()
$list.Update()
$web.Dispose()

Now when I edit the properties of the document, the Test Column is no longer there:

TestColumnThere

However, because the ShowInDisplayForm property is still set to true, the column does appear when I view the properties of a document:

TestColumnThere

The script is different if you want to achieve the same thing with a site column. I used the following script to modify the ShowInEditForm property of a site column called “Test Site Column”, which can be added to document libraries and lists directly, or through a content type. Note that the new ShowInEditForm property setting will apply to all document libraries, lists and content types where the site column has been added:

#Get the web and site column objects
$web = Get-SPWeb http://portal
$column = $web.Fields["Test Site Column"]

#Set the PushChangesToLists property for the changes to be applied
#to lists where the column has already been added
$column.PushChangesToLists = $true

#Change the ShowInEditForm property and update objects
$column.ShowInEditForm = $false
$column.Update()
$web.Update()
$web.Dispose()

5 comments:

  1. Thanks!

    This was very helpfull!

    ReplyDelete
  2. Great article. However, if I make it not display in the editform, can I edit the column programmatically?

    I'm finding that when I set it to not display, when user's create new documents, I'm unable to retrieve the value for that column.

    ReplyDelete
  3. Brad - interesting. I haven't tried the scenario you mention, although I'm surprised that you can't get the value. You could always try switching the ShowInEditForm property on using your code, checking access to the column and then switching it off again. It wouldn't be a long term solution, but would be interesting to see if you could get the value at least.

    ReplyDelete
  4. Thanks for your response Phil. I've actually tried that. It's like the value doesn't get created when the column is optional because it doesn't think it exists? Not sure.

    What's worse is if there's a default value, the default value will still show on the front end. Very frustrating and I can't find a solution.

    ReplyDelete