I have some code to do this using xlib. (enumerate top-level windows by XIDs, get their Unicode title names, PIDs and program names, activate and bring-to-front one of them using XRaiseWindow, XSetInputFocus, XMapwindow and a _NET_ACTIVE_WINDOW message to the WM).
But I'm not sure if this is really what you want/need.
And I can not guarantee that it works on all WM's.
I have only tested it on KWin (KDE) and there it is able to switch desktops and to raise and activate iconized windows.
OTOH, it uses plain X, so it's the WM's problem if it doesn't behave.
I had a hard time to get this working.