Reputation: 15912
I'm trying to hide the "Title" field in a list. This doesn't seem to work:
SPList myList;
...
SPField titleField = myList.Fields.GetField("Title");
//titleField.PushChangesToLists = true; <-- doesn't seem to make a difference
titleField.ShowInEditForm = false;
titleField.ShowInDisplayForm = false;
titleField.ShowInNewForm = false;
titleField.Update();
//myList.Update(); <-- make no difference
What am I doing wrong?
Upvotes: 8
Views: 15658
Reputation: 1109
None of the above examples of setting Hidden true will work unless CanToggleHidden has a value of true. The problem is, CanToggleHidden only has a Get, not a Set, so you have to perform a radical "SharePoint programming gymnastics stunt" using reflection to first flip CanToggleHidden from false to true. Once you have done that, you can change Hidden to true (or back to false). There are plenty of examples out on the web (although not all of them are written correctly). If needed, I can probably dig up a PowerShell example that works.
if(field.CanToggleHidden) {
field.Hidden = false;
}
else
{
// display an error message or write to your favorite logging location
// explaining that there is no hope of changing the value of Hidden until
// CanToggleHidden changes to TRUE first.
}
Upvotes: 2
Reputation: 1044
The solution above is for hiding the field everywhere. It will also be hidden in the column overview of your list.
If you only want to hide the field in a particular list. Or if you still to manipulate the field (set back to visible) by using the list settings page. You need to set the "Hidden" property of the field in the "FieldLinks" property of the list.
myList.FieldLinks["SomeField"].Hidden = true;
Upvotes: 0
Reputation: 1
try this one this will work... Title field is named as LinkTitle... other fields can be hidden in the same way.
SPView view = list.DefaultView;
if(view.ViewFields.Exists("LinkTitle"))
{
view.ViewFields.Delete("LinkTitle");
view.Update();
}
Upvotes: 0
Reputation: 31
There is a price you pay when you use Hidden property.
It's been discovered that setting a column hidden will remove the ability to delete it via code.
Upvotes: 0
Reputation: 4152
I believe visibility of fields in lists are controlled by the default view that the user "gets". Don't you want to modify the view? I know you can get the Views for a list, as well as the default view.
I'm just spit-balling here...
Upvotes: 0
Reputation: 7774
Make sure you are grabbing a new SPWeb instance.
using (SPSite site = new SPSite(webUrl))
{
using (SPWeb web = site.OpenWeb())
{
try
{
//... Get SPList ...
}
}
}
Upvotes: 0