0

I have added a custom column named "Date" inside my announcement list, but i want to hide this column from the DispForm.aspx page. i tried the following two methods to hide the Date column but none of them worked:-

  1. inside the DispForm.aspx, i add the following code:-

    <asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
    
    <script type="text/javascript">
       var control = findacontrol("Date");
       control.parentNode.parentNode.style.display="none";
    
    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];      }
       }
    }
    

    but it did not hide the Date field.

  2. i also try the following power shell script:-

    # First load SharePoint Core Assembly [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)

    $url = "http://sharepointdev/";
    $list = "List";
    $fieldname = "Date";
    
    #Setting up context
    $$contextWeb = Get-SPWeb  http://sharepointdev;
    
    $list = $contextWeb.Lists.TryGetList($list);
    $field = $list.Fields[$fieldname];
    
    # Controls Field in Edit Form
    $field.ShowInEditForm = 1;
    # Controls Field in New Form
    $field.ShowInNewForm = 0;
    # Controls Field in New Form
    $field.ShowInDisplayForm = 1;
    # Hides fields from list settings
    $field.ShowInListSettings = 1;
    # Hides fields from version history
    $field.ShowInVersionHistory = 1;
    # Hides fields form selection in views
    $field.ShowInViewForms = 1;
    # Don't forget to update this field
    $field.Update();
    # And finally dispose everything.
    $contextWeb.Dispose();
    $contextSite.Dispose();
    

but i got the following errorinside the power shell window:-

PS C:\Users\Administrator> $list = $contextWeb.Lists.TryGetList($list); You cannot call a method on a null-valued expression. At line:1 char:1 + $list = $contextWeb.Lists.TryGetList($list); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

so can anyone advice on how i can hide the custom column from my announcement APP?

EDIT

Here is the updated code:-

$url = "http://sharepointdev/";
$list = "News & Announcement";
$fieldname = "Date";

#Setting up context
$webApp = Get-SPWebApplication http://sharepointdev

$list = $contextWeb.Lists.TryGetList($list);
$field = $list.Fields[$fieldname];

# Controls Field in Edit Form
$field.ShowInEditForm = 1;
# Controls Field in New Form
$field.ShowInNewForm = 1;
# Controls Field in New Form
$field.ShowInDisplayForm = 0;
# Hides fields from list settings
$field.ShowInListSettings = 1;
# Hides fields from version history
$field.ShowInVersionHistory = 1;
# Hides fields form selection in views
$field.ShowInViewForms = 1;
# Don't forget to update this field
$field.Update();
# And finally dispose everything.
$contextWeb.Dispose();
$contextSite.Dispose();

but it is raising the following error:-

> PS C:\Users\Administrator> $webApp = Get-SPWebApplication
> http://sharepointdev PS C:\Users\Administrator> PS
> C:\Users\Administrator> $list = $contextWeb.Lists.TryGetList($list);
> You cannot call a method on a null-valued expression. At line:1 char:1
> + $list = $contextWeb.Lists.TryGetList($list);
> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
>     + FullyQualifiedErrorId : InvokeMethodOnNull
2

The problem is you are using double dollar symbol in $$contextWeb so when you try to use it with one $ symbol the variable is null.

In order to hide the column in DispForm you have to use $field.ShowInDisplayForm = 0 instead of $field.ShowInDisplayForm = 1

Finally, update not only the field $field.Update() but also the list $list.Update()

UPDATED

$url = "http://sharepointdev/";
$fieldname = "Date";

#Setting up context
$web = Get-SPWeb -Identity $url

$list = $web.Lists["InternalNameOfTheList"]
$field = $list.Fields[$fieldname];

# Controls Field in Edit Form
$field.ShowInEditForm = 1;
# Controls Field in New Form
$field.ShowInNewForm = 1;
# Controls Field in New Form
$field.ShowInDisplayForm = 0;
# Hides fields from list settings
$field.ShowInListSettings = 1;
# Hides fields from version history
$field.ShowInVersionHistory = 1;
# Hides fields form selection in views
$field.ShowInViewForms = 1;
# Don't forget to update this field
$field.Update();
    $list.Update();
# And finally dispose everything.
$web.Dispose();

Hope it helps

  • the double dollar sign is not the problem, i might have typed it wrongly on this post only ,as the error is after this statement. can you check my edit please? – john Gu Oct 2 '13 at 8:55
  • 1
    In your edit you have written $webApp = Get-SPWebApplication sharepointdev and after $list = $contextWeb.Lists.TryGetList($list); you have to put in this case $list = $webApp.Lists.TryGetList($list); – Alvmad Oct 2 '13 at 9:21
  • thanks for your reply. but using the updated code will raise an error also as follow. PS C:\Users\Administrator> $list = $webApp.Lists.TryGetList($list); You cannot call a method on a null-valued expression. At line:1 char:1 + $list = $webApp.Lists.TryGetList($list); – john Gu Oct 2 '13 at 10:23
  • 1
    Look at the code you are trying to implement. With Get-SPWebApplication you obtain a Web Application not the SPWeb element. I updated the post with the code that should work – Alvmad Oct 2 '13 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.