Display Notification Button With Count In SharePoint Page

In this article, we are going to see how to insert a notification button in our Sharepoint page using HTML and display the count of custom list item as a notification number using REST API methodology.

In this article, we are going to see how to insert a notification button in our SharePoint page using HTML and display the count of custom list item as a notification number using REST API methodology.

As a first step, we need to create an HTML source to display the notification button, so here, I am using the bell icon to display as a notification button on my SharePoint Page.

  1. <html>  
  2. <head>  
  3. <meta name="viewport" content="width=device-width, initial-scale=1">  
  4. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">  
  5. <style>  
  6. .btn {  
  7.   background-color#eb94d0;  
  8.   colorwhite;  
  9.   text-decorationnone;  
  10.   padding8px 10px;  
  11.   positionrelative;  
  12.   display: inline-block;  
  13.   border-radius: 2px;  
  14. }  
  15. .btn:hover {  
  16.    backgroundred;  
  17. }    
  18. .btn .badge {  
  19.    positionabsolute;  
  20.   top: -10px;  
  21.   right: -10px;  
  22.   padding2px 5px;  
  23.   border-radius: 50%;  
  24.   background-colorred;  
  25.   colorwhite;  
  26. }  
  27. </style>  
  28. </head>  
  29. <body>  
  30. <a href="#" class="btn">  
  31.   <span class="fa fa-bell"></span>  
  32.   <span class="badge">0</span>  
  33.   </a>  
  34. </body>  
  35. </html>  

The result of the above HTML code is as below.

Display Notfication Button With Count In SharePoint Page 
Now, I am going to call the REST API to get the number of items available in the custom list and going to display that count on my notification button.

Here, I have created one custom list called “Test” and I have added some items in that.

Before proceeding, we need to verify that we are getting the expected result by using the below given URL structure.

  • https://SharePointUrl.com/sites/Subsites/_Vti_bin/ListData.SVC/ListName/$Count
  • https://SharePointUrl.com/sites/Subsites/_api/web/lists/GetByTitle('ListName')/ItemCount

Once you got the item count, then you can implement this REST API URL to the below given code.

  1. <script src="https://code.jquery.com/jquery-1.11.3.min.js" type="text/javascript" unselectable="on"></script>      
  2. <script type="text/javascript" unselectable="on">      
  3. $(document).ready( function() {      
  4.                 var itemCont = 0;      
  5.                 var requestUri = "https://sharepoint.com/sites/subsite/_vti_bin/ListData.svc/Test/$count";    
  6.     
  7.         $.ajax({      
  8.         url: requestUri,      
  9.         method: "GET",      
  10.         headers: { "Accept""text/plain;odata=verbose" },    
  11.         success: function (data) {      
  12.             $("#Count").html(data);                   
  13.         },      
  14.         error: function (data) {      
  15.             console.log(data);      
  16.         }      
  17.     });      
  18. });      
  19. </script>    

And, the final code will be generated by combining the HTML and REST API code below.

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta name="viewport" content="width=device-width, initial-scale=1">  
  5. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">  
  6. <style>  
  7. .btn {  
  8.   background-color: #eb94d0;  
  9.   color: white;  
  10.   text-decoration: none;  
  11.   padding: 8px 10px;  
  12.   position: relative;  
  13.   display: inline-block;  
  14.   border-radius: 2px;  
  15. }  
  16.   
  17. /* Darker background on mouse-over */  
  18. .btn:hover {  
  19.    background: red;}  
  20.     
  21. .btn .badge {  
  22.    position: absolute;  
  23.   top: -10px;  
  24.   right: -10px;  
  25.   padding: 2px 5px;  
  26.   border-radius: 50%;  
  27.   background-color: red;  
  28.   color: white;  
  29. }  
  30. </style>  
  31.   
  32. <script src="https://code.jquery.com/jquery-1.11.3.min.js" type="text/javascript" unselectable="on"></script>  
  33.   
  34. <script type="text/javascript" unselectable="on">  
  35.   
  36. $(document).ready( function() {  
  37.   
  38.                 var itemCont = 0;  
  39.   
  40.                 var requestUri = "https://sharepoint.com/sites/subsite/_vti_bin/ListData.svc/Test/$count";  
  41.   
  42.         $.ajax({  
  43.   
  44.         url: requestUri,  
  45.   
  46.         method: "GET",  
  47.   
  48.         headers: { "Accept""text/plain;odata=verbose" },  
  49.   
  50.         success: function (data) {  
  51.   
  52.             $("#Count").html(data);               
  53.   
  54.         },  
  55.   
  56.         error: function (data) {  
  57.   
  58.             console.log(data);  
  59.   
  60.         }  
  61.   
  62.     });  
  63.   
  64. });  
  65.   
  66. </script>  
  67. </head>  
  68. <body>  
  69.   
  70. <a href="#" class="btn">  
  71.   <span class="fa fa-bell"></span>  
  72.   <span class="badge">  
  73.   <span id="Count">0</span>  
  74.   </span>  
  75.   </a>  
  76. </body>  
  77. </html>  
Add the entire code in the script editor and you can see the preview in the bottom part of the script editor with given default value 0.
 
Display Notfication Button With Count In SharePoint Page
 
Now, click "Insert" and save the page. Once the page loads successfully, the value zero (0) will get changed based on the Item count. In my list, I have 4 items so my result is 4 and the final output would be like the below image.
 
You can add the code anywhere in your SharePoint page and render the result faster. You can insert this code in your Master Page. 
 
Display Notfication Button With Count In SharePoint Page