Thursday, July 19, 2012

SharePoint : Show/Hide Columns in New, Edit and Disp Forms

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 Javascript:

<script language="javascript" type="text/javascript">

_spBodyOnLoadFunctionNames.push("hideFields");

function findacontrol(FieldName) {

   var arr = document.getElementsByTagName("!");
   // get all comments
   for (var i=0;i < arr.length; i++ )
   {
      // now match the field name
      if (arr[i].innerHTML.indexOf(FieldName) > 0)
      {         return arr[i];      }
   }
}

function hideFields() {

   var control = findacontrol("Title");
   control.parentNode.parentNode.style.display="none";
   control = findacontrol("Document Link");
   control.parentNode.parentNode.style.display="none";
   control = findacontrol("PublishDate");
   control.parentNode.parentNode.style.display="none";

}
</script>
Ref: http://sharepointsherpa.com/2008/08/26/sharepoint-2007-hiding-fields-on-newformaspx-and-editformaspx-the-easy-way/ 

Using Powershell : Hide/Show Columns in SharePoint

  • 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"
    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(); 
 Ref: http://howtosharepoint.blogspot.com/2010/11/hide-columns-in-sharepoint-new-edit-and.html

Using Jquery: Hide/Show Columns in SharePoint

This is another post in Paul Galvin's on-going series on how to use jQuery with SharePoint. If you want to learn more about jQuery, I highly recommend: jQuery in Actionhttp://www.assoc-amazon.com/e/ir?t=httppaulgalvi-20&l=ur2&o=1 by Bear Bibeault and Yehuda Katz.
UPDATE (already!): I did think of a better way to locate the <TR> tag I want to hide and wrote about it here.  You may still find this article interesting anyway so I'm leavnig it up.
I want to hide a text field, �Hide Me!� as shown:
Quick and Easy Hide a Text Field
The following jQuery does the trick for me:
1<script type="text/javascript">
2$(function() {
3$('input[title=Hide Me!]').parent().parent().parent().hide();
4});
5</script>
The code is saying, �find me all input fields whose title = Hide Me!.  Then, get its parent and then next parent and the *next* parent (phew!) and invoke the hide() method on that thing, whatever it happens to be.
I figured out that parent structure by viewing the HTML for the form that SharePoint created as shown:
01    <TR>
02    <TD nowrap="true" valign="top" width="190px" class="ms-formlabel">
03    <H3 class="ms-standardheader">
04    <nobr>Hide Me!</nobr>
05    </H3>
06    </TD>
07    <TD valign="top" class="ms-formbody" width="400px">
08      <!-- FieldName="Hide Me!"
09      FieldInternalName="Hide_x0020_Me_x0021_"
10      FieldType="SPFieldText"
11      -->
12      <span dir="none">
13      <input
14      name="ctl00$m$g_bdb23c2c_fde7_495f_8676_69714a308d8e$ctl00$ctl04$ctl02$ctl00$ctl00$ctl04$ctl00$ctl00$TextField"
15      type="text"
16      maxlength="255"
17      id="ctl00_m_g_bdb23c2c_fde7_495f_8676_69714a308d8e_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00_TextField"
18      title="Hide Me!"
19      class="ms-long" />
20      <br>
21      </span>
22      </td>
23</tr>
This picture shows the same, but marked up with the parents:
Quick and Easy Hide a Text Field
The first parent (1) is a span tag.  Span�s parent (2) is a TD tag and then finally we get to the real parent I want to hide (3) which is the TR tag itself.

Ref: https://www.nothingbutsharepoint.com/sites/eusp/Pages/quick-and-easy-use-jquery-to-hide-a-text-field-on-a-sharepoint-form.aspx

No comments:

Post a Comment