How do I convert my web parts into tabs?

Tabify

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:

  1. This solution has been tested to work for SharePoint 2013 and SharePoint within Office 365 only.
  2. All web parts that you want to “tabify” should be placed within the same web part zone on your page.

 

LIST OF STEPS:

  1. Create a new .txt. file with the following code in it.  Upload the file somewhere on your web site.  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.

  2. Reference the file you created in Step 1 in 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 than the one you wish to “tabify.”  This solution works best if you only wish to “tabify” web parts on this page, and nowhere else in your site.OR
    • Change the .txt file extension to a .js file and reference in 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.
  3. 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.

  4. 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>
       
    

    Tabify2.jpg

  5. 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>
       
    

    Tabify3.jpg

Voilà!  I hope you enjoy making this work and feel free to leave a comment below!

All SharePoint Versions

The web parts are functional components that extend your SharePoint environment whether it’s hosted, on-premises, or part of Microsoft® Office 365.

SharePoint 2013, 2016, 2019, Online (Office 365)

On-Premises Only

These web parts extend SharePoint beyond its out-of-the-box capabilities by tailoring it to your requirements with Bamboo Solution’s growing portfolio of SharePoint Web Parts.

SharePoint 2013, 2016, 2019

SharePoint

Product Suites

Experience greater power and savings by bundling our SharePoint apps and web parts.


Essentials Suite


Essentials Plus Suite


Bamboo Premier Suite


Project Management Suite


Knowledge Management Suite


External User Manager


 

For more information on our product suites, contact us.

Featured Services

SharePoint Health Check

A SharePoint Health Check will identify the causes of issues and risks associated with your specific environment, and is custom tailored to provide you with the best recommendations to optimize your SharePoint environment.

SQL Health Check

Document recommendations relating to performance, stability, availability, or a specific focus you request of your SQL Server database instances.

My SharePointXperts

The truth is that each SharePoint skill may not be a full time job for many organizations, and it is nearly impossible for one person to do everything you need – so augment your team with SharePointXperts; providing the skill sets you need when you need them!