Dynamically Adding Dropdown Using REST API and jQuery

Introduction

SharePoint 2013 has greatly expanded the REST services available to developers. With this, we have much more SharePoint functionality exposed via CSOM and Web Services. Also, all of the new REST Services are in SharePoint 2013. SharePoint 2013 was able to provide me with a REST API, I could call with jQuery ajax requests and this was exactly what I wanted.

REST Services-High Level Overview

Let's start out with our basic get commands in REST. The following is a list of the basic commands used to get List Items from a SharePoint List using the SharePoint 2013 REST Services.



Imagine

In my example, I'm accessing a Custom list (of countries) and output the result binding it to a dynamic dropdown. I have sorted by a column in ascending only. Using SharePoint's REST API lets us add these filters to our request. The results are given to us as a JSON object that we can then loop through and insert into a dropdown runtime. I also used a modular pattern to structure my code. We can generate our REST request. _spPageContextInfo is a SharePoint object that gives us useful information about the page and site we're on, including the base URL of our site.

After successfully getting our list information, we just need to loop through our data, put it in a dropdown and then inserted into our predefined container element. jQuery helps make this an easy process.



Let's proceed

Step 1: Navigate to your SharePoint 2013 site.

Step 2: From this page select the Site Actions | Edit Page.

Edit the page, go to the "Insert" tab in the Ribbon and click the "Web Part" option. In the "Web Parts" picker area, go to the "Media and Content" category, select the "Script Editor" Web Part and press the "Add button".

Step 3: Once the Web Part is inserted into the page, you will see an "EDIT SNIPPET" link; click it. You can insert the HTML and/or JavaScript as in the following:

  1. <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.1.min.js"></script>  
  2. <script>  
  3.   
  4.     $(document).ready(function () {  
  5.         countriesDrpDownBind();  
  6.       });  
  7.     function countriesDrpDownBind() {  
  8.         var listName = "countries";  
  9.         var url = _spPageContextInfo.webAbsoluteUrl;  
  10.   
  11.         getListItems(listName, url, function (data) {  
  12.             var items = data.d.results;  
  13.              
  14.             var inputElement = '<select id="drpcountries"> <option  value="">Select</option>';  
  15.                // Add all the new items  
  16.             for (var i = 0; i < items.length; i++) {  
  17.                  var itemId = items[i].Title,  
  18.                    itemVal = items[i].Title;  
  19.                  inputElement += '<option value="' + itemId + '"selected>' + itemId + '</option>';  
  20.                 
  21.                }  
  22.                 inputElement += '</select>';  
  23.                 $('#divisiondrp').append(inputElement);  
  24.   
  25.               $("#drpcountries").each(function () {  
  26.                 $('option'this).each(function () {  
  27.   
  28.                     if ($(this).text() == 'Select') {  
  29.                         $(this).attr('selected''selected')  
  30.                     };  
  31.                 });  
  32.             });  
  33.                // assign the change event to provide an alert of the selected option value  
  34.               $('#drpcountries').on('change'function () {  
  35.               alert($(this).val());  
  36.                   });  
  37.              
  38.           }, function (data) {  
  39.             alert("Ooops, an error occured. Please try again");  
  40.         });  
  41.     }  
  42.     // READ operation  
  43.     // listName: The name of the list you want to get items from  
  44.     // siteurl: The url of the site that the list is in.  
  45.     // success: The function to execute if the call is sucesfull  
  46.     // failure: The function to execute if the call fails  
  47.     function getListItems(listName, siteurl, success, failure) {  
  48.         $.ajax({  
  49.             url: siteurl + "/_api/web/lists/getbytitle('" + listName + "')/items?$orderby=Title asc",  
  50.             method: "GET",  
  51.             headers: { "Accept""application/json; odata=verbose" },  
  52.             success: function (data) {  
  53.                 success(data);  
  54.             },  
  55.             error: function (data) {  
  56.                 failure(data);  
  57.             }  
  58.         });  
  59.     }  
  60.   
  61. </script>  
  62. Division  
  63. <div id="divisiondrp"></div>  
Finally the result looks as in the following:

Up Next
    Ebook Download
    View all
    Learn
    View all