10 November 2010

Hide Columns in SharePoint New, Edit and Disp Forms: Poweshell

Hide Columns in SharePoint New, Edit and Disp Forms: Powershell


So here is the scenario, you have a issue tracking list and if you only want to introduce certain hidden columns on your New and Disp forms but not your Edit form as an example. You can have any combination for these requirements.
Since the issues are logged by the QA or end-user, we want to hide the Assigned To, Due Date, Priority and Impact from the new form. We want the user editing, typically the dev team lead, to assign these values.

There are several ways you can achieve this.. choose the best that fits your need... or comfortable with.

1. PowerShell
2. Programmatically
3. Custom Forms (SharePoint Designer)
4. ContentTypes
5. jQuery

Using Powershell

  • First we need to get the id of the fields to be hidden. A very important tool in your armor is a free tool called SharePoint Manager 2010.
  • Navigate to your Web-App > Site Collection > Site > List > Fields and get the ids of the fields you want to hide
  • Next we will use another important free tool PowerGUI and write the following powershell script
  • #$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
    #if ($snapin -eq $null) {
    #Write-Host "Loading SharePoint Powershell Snapin"
    #Add-PSSnapin "Microsoft.SharePoint.Powershell"
    #}
    
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
    
    $WebUrl = "http://sp2010vmc002/hidecoldemo"
    Write-Host "Opening Web" $WebUrl
    $web = Get-SPWeb $WebUrl
    
    ####### Issue Tracker List ######
    $list = $web.Lists["IssueTracker1"];
    
    #Assigned To
    $FieldGuid = New-Object System.Guid("53101f38-dd2e-458c-b245-0c236cc13d1a");
    $Field = $list.Fields[$FieldGuid];
    
    $Field.ShowInNewForm = $false;
    $Field.ShowInDisplayForm = $true;
    $Field.ShowInEditForm = $true;
    
    $Field.Update();
    $list.Update();
    
    #Due Date
    $FieldGuid = New-Object System.Guid("cd21b4c2-6841-4f9e-a23a-738a65f99889");
    $Field = $list.Fields[$FieldGuid];
    
    $Field.ShowInNewForm = $false;
    $Field.ShowInDisplayForm = $true;
    $Field.ShowInEditForm = $true;
    
    $Field.Update();
    $list.Update();
    
    #Similarly for Impact and Propority..
    
    $Web.Dispose();  
    
  • The resultant new form will look like this




Ref:
http://blog.qumsieh.ca/2010/02/16/hide-columns-in-sharepoint-new-edit-and-disp-forms/

7 comments:

  1. Hi man
    you can do this without code within sharepoint manager
    when selecting the column inside the library or list

    ReplyDelete
  2. hello Anonymous,

    "Hi man
    you can do this without code within sharepoint manager
    when selecting the column inside the library or list"

    I will be looking this up myself, though if you had more imput i'd love to hear it.

    ReplyDelete
  3. Yes you can do it in SharePoint Manager

    ReplyDelete
    Replies
    1. Hi What happen If i take back up and restore it in another farm?
      Whether I need to re run the script again?

      Delete
  4. It will be retain even if we restore it in another farm?

    ReplyDelete