Highlight Left Navigation Using JS And SharePoint

In this blog, we will take a look at creation of left navigation and highlighting categories based on URL value.

I have created one community site in SharePoint for internal discussion of our company. For that, I have used all out of box features. In that, I have created multiple categories based on the company categories/products. The requirement is that we need to show all the categories in the left side and after clicking on any one of that, it should navigate to that particular discussion.

Below are the steps I followed.

  • Create community site using Site Contents -> New subsite -> give name to that site.

  • Select Community site template from collaboration menu-> select permission option to inherit parent permission or not, and click on "Create" button.

  • After creation of site, open root site in designer using run as administrator option, go to Master pages -> create new master page (discussionforumMaster.html) using Seattle. 

  • Check out discussionforumMaster.html and open it for editing.

  • Now, we will add one div in master page for left navigation to appear on each page.

  • Search “ms-core-listMenu-verticalBox” in discussionforumMaster.html and add the below code after the end of this div (<div class="ms-core-listMenu-verticalBox">).
  1. <div id="ForumleftNavigation" style="margin-left: 22px;font-size: 15px;" class="forum-left-navigation"></div>  

This div will appear on left side.

  • Now, we will dynamically bind categories to the div that we have added in master page from categories list of SharePoint.

Create one JS file and add the below code into it.

  1. $(document).ready(function() {  
  2.     getCategories();  
  3. });  
  4.   
  5. function getCategories() {  
  6.     $.ajax({  
  7.         url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Categories')/items",  
  8.         method: "GET",  
  9.         headers: {  
  10.             "Accept""application/json; odata=verbose"  
  11.         },  
  12.         success: function(data) {  
  13.             if (data.d.results.length > 0) {  
  14.                 var resultDiv = "<ul>";  
  15.                 $.each(data.d.results, function(index, item) {  
  16.                     var categoryurl = _spPageContextInfo.webAbsoluteUrl + "/SitePages/Category.aspx?CategoryID=" + item.ID + "&SiteMapTitle=" + item.Title;  
  17.                     resultDiv = resultDiv + "<li id='" + item.Title + "'><a href='" + categoryurl + "' style='text-decoration:none;'>" + item.Title + "</a></li>";  
  18.                 });  
  19.                 resultDiv += "</ul>";  
  20.                 $("#ForumleftNavigation").append(resultDiv);  
  21.             }  
  22.         },  
  23.         error: function(data) {  
  24.             alert("Error: " + data);  
  25.         }  
  26.     });  
  27. }  
  28. $(window).load(function() {  
  29.     var category = getUrlParameter('SiteMapTitle');  
  30.     $('#' + category).css("background""#d8d6d6");  
  31. });  
  32.   
  33. function getUrlParameter(name) {  
  34.     name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');  
  35.     var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');  
  36.     var results = regex.exec(location.search);  
  37.     return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));  
  38. };  

For CSS, create one CSS file and add the below styles into it.

  1. .ms - webpart - titleText {  
  2.     font - family: "Lucida Sans Unicode""Lucida Grande""sans-serif"!important;  
  3.     font - size: 18 px!important;  
  4.     color: black!important;  
  5. }.ms - comm - activityStatsNumber.ms - largeNumber {  
  6.     color: black!important;  
  7. }.ms - comm - activityStatsItem {  
  8.     color: black!important;  
  9.     font - size: 14 px!important;  
  10. }.ms - textSmall.ms - srch - sb - prompt.ms - helperText {  
  11.     color: black!important;  
  12. } #ForumleftNavigation ul li a {  
  13.     font - family: "Lucida Sans Unicode""Lucida Grande""sans-serif"!important;  
  14. }  
  15. span.ms - textLarge.ms - noWrap {  
  16.     font - family: "Lucida Sans Unicode""Lucida Grande""sans-serif"!important;  
  17.     font - size: 16 px;  
  18. }  
  19. h1 #pageTitle {  
  20.     display: none!important;  
  21. }.ms - accentText.ms - textXLarge {  
  22.     font - family: "Lucida Sans Unicode""Lucida Grande", sans - serif!important;  
  23.     font - size: 20 px!important;  
  24. }#ForumleftNavigation ul {  
  25.     list - style - type: none;  
  26.     margin: 0;  
  27.     padding: 0;  
  28.     width: 160 px;  
  29.     border: 1 px solid #887f7f;  
  30. }  
  31. #ForumleftNavigation ul li a {  
  32.             display: block;  
  33.             color: #000;  
  34. padding: 8px 16px;  
  35. text-decoration: none;  
  36. }  
  37. #ForumleftNavigation ul li {  
  38.                 border - bottom: 1 px solid #bbb0b0;  
  39.             }#ForumleftNavigation ul li: last - child {  
  40.                 border - bottom: none;  
  41.             }.ms - comm - postListItem {  
  42.                 padding: 10 px;  
  43.                 border - bottom: 1 px solid #eae1e1;  
  44.                 font - family: "Lucida Sans Unicode",  
  45.                 "Lucida Grande",  
  46.                 sans - serif!important;  
  47.             }  
Save both the .css and the .js files and reference these files into master page. Check in master and apply to the community site that we have created.

All categories will appear on the left side and when we redirect to the categories discussion, it will highlight that category.

Thank you.