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.

29 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.

Anonymous 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 Kumar 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.

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.

Unknown 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

Anonymous said...

I want to do a kind of complicated thing. I am trying to do some kind of Q&A with a list on Sharepoint. Users will be able to search (not filter) for there answer.

So I need to hide columns depending on users. And probably I will also need to only show certain items to certain users.

Do you know of any SharePoint addon that could do this or how could this code be modified to accomplish this.

The most important thing is that this restrictions shouldn't be overrun when running a search.

Also if you know of any site that has done something similar I will really appreciate if you can point me to them to get ideas and tips from them.
Thanks.

SharePoint Kings said...

Anonymous,

your requirement is so specific.
so we don't think you will get something readily available. and if you do please share with us and other users also.

Thanks

Anonymous said...

Hello, you can also check PowerForms which is a silverlight webpart that fully customizes sharepoint forms. You can easily hide columns, change position and a lot other advanced tasks without coding. Give it a try, i think it will solve a lot of problems.
http://www.bpc.gr/powerforms

Thakhi Shaik said...

how to do for allitems.aspx

SharePoint Kings said...

Thakhi Shaik,
you can change view on allitems.aspx :)

Rajesh CHENNUPATI said...

How to hide column in custom edit form (say EditFormNew.aspx)?

When I say "fld.showIneditform" it actually referring "editform.aspx"
but how can I achieve this for the new edit form?

please share us.

Rajesh CHENNUPATI said...

How to do this in EditformNew.aspx?

I have custom editform, I want to do the same in this custom page.

how to do it? Please share.

SharePoint Kings said...

Rajesh,

actually we had not tried what you are trying to do but there are several ways you can achieve this.

a) if you are creating custom editform.aspx from designer then you can hide field in designer only.

b) you can hide a list fields from javascript also refer this link
<a href="http://www.sharepointkings.com/2009/10/hiding-field-in-sharepoint-through.html>http://www.sharepointkings.com/2009/10/hiding-field-in-sharepoint-through.html</a>

c)This option we are not sure but you can give it a try. first hide field from default editform.aspx and then create a new copy from it. it might work or make you new one as default and then try to hide from code it might work.

Rajesh CHENNUPATI said...

Thanks alot for sharing.

I try the third one.


One more query I have, I am not sure where to ask. Please share your knowledge.

I have one datasheet view for one list. I want to filter the view depends on the query string. i googled a lot for the solution, but no luck.

Finally I came up with one solution, which is not accurate, but solves my problem temporarily.

My solution is, created one custom webpart and added to the page where i have my datasheet view. In page load of webpart i access the view and update the query. But by the time datasheetview is already dispalyed. so I had to redirect the entire page to see the latest results. It makes my entire page loads twice. How can I avoid it?

Please share your knowledge.

SharePoint Kings said...

HiRajesh,

Can you elaborate your View query? Is is something that can be solved using Filter web parts? If yes, then use filter webparts, get the Querystring and connect two web parts.

Rajesh CHENNUPATI said...

Thanks for the suggestion. But I am not sure about these filter webparts. I am using Foundation. (Does foundation have these webparts?). Though I have, I cannot use connection webparts as my view is datasheet view.

my requirement is simple. I want to filter Datasheet view dynamically (programatically!!!)

Do you have any solutions or suggestion?

Thanks in advance

reddy said...

administrator ento into site he create a site and add a employee list and add 100 records to the employee list.after that he logout hte site.after the enter one user enter he clicked the employee list he shows only 10 records only,how can ido this pls send it to my mail id sanjamala.malli@gmail.com

SharePoint Kings said...

reddy,

not able to understand your scenario can you please describe in detail.

Minerva said...

Works great !
But is there any possibility to associate 2 columns ?
for example i make 2 combo box columns. The first one has e.g. 3 main topics "cars,planes,ships" and in the second column all subcategories of possible companies e.g. "BMW, Mercedes, Boeing,.." Hope you get it. Is there any way, that i can manipulate it so that if you chose planes for example, it only shows the to planes associated companies ? and hides the rest ?

SharePoint Kings said...

@Minerva,

you can refer this link http://www.sharepointkings.com/2010/09/sharepoint-cascading-drop-downs-using.html to find cascading dropdown




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