{"id":1270,"date":"2014-06-22T22:14:51","date_gmt":"2014-06-23T05:14:51","guid":{"rendered":"https:\/\/mmman.itgroove.net\/?p=1270"},"modified":"2023-02-24T21:48:26","modified_gmt":"2023-02-24T21:48:26","slug":"hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page","status":"publish","type":"post","link":"https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/","title":{"rendered":"Hiding Empty SharePoint Web Parts Using JavaScript\/jQuery on a Dashboard Page"},"content":{"rendered":"<p>Note: The technique described below is designed for SharePoint 2013.\u00a0 It could be adapted to 2010, but it\u2019s not likely to work in 2010 with a simple copy\/paste.<\/p>\n<p>Note 2: This also should work just fine in Office 365, so get ready to knock some socks off.<\/p>\n<p>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\u2019s 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).<\/p>\n<p>Enter the solution below.\u00a0 It\u2019s a piece of code designed to hide empty web parts on a SharePoint dashboard, if the contents of that web part is empty.\u00a0 The reason this code works is that many of our web parts are simply list views of content already in SharePoint.\u00a0 This code is mainly only going to hide empty list view web parts \u2013 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.\u00a0 <img decoding=\"async\" class=\"wlEmoticon wlEmoticon-winkingsmile\" src=\"https:\/\/mmman.itgroove.net\/wp-content\/uploads\/mmman\/2014\/06\/wlEmoticon-winkingsmile.png\" alt=\"Winking smile\" \/><\/p>\n<p>Note: We\u2019re already including a jQuery library in our masterpage, and as such, I haven\u2019t included a reference to bring in the jQuery library.\u00a0 Essentially, if you don\u2019t already have jQuery elsewhere on the page (or in the masterpage like I do), you can include the following line above the code below.<\/p>\n<p><span style=\"font-family: Courier New;font-size: small\">\/* If you don\u2019t have jQuery already included, use this *\/<br \/>\n&lt;script type=&#8221;text\/javascript&#8221; src=&#8221;http:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.6.2\/jquery.min.js&#8221;&gt;&lt;\/script&gt;<\/span><\/p>\n<p>Essentially what the code below is doing is as follows.\u00a0 The <span style=\"font-family: Courier New\">waitForSPReady<\/span> method is essentially sitting in a loop waiting for SharePoint to be ready to perform the action in the main body of code.\u00a0 If the method \u201c<span style=\"font-family: Courier New\">SP.ClientContext.get_current<\/span>\u201d isn\u2019t yet ready, SharePoint isn\u2019t ready, so it will wait another 100ms and try again.\u00a0 Once SharePoint is ready, <span style=\"font-family: Courier New\">cleanupEmptyWebParts<\/span> is called, which gathers a collection of all the web parts on the page that have the CSS class \u201c<span style=\"font-family: Courier New\">ms-list-emptyText-compact<\/span>\u201d.\u00a0 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 \u201c<span style=\"font-family: Courier New\">ms-list-emptyText-compact<\/span>\u201d (you can never be too sure \u2013 that\u2019s why there\u2019s a bunch of code to test for it\u2019s presence properly, and traverse around the DOM to find the right thing to hide).<\/p>\n<p>Note: The variable \u201c<span style=\"font-family: Courier New\">itgDebugEnabled<\/span>\u201d allows you to manually turn the functionality on or off, by setting this in a JavaScript variable somewhere in the page (set the variable to \u201ctrue\u201d to stop the code from processing).\u00a0 If you want to set the variable, either you can set it in this code (somewhere earlier\/outside the method) or it can be set in any other included file \/ script block on the page.<\/p>\n<p>To add this code to a web part page, just add a \u201cScript Editor\u201d web part to the page, and include the code in there.<\/p>\n<p>Note: The code below should work in all major (modern) browsers that are supported by SharePoint.\u00a0 It\u2019s been tested on all of the latest major browser versions (as of the publishing of this article).\u00a0 I do not know how well it will work on significantly older browsers (like IE 7\/8\/9).<\/p>\n<p>The code:<br \/>\n[code language=&#8221;javascript&#8221;]<br \/>\n&lt;script type=&quot;text\/javascript&quot;&gt;<br \/>\n $(document).ready(function(){<br \/>\n     setTimeout(waitForSPReady, 100);<br \/>\n });<\/p>\n<p>function waitForSPReady()<br \/>\n {<br \/>\n     if (typeof SP == &quot;object&quot; &amp;&amp; typeof SP.ClientContext == &quot;function&quot; &amp;&amp; typeof SP.ClientContext.get_current == &quot;function&quot;)<br \/>\n     {<br \/>\n         cleanupEmptyWebParts();<br \/>\n     }<br \/>\n     else<br \/>\n     {<br \/>\n         setTimeout(waitForSPReady, 100);<br \/>\n     }<br \/>\n }<\/p>\n<p>function cleanupEmptyWebParts()<br \/>\n {<br \/>\n     if (typeof itgDebugEnabled != &quot;undefined&quot; &amp;&amp; itgDebugEnabled == true)<br \/>\n     {<br \/>\n         return;<br \/>\n     }<br \/>\n     var outerEltClass = &quot;ms-webpartzone-cell&quot;;<br \/>\n     var eltList = $(&quot;.ms-list-emptyText-compact&quot;);<br \/>\n     var outermostID = &quot;_invisibleIfEmpty&quot;;<\/p>\n<p>    if (eltList.size() &gt; 0)<br \/>\n     {<br \/>\n         var iter = null;<br \/>\n         var curItem = null;<br \/>\n         for (iter = 0; iter &lt; eltList.size(); iter++)<br \/>\n         {<br \/>\n             curItem = eltList[iter];<br \/>\n             if (curItem.id == outermostID)<br \/>\n             {<br \/>\n                 continue;<br \/>\n             }<br \/>\n             else<br \/>\n             {<br \/>\n                 while (curItem.id != outermostID)<br \/>\n                 {<br \/>\n                     var foundItem = false;<br \/>\n                     var cIter = -1;<br \/>\n                     var maxIter = curItem.classList.length;<br \/>\n                     if (maxIter &gt; 0)<br \/>\n                     {<br \/>\n                         do<br \/>\n                         {<br \/>\n                             cIter++;<br \/>\n                             if (curItem.classList[cIter] == outerEltClass)<br \/>\n                             {<br \/>\n                                 foundItem = true;<br \/>\n                                 break;<br \/>\n                             }<br \/>\n                         }<br \/>\n                         while (curItem.classList[cIter] != outerEltClass &amp;&amp; cIter &lt; (maxIter &#8211; 1))<br \/>\n                         if (foundItem == true)<br \/>\n                         {<br \/>\n                             $(curItem).css(&quot;display&quot;, &quot;none&quot;);<br \/>\n                             break;<br \/>\n                         }<br \/>\n                     }<br \/>\n                     curItem = curItem.parentNode;<br \/>\n                 }<br \/>\n             }<br \/>\n         }<br \/>\n     }<br \/>\n }<br \/>\n &lt;\/script&gt;<br \/>\n[\/code]<\/p>\n<p>Before:<br \/>\n(Note the 5 empty list view web parts + my tasks)<\/p>\n<p><a href=\"https:\/\/mmman.itgroove.net\/wp-content\/uploads\/mmman\/2014\/06\/image3.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" src=\"http:\/\/mmman.itgroove.net\/wp-content\/uploads\/mmman\/2014\/06\/image_thumb3.png\" alt=\"image\" width=\"704\" height=\"310\" border=\"0\" \/><\/a><\/p>\n<p>After:<br \/>\n(Now only my tasks are left \u2013 I\u2019d show you all the empty whitespace on the screen, but there\u2019s not a lot of point <img decoding=\"async\" class=\"wlEmoticon wlEmoticon-smile\" src=\"https:\/\/mmman.itgroove.net\/wp-content\/uploads\/mmman\/2014\/06\/wlEmoticon-smile.png\" alt=\"Smile\" \/>)<\/p>\n<p><a href=\"https:\/\/mmman.itgroove.net\/wp-content\/uploads\/mmman\/2014\/06\/image4.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" src=\"http:\/\/mmman.itgroove.net\/wp-content\/uploads\/mmman\/2014\/06\/image_thumb4.png\" alt=\"image\" width=\"660\" height=\"190\" border=\"0\" \/><\/a><\/p>\n<p>Hopefully you find this as useful as I did.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Note: The technique described below is designed for SharePoint 2013.\u00a0 It could be adapted to 2010, but it\u2019s 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 &hellip; <a href=\"https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/\"><\/a><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[4,24,6,7],"tags":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Hiding Empty SharePoint Web Parts Using JavaScript\/jQuery on a Dashboard Page - Archive<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hiding Empty SharePoint Web Parts Using JavaScript\/jQuery on a Dashboard Page - Archive\" \/>\n<meta property=\"og:description\" content=\"Note: The technique described below is designed for SharePoint 2013.\u00a0 It could be adapted to 2010, but it\u2019s 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 &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/\" \/>\n<meta property=\"og:site_name\" content=\"Archive\" \/>\n<meta property=\"article:published_time\" content=\"2014-06-23T05:14:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-24T21:48:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/mmman.itgroove.net\/wp-content\/uploads\/mmman\/2014\/06\/wlEmoticon-winkingsmile.png\" \/>\n<meta name=\"author\" content=\"Colin Phillips (Alumni)\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Colin Phillips (Alumni)\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/\",\"url\":\"https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/\",\"name\":\"Hiding Empty SharePoint Web Parts Using JavaScript\/jQuery on a Dashboard Page - Archive\",\"isPartOf\":{\"@id\":\"https:\/\/regroove.ca\/archive\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/mmman.itgroove.net\/wp-content\/uploads\/mmman\/2014\/06\/wlEmoticon-winkingsmile.png\",\"datePublished\":\"2014-06-23T05:14:51+00:00\",\"dateModified\":\"2023-02-24T21:48:26+00:00\",\"author\":{\"@id\":\"https:\/\/regroove.ca\/archive\/#\/schema\/person\/adeb0df1cc7a862160be620ca7eace1b\"},\"breadcrumb\":{\"@id\":\"https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/#primaryimage\",\"url\":\"https:\/\/mmman.itgroove.net\/wp-content\/uploads\/mmman\/2014\/06\/wlEmoticon-winkingsmile.png\",\"contentUrl\":\"https:\/\/mmman.itgroove.net\/wp-content\/uploads\/mmman\/2014\/06\/wlEmoticon-winkingsmile.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog Archive\",\"item\":\"https:\/\/regroove.ca\/archive\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Hiding Empty SharePoint Web Parts Using JavaScript\/jQuery on a Dashboard Page\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/regroove.ca\/archive\/#website\",\"url\":\"https:\/\/regroove.ca\/archive\/\",\"name\":\"Archive\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/regroove.ca\/archive\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/regroove.ca\/archive\/#\/schema\/person\/adeb0df1cc7a862160be620ca7eace1b\",\"name\":\"Colin Phillips (Alumni)\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/regroove.ca\/archive\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/14eeab0d377e9630e0983d9c08911979?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/14eeab0d377e9630e0983d9c08911979?s=96&d=mm&r=g\",\"caption\":\"Colin Phillips (Alumni)\"},\"url\":\"https:\/\/regroove.ca\/archive\/author\/cphillips\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Hiding Empty SharePoint Web Parts Using JavaScript\/jQuery on a Dashboard Page - Archive","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/","og_locale":"en_US","og_type":"article","og_title":"Hiding Empty SharePoint Web Parts Using JavaScript\/jQuery on a Dashboard Page - Archive","og_description":"Note: The technique described below is designed for SharePoint 2013.\u00a0 It could be adapted to 2010, but it\u2019s 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 &hellip;","og_url":"https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/","og_site_name":"Archive","article_published_time":"2014-06-23T05:14:51+00:00","article_modified_time":"2023-02-24T21:48:26+00:00","og_image":[{"url":"https:\/\/mmman.itgroove.net\/wp-content\/uploads\/mmman\/2014\/06\/wlEmoticon-winkingsmile.png"}],"author":"Colin Phillips (Alumni)","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Colin Phillips (Alumni)","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/","url":"https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/","name":"Hiding Empty SharePoint Web Parts Using JavaScript\/jQuery on a Dashboard Page - Archive","isPartOf":{"@id":"https:\/\/regroove.ca\/archive\/#website"},"primaryImageOfPage":{"@id":"https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/#primaryimage"},"image":{"@id":"https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/#primaryimage"},"thumbnailUrl":"https:\/\/mmman.itgroove.net\/wp-content\/uploads\/mmman\/2014\/06\/wlEmoticon-winkingsmile.png","datePublished":"2014-06-23T05:14:51+00:00","dateModified":"2023-02-24T21:48:26+00:00","author":{"@id":"https:\/\/regroove.ca\/archive\/#\/schema\/person\/adeb0df1cc7a862160be620ca7eace1b"},"breadcrumb":{"@id":"https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/#primaryimage","url":"https:\/\/mmman.itgroove.net\/wp-content\/uploads\/mmman\/2014\/06\/wlEmoticon-winkingsmile.png","contentUrl":"https:\/\/mmman.itgroove.net\/wp-content\/uploads\/mmman\/2014\/06\/wlEmoticon-winkingsmile.png"},{"@type":"BreadcrumbList","@id":"https:\/\/regroove.ca\/archive\/2014\/06\/22\/hiding-empty-sharepoint-web-parts-using-javascriptjquery-on-a-dashboard-page\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Archive","item":"https:\/\/regroove.ca\/archive\/"},{"@type":"ListItem","position":2,"name":"Hiding Empty SharePoint Web Parts Using JavaScript\/jQuery on a Dashboard Page"}]},{"@type":"WebSite","@id":"https:\/\/regroove.ca\/archive\/#website","url":"https:\/\/regroove.ca\/archive\/","name":"Archive","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/regroove.ca\/archive\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/regroove.ca\/archive\/#\/schema\/person\/adeb0df1cc7a862160be620ca7eace1b","name":"Colin Phillips (Alumni)","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/regroove.ca\/archive\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/14eeab0d377e9630e0983d9c08911979?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/14eeab0d377e9630e0983d9c08911979?s=96&d=mm&r=g","caption":"Colin Phillips (Alumni)"},"url":"https:\/\/regroove.ca\/archive\/author\/cphillips\/"}]}},"_links":{"self":[{"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/posts\/1270"}],"collection":[{"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/comments?post=1270"}],"version-history":[{"count":1,"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/posts\/1270\/revisions"}],"predecessor-version":[{"id":1875,"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/posts\/1270\/revisions\/1875"}],"wp:attachment":[{"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/media?parent=1270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/categories?post=1270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/regroove.ca\/archive\/wp-json\/wp\/v2\/tags?post=1270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}