Tuesday, May 20, 2008

How to hide a column of SharePoint list in different mode (Add / Edit / Display Mode)?

Most of the time we have requirement like that this field should be shown only in new mode or it should not be shown only in Edit mode not in new mode.

in simple word every one is asking the same question
"How to hide a column of SharePoint list in different mode (Add / Edit / Display Mode)?"

We can hide it in view by not to show in grid view but what to do if you do not want to see that field in Dispforms.aspx?

Here is the way how to do that.

Check the code snippet first

SPSite objSite = SPContext.Current.Site;
SPWeb objWeb = objSite.OpenWeb();
SPList objList = objWeb.Lists[“Name of the list”];
SPField objField = objList.Fields[“Name of the column”];


This SPField has following properties.

objField.ShowInDisplayForm //show in display mode(dispform.aspx)
objField.ShowInEditForm //show in edit mode(editform.aspx)
objField.ShowInListSettings //show in list setting page of list where u can set order or remove that field.
objField.ShowInNewForm //show in new form (newform.aspx)
objField.ShowInVersionHistory // displayed in the page for viewing list item versions. objField.ShowInViewForms //show in grid view


All the properties shows their setting meaning by their name.

Just set the properties you want and uodate field and list.

objField.Update();
objList.Update();


See the magic.

Another problem is after hiding column how to set its data?

In event handler / item handler you can use

properties.AfterProperties[“Internal name of that column”] = “assign text”;

You will get your result.
Only thing to keep in mind that
If column is hidden then u cannot retrieve its value.


Some time it will show error

“One or more field types are not installed properly. Go to the list settings page to delete these fields.”


If you get these error there is only two scenarios is there.
1) If you have any custom field then it was not properly installed.
2) If you have do not have the field with that name and you are assigning value to it.

Just do a due diligence and check this two scenarios and your error might be solved.

15 comments:

Anonymous said...

Where do you write this code?
when will this fired.

Parth Patel said...

this code is kind of an admin/development code.
so you can create a webpart for admin and write this code in that webpart.

For the development also you can have this code in webpart only.

Another way is if you are creating list dynamically then at the time of creation you can execute this code.

We had created a webpart soon we upload that.

Gary said...

It'd be great to check out the webpart if/when you upload this. Any idea when this might happen?

Anonymous said...

I've done something similar, but in PowerShell :)

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = new-object Microsoft.SharePoint.SPSite("http://intranet")
$web = $site.OpenWeb()
$list = $web.Lists["Announcements"]
$field = $list.Fields["Page Image"]
#$field.ShowInNewForm = "true"
$field.ShowInDisplayForm = $false
#$field.ShowInEditForm = $true
#$field.ShowInListSettings = $true
#$field.ShowInViewForms = $true
$field.Update()
$web.Dispose()
$site.Dispose()

Works like a charm.

anil said...

Hi

I have tried every thing to achieve exactly what we are speaking in this blog.

My code sniipet

Guid listGuid = new Guid(lstSiteLists.SelectedValue);
Guid fieldGuid = new Guid(lstFields.SelectedValue);

SPWeb site = SPContext.Current.Site.OpenWeb();//)
site.AllowUnsafeUpdates = true;
SPList list = site.Lists[listGuid];
SPField field = list.Fields[fieldGuid];

lblFieldID.Text = field.Id.ToString();
lblFieldInternalName.Text = field.InternalName;

if (!(field.CanToggleHidden))
{
Type type = field.GetType();
MethodInfo mi = type.GetMethod("SetFieldBoolValue", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Invoke(field, new object[] { "CanToggleHidden", true });
}

field.Hidden = chkFieldHidden.Checked;
field.ShowInEditForm=CheckBoxShowInEdit.Checked;
field.ShowInNewForm=CheckBoxShowInNew.Checked ;
field.ShowInViewForms=CheckBoxShowInView.Checked ;

field.Update();
list.Update();
site.Update();
site.AllowUnsafeUpdates = false;

swastik nath said...

hi anil,
can you tell me where exactly i need to write this code ?
can i write it in itemadding event handler?
but i need to hide the columns in newform.aspx so i need to fire even before that. so where exactly do i need to write the code.

Parth Patel said...

@ swastik nath,
you can create list as a feature and on feature activated event you can run this code?

other wise you can create admin page where admin can do this stuff so admin can set the things before user can open newform.aspx.

Anonymous said...

hi
can i hide this column for a specific user? other users are allowed to see this column.

SharePoint Kings said...

that you cannot do this with this property.

this property will hide for all the user including system account.

Anonymous said...

Nice !!!!!!!!

Hide columns in SharePoint, it is simple.

Try this too,
How to hide columns in SharePoint

giriraj said...

How to Hide a document for the peticular user?..

SharePoint Kings said...

Hi,

Thanks for taking your time.

You can do this with the permission level. Don't give permission on document level to specific user. User won't be able to see the document then.

أحمد بن عمر باجابر said...

hiding some columns from particular users !!

WOW!

Can some one help me how to achieve this feature through coding?!

awaiting

SharePoint Kings said...

not possible to update column for each users.

create a webpart put on each page.

check in that webpart which user has permission and render a javascript which hide field. that's the only way we found.

check this url

http://www.sharepointkings.com/2009/10/hiding-field-in-sharepoint-through.html

may help how to do it in javascript.

Eitan said...

You can hide a column for a specific user. it can be done with 3rd party addons for SharePoint.


Take a look at Smart List Lite of Infowise. its free

http://www.infowisesolutions.com/product.aspx?id=SmartListLite




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com