TIP: Creation of Lists with Hidden Columns via code:

February 28th, 2008 by rchakravarti Leave a reply »

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

Advertisement

2 comments

  1. Silver says:

    A way to set CanToggleHidden attribute to false was exactly what I was looking for. Thanks!

  2. rchakravarti says:

    You’re welcome, glad to help…

Leave a Reply