SharePoint: Add-in To Show List Data In Accordion View

We will create a SharePoint hosted app. To know more on how to create a SharePoint App, check this link.

  1. In the newly-created App project open the default.aspx page.

    Replace the existing div with the following code:
    1. <div id="configSection" style="padding-top:15px">  
    2.     <table class="table">  
    3.         <tr>  
    4.             <td>List Name: </td>  
    5.             <td>  
    6.                 <input type="text" id="listName" /> </td>  
    7.         </tr>  
    8.         <tr>  
    9.             <td>Header Column Name: </td>  
    10.             <td>  
    11.                 <input type="text" id="headerColumn" /> </td>  
    12.         </tr>  
    13.         <tr>  
    14.             <td>Body Column Name: </td>  
    15.             <td>  
    16.                 <input type="text" id="bodyColumn" /> </td>  
    17.         </tr>  
    18.         <tr>  
    19.             <td colspan="2">  
    20.                 <div class="btn btn-primary" id="btnConfig">Configure</div>  
    21.             </td>  
    22.         </tr>  
    23.     </table>  
    24. </div>  
    25. <div class="panel-group" id="accordion" style="padding-top:15px">  
    26. </div>  
  2. As we will be using the bootstrap accordion we need to add the following references to the page.
    1. <!-- Latest compiled and minified CSS -->  
    2. <link rel="stylesheet"href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">  
    3.   
    4. <!-- Latest compiled and minified JavaScript -->  
    5. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
  3. The next step is to set up the list from which we will pull the data for the accordion. Let’s keep the structure flat and simple. The list will have two columns, Head and Body. Here the data in the ‘Head’ column will map to the header of the accordion section and the respective body will be mapped with the ‘Body’ column.

    The list will appear as below.

    Test list
  4. Now move back to the app project in Visual Studio. We need to write the code in the App.js to pull the list data and bind it to the accordion div. Remove the existing code in the App.js and paste the below code.
    1. var context = SP.ClientContext.get_current();  
    2. var user = context.get_web().get_currentUser();  
    3. var spHost = window.location.href.slice(window.location.href.indexOf('SPHostUrl')).split('&');  
    4. var configListItemCollection = null;  
    5. var listAccordian = null;  
    6. var listAccordianCollection = null;  
    7. for (var i = 0; i < spHost.length; i++)  
    8. {  
    9.     var urlparam = spHost[i].split('=');  
    10.     if (urlparam[0] == 'SPHostUrl')  
    11.     {  
    12.         spHost = decodeURIComponent(urlparam[1]);  
    13.     }  
    14. }  
    15. $(document).ready(function ()  
    16. {  
    17.     $("#btnConfig").click(function ()  
    18.     {  
    19.         createAccordian();  
    20.     });  
    21. });  
    22. ///Create the accordian  
    23. function createAccordian()  
    24. {  
    25.     var clientContext = new SP.AppContextSite(context, spHost)  
    26.     var web = clientContext.get_web();  
    27.     context.load(web)  
    28.     var listName = $("#listName").val();  
    29.     listAccordian = web.get_lists().getByTitle(listName);  
    30.     var camlQuery = new SP.CamlQuery();  
    31.     camlQuery.set_viewXml('<View><RowLimit>5000</RowLimit></View>');  
    32.     listAccordianCollection = listAccordian.getItems(camlQuery);  
    33.     context.load(listAccordianCollection);  
    34.     context.load(listAccordian);  
    35.     context.executeQueryAsync(loadAccordianData, onQueryFailed);  
    36. }  
    37.   
    38. function loadAccordianData()  
    39. {  
    40.     var listEnumerator = listAccordianCollection.getEnumerator();  
    41.     //Get the textbox values  
    42.     var headerColumn = $("#headerColumn").val();  
    43.     var bodyColumn = $("#bodyColumn").val();  
    44.     var c = 0;  
    45.     while (listEnumerator.moveNext())  
    46.     {  
    47.         var oItem = listEnumerator.get_current();  
    48.         var headerContent = oItem.get_item(headerColumn);  
    49.         var bodyContent = oItem.get_item(bodyColumn)  
    50.         c = c + 1;  
    51.         var stringVal = "<div class='panel panel-default'>\  
    52. <div class='panel-heading'>\  
    53. <h4 class='panel-title'>\  
    54. <a data-toggle='collapse' data-parent='#accordion' href='#collapse" + c + "'>" + headerContent + "</a>\  
    55. </h4>\  
    56. </div>\  
    57. <div id='collapse" + c + "' class='panel-collapse collapse in'>\  
    58. <div class='panel-body'>" + bodyContent + "</div>\  
    59. </div>\  
    60. </div>";  
    61.         $('#accordion').append(stringVal);  
    62.     }  
    63.     $("#configSection").hide();  
    64. }  
    65.   
    66. function onQueryFailed(sender, args)  
    67. {  
    68.     alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
    69. }  
  5. In the above code, we have first declared some global variables, then retrieved the SPHost url to create the client context.

  6. Initially we are taking user inputs List Name, Header Column Name and Body Column Name. Once the user clicks on the Configure button the accordion is created.

    Configure
  7. On the ‘Configure’ button click the ‘createAccordian()’ method is executed, which asynchronously loads data in the ‘loadAccordianData()’ method and binds with the accordion div.

    loadAccordianData
  8. You need to give the App (Add-in) permission to read the site collection data. This change has to be done in the App manifest.

    Add-in

This Add-in can be extended to save the source list details in another config list and load the data on each page load, this way we can avoid taking user inputs every time.

Up Next
    Ebook Download
    View all
    Learn
    View all