in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Rajiv and Michael's SharePoint discoveries and musings...

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();

Comments

 

joed said:

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.

February 28, 2008 11:13 AM
 

Links (2/28/2008) « Steve Pietrek’s SharePoint Stuff said:

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

February 28, 2008 7:25 PM
 

Rafelo said:

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

June 19, 2008 11:08 PM

Leave a Comment

(required )  
(optional )
(required )  
Add

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts