Find the hidden smart part
Nicolas Galler | March 18, 2008How to programmatically find if a smart part is present on a page (for example, if the behavior of one smart part depends on whether another smart part is loaded… in my case I wanted to show a button on my smart part ONLY if some other custom smart part had been added to the page):
- If the smart part you need to find is on any workspace but DialogWorkspace, you can use FindControl (or FindControlRecursive – google for the code). Remember the smart part will only show up if it is displayed in the particular mode (for example if it is a Detail mode smart part it won’t be anywhere in List mode)
- If the smart part is on the DialogWorkspace it is a bit trickier. If it is not currently displayed, it won’t be there as a control, but instead it will be in the private variable _mySmartParts of the DialogWorkspace.
So here is my FindSmartPart function:
/// <summary> /// Find a smart part under the specified work item. /// If the smart part is not on the page, return null. /// </summary> /// <param name="parent"></param> /// <param name="smartPartId"></param> /// <returns></returns> private Control FindSmartPart(UIWorkItem parent, String smartPartId) { foreach (var ws in parent.Workspaces) { if (ws.Value is DialogWorkspace) { // in this case peek in _mySmartParts FieldInfo field = typeof(DialogWorkspace).GetField("_mySmartParts", BindingFlags.Instance | BindingFlags.NonPublic); if (field == null) throw new InvalidOperationException("Field _mySmartParts not found in DialogWorkspace"); Dictionary<object, ISmartPartInfo> smartParts = (Dictionary<object, ISmartPartInfo>)field.GetValue(ws.Value); if (smartParts != null) { foreach (object smartPart in smartParts.Keys) { Control c = smartPart as Control; if (c != null && c.ID == smartPartId) return c; } } } foreach (var smartPart in ws.Value.SmartParts) { Control c = (Control)smartPart; if (c.ID == smartPartId) return c; } } return null; }
Well, nobody said it would be pretty – but, it gets the job done.





