Thursday, November 08, 2012

WebpartToolPart category "Appearance" expanded by default - How to revert this ?

I had this problem myself the other day and it seems that all the solutions I found where mostly javascript based. But this is not a good solution in my mind, therefore I did some reflection and came up with this solution, all server side.

public override ToolPart[] GetToolParts()
{

var list = new List<ToolPart>(base.GetToolParts());

foreach (var wptp in list.OfType<WebPartToolPart>())
{
HideExpandIndexes(wptp);
}

return list.ToArray();
}


private void HideExpandIndexes(WebPartToolPart part)
{

var field_helper = part.GetType().GetField("_helper", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
var _helper = field_helper.GetValue(part);

var field_expandIndexes = _helper.GetType().BaseType.GetField("_expandIndexes", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
var _expandIndexes = field_expandIndexes.GetValue(_helper);

var field_items = _expandIndexes.GetType().GetField("_items", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
var _items = (Hashtable)field_items.GetValue(_expandIndexes);

_items.Clear();
}





The code can be improved to a finer detail, but it solves the problem for the moment.

No comments: