• Hide a required field in SharePoint list while adding new item

    by  • May 13, 2010 • MOSS 2007, PowerShell • 4 Comments

    There may be cases when we need to a hide required field while adding a new item to a list but at the same time you may want to show it while editing the item.

    The above case looks tricky but it is easily achievable using some extra bit of code or PowerShell

    C#

    string siteUrl = "http://mysite";
    SPSite site = new SPSite(siteUrl);
    SPWeb web = site.OpenWeb();
    site.AllowUnsafeUpdates = true;
    web.AllowUnsafeUpdates = true;
    SPList list = web.Lists["mylist"];
    SPField fldName = list.Fields["Name"];
    fldName.ShowInNewForm = false;
    fldName.Update();
    list.Update();
    

    PowerShell

    [system.reflection.assembly]::loadwithpartialname("microsoft.sharepoint")
    $site= New-Object Microsoft.SharePoint.SPSite ("http://d-dev1:1234/gp")
    $web=$site.OpenWeb()
    $list=$web.Lists["mylist"]
    $field = $list.Fields["Name"]
    $field.ShowInNewForm = $false
    $field.Update()
    

    If you want to hide this field in edit form, you can use below command

    fldName.ShowInEditForm = false;
    

    If you want to hide this field in display form, you can use below command

    fldName.ShowInDisplayForm = false;
    

    If you want to hide this field in list settings, you can use below command

    fldName.ShowInListSettings = false;
    

    If it is not a required filed then you can hide the item using Jquery in client side.

    <script type="text/javascript">// <![CDATA[
     $(document).ready(function() { $('nobr:contains("Name")').closest('tr').hide(); });
    // ]]></script>
    

    About

    Hojo Clement was working in US Technology in Trivandrum as a Senior Software Engineer. He has 6 years experience in working with Web Development Technologies inlcuding MOSS and ASP.net.

    4 Responses to Hide a required field in SharePoint list while adding new item

    1. Tim
      April 29, 2011 at 11:48 am

      Hello,

      When I attempt to implement the following script:

      //

      I get an error: Object Expected.

      Are there anyother missing elements I need to have in order to get beyond this error?

      Thank you for posting this.

      Sincerely,
      Tim

    2. Tim
      April 29, 2011 at 11:50 am

      Sorry, my script block did not paste. Must be getting filtered. It is the same script block as the one you posted on the bottom [not required using jquery].

      Thank you,
      Tim

    3. Sureshkumar Adaikan
      May 29, 2011 at 10:29 am

      Hi
      Where I need to write the above code? Please let me know

    4. Hojo
      June 1, 2011 at 5:52 am

      @Suresh,

      If you are using C# code, the better way to create an exe using the code and run in the server

      If you are using powershell, run the script in powershell editor in the server

      If the Jquery is your option, add a content editor webpart in newform.aspx page of that list and add the script there.

    Leave a Reply

    Your email address will not be published. Required fields are marked *