Sanjaya on SharePoint/Microsoft technologies

a blog on technologies like .net, MOSS 2007, wss 3.0 etc

Hide and Unhide Sharepoint fields through reflection

Posted by sNjY on July 28, 2008

There were couple of fields that were created as part of content type and enabled in a document libraries.

As the fields need not be shown in list diplay form, they were set to hidden but search could not index them and eventually searching based on those fields was not possible. So decided to unhide those fields through WSSListConfigurator tool downloaded from codeplex but the system didn’t allow to unhide the fields The reason  was that the fields hidden property was dependent on another property called cantogglehidden. The cantogglehidden property was readonly and could not be changed.

But with this piece of code below, it was all possible, Code is self explainatory…

SPSite site = new SPSite(“http://ressintspwebd01:99“);
SPWeb web = site.RootWeb;
web.AllowUnsafeUpdates =
true;
SPField spfield = web.Lists["Policies"].Fields["Document Type"];
Type type = spfield.GetType();
MethodInfo mi = type.GetMethod(“SetFieldBoolValue”,
BindingFlags.NonPublic | BindingFlags.Instance);
mi.Invoke(spfield,
new object[] { “CanToggleHidden”, true });
spfield.Hidden =
false;
spfield.Update();
 

Thanks to jigar for sharing tis piece of useful code.

3 Responses to “Hide and Unhide Sharepoint fields through reflection”

  1. Faizan Ahmed said

    I really appreciate this solution. I had night mare with Cross List issue but you solution has resolved my issue.

  2. Jason said

    Hello -

    quick question:

    how do i hide the option “view all responses” once a participant completes a survey?

  3. bazztrap said

    http://spyralout.com/2009/09/18/masking-sharepoint-created-by-and-modified-by-making-list-anonymous/

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>