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:
- <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>';
-
- 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')
- };
- });
- });
-
- $('#drpcountries').on('change', function () {
- alert($(this).val());
- });
-
- }, function (data) {
- alert("Ooops, an error occured. Please try again");
- });
- }
-
-
-
-
-
- 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: