SharePoint 2013 Dynamically Adding Dropdown Using REST API and jQuery

SharePoint 2013 Dynamically Adding Dropdown Using REST API and jQuery

In this article explains how to retrieve List Items and dynamically add a Dropdown using the REST API and JQuery. 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.


Read some SharePoint 2013 tutorials below:

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.


SharePoint 2013 Dynamically Adding Dropdown Using REST API and jQuery
SharePoint 2013 Dynamically Adding Dropdown Using REST API and jQuery

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.

SharePoint 2013 Dynamically Adding Dropdown Using REST API and jQuery
SharePoint 2013 Dynamically Adding Dropdown Using REST API and jQuery

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:



<script src=”http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.1.min.js”></script>
<script>

$(document).ready(function () {
countriesDrpDownBind();
});
function countriesDrpDownBind() {
var listName = “countries”;
var url = _spPageContextInfo.webAbsoluteUrl;

getListItems(listName, url, function (data) {
var items = data.d.results;

var inputElement = ‘<select id=”drpcountries”> <option value=””>Select</option>’;
// Add all the new items
for (var i = 0; i < items.length; i++) {
var itemId = items[i].Title,
itemVal = items[i].Title;
inputElement += ‘<option value=”‘ + itemId + ‘”selected>’ + itemId + ‘</option>’;

}
inputElement += ‘</select>’;
$(‘#divisiondrp’).append(inputElement);

$(“#drpcountries”).each(function () {
$(‘option’, this).each(function () {

if ($(this).text() == ‘Select’) {
$(this).attr(‘selected’, ‘selected’)
};
});
});
// assign the change event to provide an alert of the selected option value
$(‘#drpcountries’).on(‘change’, function () {
alert($(this).val());
});

}, function (data) {
alert(“Ooops, an error occured. Please try again”);
});
}
// READ operation
// listName: The name of the list you want to get items from
// siteurl: The url of the site that the list is in.
// success: The function to execute if the call is sucesfull
// failure: The function to execute if the call fails
function getListItems(listName, siteurl, success, failure) {
$.ajax({
url: siteurl + “/_api/web/lists/getbytitle(‘” + listName + “‘)/items?$orderby=Title asc”,
method: “GET”,
headers: { “Accept”: “application/json; odata=verbose” },
success: function (data) {
success(data);
},
error: function (data) {
failure(data);
}
});
}

</script>
Division
<div id=”divisiondrp”></div>



Finally the result looks as in the following:

rest api add dynamic dropdown to sharepoint 2013
rest api add dynamic dropdown to sharepoint 2013
Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Sagar Pardeshi

I am Developer working on Microsoft Technologies for the past 6+years. I am very much passionate about programming and my core skills are SharePoint, ASP.NET & C#,Jquery,Javascript,REST. I am running this blog to share my experience & learning with the community I am an MCP, MCTS .NET & Sharepoint 2010, MCPD Sharepoint 2010, and MCSD HTML 5,Sharepoint 2013 Core Solutions. I am currently working on Sharepoint 2010, MOSS 2007, Sharepoint 2013,Sharepoint 2013 App Dev, C#, ASP.NET, and SQL Server 2008.

View all posts by Sagar Pardeshi →