Wednesday, October 27, 2010

SharePoint 2010, Programatically change Hidden value of a field if CanToggleHidden is false

We can not delete hidden fields directly from sharepoint list forms. In order to delete it, we need to set CanToggleHidden attribute to true & then we can unhide the field & can delete it using Object Model.


Add System.Reflection namespace.

  using (SPSite site = new SPSite(site url))
    {
    using (SPWeb web = site.OpenWeb())
    {
    SPList testList = web.Lists["Test"];
    web.AllowUnsafeUpdates = true;
    SPField objTitle = testList.Fields["Title"];
    Type type = objTitle.GetType();
    MethodInfo mi = type.GetMethod("SetFieldBoolValue",BindingFlags.NonPublic | BindingFlags.Instance);
    mi.Invoke(objTitle, new object[] { "CanToggleHidden", true });
    objTitle.Hidden = true;   //objTitle.Hidden = false;
    objTitle.Update();
                                 
    }
  }

Once CanToggleHidden attribute is set to "true" , so you can unhide the field as you wish. and further you can delete the hidden field.

No comments:

Post a Comment