SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

TIP: Creation of Lists with Hidden Columns via code:

Just thought I would share a tip that I found on the usenet group: microsoft.public.sharepoint.windowsservices.development
and in particular a post made by Colin Byrne colinb@>flexnetconsult.co.uk

I am summarizing the tip below in brief with thanks to Colin for the very useful information:
nbsp;
-------------------
Question: How does one create a list field that is hidden from users, but can be accessed for use in WorkFlows, etc?
Answer:Apparently, the hidden property of a list field (SPField) cannot be changed because it is blocked by the internal property CanToggleHidden being false. You can't adjust this though the public object model.

There are two work arounds, via code.

1. Add the field as XML

list.Fields.AddFieldAsXml("<Field DisplayName=\"NewField5\"
Type=\"Boolean\" Required=\"FALSE\" Name=\"NewField5\"
CanToggleHidden=\"TRUE\" Hidden=\"TRUE\"/>");

2. Set the internal CanToggleHidden property via reflection

string id=list.Fields.Add("NewField5",SPFieldType.Boolean,false);
SPField spfield=(SPField)list.Fields.GetField(id);

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

spfield.Hidden=true;
spfield.Update();


Posted 02-28-2008 8:12 AM by RChakravarti

Comments

joed wrote re: TIP: Creation of Lists with Hidden Columns via code:
on 02-28-2008 11:13 AM

You can also do this via the UI if the column is a Site Column.

I don't know why Sharepoint allows this for Site Columns, but not for list-specific columns.

Links (2/28/2008) « Steve Pietrek’s SharePoint Stuff wrote Links (2/28/2008) &laquo; Steve Pietrek&#8217;s SharePoint Stuff
on 02-28-2008 7:25 PM

Pingback from  Links (2/28/2008) &laquo; Steve Pietrek&#8217;s SharePoint Stuff

Rafelo wrote re: TIP: Creation of Lists with Hidden Columns via code:
on 06-19-2008 11:08 PM

I've created an application page that allows you to change the hidden attribute of any SharePoint list and list field. Your feeback is appreciated. The example can be found at:  blog.rafelo.com/.../exposing-and-creating-hidden-sharepoint.html

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.