Monday, February 18, 2008

Hide\Show Column in Sharepoint Form

You don't need desugner to hide or show a column in Display, Edit and New form of Sharepoint List. You could use Sharepoint Objects to set this programmatically-

Create a console application and Reference SharePoint Library-
The code will be
SPSite site = new SPSite(SiteName);
SPWeb web = site.OpenWeb();
SPList list = web.Lists[ListName]; // Get teh list
// Colname = Column to hide\show
list.Fields[ColName].ShowInEditForm = ShowInEditForm;
list.Fields[ColName].ShowInDisplayForm = ShowInDispForm;
list.Fields[ColName].ShowInNewForm = ShowInNewForm;
list.Fields[ColName].Update();

2 comments:

Anonymous said...

Hello Sanjay.

How do you implement this with restrictions in permissions. That is, If I am a user with administrator rights, this column is visible, otherwise, this column is hidden, validations are disabled (if it is a required field).

Thanks.

Sanjay Sansanwal said...

Hi there,

For that you need to modify DispForm.aspx form. Follow my tutorial about customising List form:
http://sansanwal.blogspot.com/2009/03/customise-sharepoint-list-forms-part-1.html