ReadOnly, Hidden and Sealed Fields

That’s another question that I Answered in MSDN Social forums and thought of sharing with you here 🙂
Q :
The documentation regarding those attributes is not informative enough.
Hidden :
Optional Boolean . If TRUE , the field is completely hidden from the user interface. Setting ReadOnly to TRUE means the field is not displayed in New or Edit forms but can still be included in views.
ReadOnly :
Optional Boolean . If TRUE , the field is not displayed in New or Edit forms but can be included in views. Setting the Hidden attribute to TRUE completely hides the field from the UI.
Anybody knows the differences ?And what about CanToggleHidden attribute ?

A :

Similarities : Both aren’t displayed anywhere unless you configure a view to show any of them.
Differences : Hidden Fields can be updated through the object model but ReadOnly fields can’t be updated through OM like “Created” And “Created By” fields.

If you are still confused, consider the following example :You have a field that you use only by the object model for setting or getting a flag and you don’t want the field to be visible to anybody and non updatable through the UI, however you want it to be updatable through the object model.
For me, I was creating a list that stores the applications of Scholarships offered by a foundation and we needed the status of each application to be totally hidden but updatable through the OM.
Theoritically, this should be an east task :
SPSite site=SPContext.Current.Site;
SPWeb web=site.OpenWeb();
SPList list =web.Lists[“ScholarshipApplicationsList”];
string id=list.Fields.Add(“Status”,SPFieldType.Boolean,false);
SPField statusField = list.Fields.GetField(id);

But if you try this you’ll receive a nice exception
Why? The Hidden property checks if CanToggleHidden property is true and throws an exception if its not.
Fair enough, all we do then is set the CanToggleHidden property to true and do the update but there is no such a property in the object model.
So I’ve decided to use “Reflection” ..
SPField statusField= web.Lists[“ScholarshipApplicationsList”].Fields[“Status”];
Type type = spfield.GetType();
MethodInfo mi = type.GetMethod(”SetFieldBoolValue”,
BindingFlags.NonPublic BindingFlags.Instance);
mi.Invoke(spfield, new object[] { “CanToggleHidden”, true });
statusField.Hidden = true;
And if you want to use CAML, this is much more easier :
<field displayname=”Status” type=”Boolean” required=”FALSE” name=”NewField” cantogglehidden=”TRUE” hidden=”TRUE”>

Q :
Thanks Ayman El-Hattab.I’ve created a few hidden fields but now I’m trying to delete them using the following code :
But I’m gettin the following exception :
“You cannot delete a hidden column ”

A :
Unhide the field , then delete it. CanToggleHidden attribute is now set to true, so you can hide and unhide the field as you wish.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s