Here is a neat trick that can make your team pages a lot more efficient. Using jQuery and jQuery UI’s tabs feature, we can automatically “tabify” all web parts contained within a web part zone.
PREREQUISITES FOR THIS SOLUTION TO WORK:
- This solution has been tested to work for SharePoint 2013 and SharePoint within Office 365 only.
- All web parts that you want to “tabify” should be placed within the same web part zone on your page.
LIST OF STEPS:
-
- Create a new .txt. file with the following code in it. Upload the file somewhere on your website. For the sake of this example, I placed the file on my site’s /siteassets/ folder. Therefore, the URL will be /samples/siteassets/tabify.txt/
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <script type="text/javascript" src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script> <link rel="stylesheet" type="text/css" type="text/javascript" href="https://code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css" > <script type="text/javascript"> var inDesignMode = document.forms[MSOWebPartPageFormName].MSOLayout_InDesignMode.value ? document.forms[MSOWebPartPageFormName].MSOLayout_InDesignMode.value == "1" : false; var wikiInEditMode = document.forms[MSOWebPartPageFormName]._wikiPageMode ? document.forms[MSOWebPartPageFormName]._wikiPageMode.value == "Edit" : false; function tabify() { if (!inDesignMode && !wikiInEditMode) { $('.tabifyMe').each(function(){ var tabifyMeDiv = $(this); var theZone = tabifyMeDiv.closest(".ms-webpart-zone").length > 0 ? tabifyMeDiv.closest(".ms-webpart-zone") : tabifyMeDiv.closest(".ms-rte-layoutszone-inner"); var WebPartDivIDs = []; var tabsWebPartIDSuffix = tabifyMeDiv.closest(".ms-webpartzone-cell").attr('id').replace('MSOZoneCell_WebPart',''); //This will return something like WPQ2 //Put the Web Part Title for all the Web Parts you wish to exclude from being tabbed into the array below. //These web parts will be moved above or below the tabs area (depending on the existance of the data-location attribute of your .tabifyMe div) //The excluded web parts will display in the same order specified in the array below var excludedWebParts = []; if($.trim(tabifyMeDiv.text()) != "") { excludedWebParts = tabifyMeDiv.text().trim().split(","); //Finds webparts whose titles match the comma-separated list of titles inside the .tabifyMe div and pushes thir IDs to the array excludedWebParts for (i = 0; i < excludedWebParts.length; i++) { excludedWebParts[i] = theZone.find('.ms-webpart-titleText span').filter(function() { return $(this).text() === excludedWebParts[i].trim() || $(this).text().indexOf('tabify.txt') > -1; }).closest('div[id^=MSOZoneCell_WebPart]').attr('id'); } } // A true value indicates the tabs will be placed ABOVE all the excluded web parts within zone. // A false value indicates the tabs will be placed BELOW all the excluded web parts within zone. var tabsOnBottom = tabifyMeDiv.attr('data-location') == "bottom"; //hide the webpart containing the '.tabifyMe' div $('#MSOZoneCell_WebPart' + tabsWebPartIDSuffix).hide(); //for all web parts inside the web part zone, put them into the WebPartDivIDs array excluding the web parts inside the excludedWebParts array theZone.find('.ms-webpartzone-cell').each(function () { if($(this).attr('id') != 'MSOZoneCell_WebPart' + tabsWebPartIDSuffix && (($.inArray($(this).attr('id'),excludedWebParts)) < 0)) { WebPartDivIDs.push($(this).attr('id').replace('MSOZoneCell_WebPart','')); } }); tabifyZone(theZone, WebPartDivIDs); //Move the excluded webparts to above/below the tabs if(tabsOnBottom) { for (var i = excludedWebParts.length; i-- > 0; ) $('#' + excludedWebParts[i]).prependTo(theZone); } else { for (i in excludedWebParts) $('#' + excludedWebParts[i]).appendTo(theZone); } }) } } function tabifyZone(zone, webPartIDs) { zone.wrapInner( "<div class='tabsContainer'>" + "<ul class='tabsContainerInner'></ul>" + "</div>"); for (index = webPartIDs.length - 1; index > -1; index--) { var title = webPartIDs[index]; /*if($('#MSOZoneCell_WebPart' + title + ' div.ms-hide').length > 0) continue;*/ zone.find(".tabsContainerInner").prepend('<li><a href="#Tab' + title + '" id="TabHead' + title + '">' + $('span#WebPartTitle' + title + ' .ms-webpart-titleText').text() + '</a></li>'); $('div#MSOZoneCell_WebPart' + title).wrap('<div class="TabContent" id=Tab' + title + '></div>'); $('#WebPart' + title + '_ChromeTitle').remove(); } zone.find(".tabsContainer").tabs(); zone.find('.tabsContainer #' + $('.ui-tabs-active').attr('aria-controls')).show(); } _spBodyOnLoadFunctionNames.push("tabify"); </script> <style type="text/css"> .tabsContainer .ms-WPBorder {border:none;} .TabContent {margin-top:40px;} </style>
Note: As you can see, the script already references the jQuery and jQuery UI libraries. If your current website already references them elsewhere, then lines 1 and 2 of this script can be omitted.
- Reference the file you created in Step 1 on your page so that the code runs. You can do this in one of two ways:
- Create a new Content Editor Web Part on your page that points to the file. Please note that you must place this Content Editor Web Part in a separate zone from the one you wish to “tabify.” This solution works best if you only wish to “tabify” web parts on this page, and nowhere else on your site. OR
- Change the .txt file extension to a .js file and reference it on your master page. This solution works best if you wish to “tabify” multiple pages within your site and don’t want to create Content Editor Web Parts referencing the script for every page.
- Create a new Content Editor Web Part in the same zone as the other web parts you wish to “tabify.” This new CEWP should be located as the first web part in the zone. This content CEWP would contain the following HTML code in it:
<div class="tabifyMe"></div>
For the sake of this example, we will call this div the .tabifyMe div.
- If there are web parts within the zone that you do not want to be included in the “tabify” process, simply list their web part titles inside the .tabifyMe div, in a comma-separated list. Doing this will leave the listed web parts alone and not include them within the tabs
<div class="tabifyMe">Images</div>
- Create a new .txt. file with the following code in it. Upload the file somewhere on your website. For the sake of this example, I placed the file on my site’s /siteassets/ folder. Therefore, the URL will be /samples/siteassets/tabify.txt/
- If you are excluding web parts from being “tabified”, but would like these excluded web parts to be shown above the tabs, simply include the attribute data-location=bottom in the .tabifyMe div
<div class="tabifyMe" data-location="bottom">Images</div>
Voilà! I hope you enjoy making this work and feel free to leave a comment below!