Note: The technique described below is designed for SharePoint 2013. It could be adapted to 2010, but it’s not likely to work in 2010 with a simple copy/paste.
Note 2: This also should work just fine in Office 365, so get ready to knock some socks off.
In SharePoint 2013 we at itgroove have a lot of dashboards that we use for various purposes (the consultant dashboard, the GM dashboard, the president’s dashboard, and about 20 or so more) and one of the problems we always had was trying to jam more relevant content on to each dashboard, while simultaneously trying to reclaim as much space as possible (show the user what they need to see).
Enter the solution below. It’s a piece of code designed to hide empty web parts on a SharePoint dashboard, if the contents of that web part is empty. The reason this code works is that many of our web parts are simply list views of content already in SharePoint. This code is mainly only going to hide empty list view web parts – so if you have a dashboard with other empty web parts (whatever those may be), you may be out of luck, or you may be able to adapt it somehow, but this is all you get from me for free.
Note: We’re already including a jQuery library in our masterpage, and as such, I haven’t included a reference to bring in the jQuery library. Essentially, if you don’t already have jQuery elsewhere on the page (or in the masterpage like I do), you can include the following line above the code below.
/* If you don’t have jQuery already included, use this */
Essentially what the code below is doing is as follows. The waitForSPReady method is essentially sitting in a loop waiting for SharePoint to be ready to perform the action in the main body of code. If the method “SP.ClientContext.get_current” isn’t yet ready, SharePoint isn’t ready, so it will wait another 100ms and try again. Once SharePoint is ready, cleanupEmptyWebParts is called, which gathers a collection of all the web parts on the page that have the CSS class “ms-list-emptyText-compact”. It then iterates over that collection and for each valid item, it hides the web part if it successfully finds a match for the CSS class “ms-list-emptyText-compact” (you can never be too sure – that’s why there’s a bunch of code to test for it’s presence properly, and traverse around the DOM to find the right thing to hide).
To add this code to a web part page, just add a “Script Editor” web part to the page, and include the code in there.
Note: The code below should work in all major (modern) browsers that are supported by SharePoint. It’s been tested on all of the latest major browser versions (as of the publishing of this article). I do not know how well it will work on significantly older browsers (like IE 7/8/9).
(Note the 5 empty list view web parts + my tasks)
(Now only my tasks are left – I’d show you all the empty whitespace on the screen, but there’s not a lot of point )
Hopefully you find this as useful as I did.