Hiding a ListField in NewForm/EditForm in Sharepoint

Hiding a Field can be useful if you don’t want your Users to be able to edit a specific field. Mattias Karlsson, a friend of mine, describes a step-by-step on achieving this on his blog. In this Post, I’m going to describe how to do it through PowerShell. First make a connection to the specific Field, then simply set the ShowInNewForm and ShowInEditForm properties to $false. This will hide the fields from the User when the user is trying to create or edit a List Item.

The image below shows the list that I’m going to edit. I will remove the UserField so that Users editing Items in this List won’t be able to edit the UserField

sharepoint-171

We can hide the fields through a simple function.


function Hide-SPField([string]$url, [string]$List, [string]$Field) {
  [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

  $SPSite = New-Object Microsoft.SharePoint.SPSite($url)
  $OpenWeb = $SPSite.OpenWeb()

  $OpenList = $OpenWeb.Lists[$List]

  $OpenField = $OpenList.Fields[$Field]
  $OpenField.ShowInNewForm = $False
  $OpenField.ShowInEditForm = $False
  $OpenField.Update()

  $SPSite.Dispose()
  $OpenWeb.Dispose()
}

Hide-SPField -url http://moss -List "My Custom List" -Field "UserField"

Now, when the user clicks on New Item or Edit Item, the UserField won’t be visible.

sharepoint-18

Rating 4.00 out of 5
[?]
Categories: Sharepoint Tags:
  1. Pandiya
    July 11th, 2009 at 05:04 | #1

    Excellente! Thanks a lot for this simple yet great way of hiding fields on edit form and new form.

    Do you know any way to add more fields to Document library “Group By” option?

  2. Lee
    November 19th, 2009 at 20:21 | #2

    Nice script, thanks.

    I found that it was necessary to add .PushChangesToList=true

  3. Jimbo Alba
    August 3rd, 2010 at 08:57 | #3

    Works great, thanks Niklas.

    I’m having a list with 2 content types. However, running the script it only applies to the default content type. Do you know how to apply it to all content types on the list? Or maybe be able to select each content type and apply the script?

  4. Cauline
    November 18th, 2011 at 23:16 | #4

    Perfect. Thanks :)

  1. January 16th, 2009 at 10:28 | #1

Comment Spam Protection by WP-SpamFree