In this blog, we will see how we can use REST API with SharePoint lists to display data. Many times, we need to fetch the data from SharePoint list with the type of parameters; eg., Title, date etc.
Below are some code snippets that are useful in daily list operations. First, we need to add reference to jQuery on top of the code.
Below are some code snippets that are useful in daily list operations. First, we need to add reference to jQuery on top of the code.
- <script src="/SiteAssets/JS/jquery.min.js"></script>
Get list item using ID column - The below code is used to get a specific item using default ID column of SharePoint list.
- $(document).ready(function() {
- getItems(_spPageContextInfo.webAbsoluteUrl, function(data) {
- var title = data.d.Title;
- var desc = data.d.Description;
- }, function(data) {
- alert("Ooops, an error occured. Please try again");
- });
- });
- function getItems(siteurl, success, failure) {
- $.ajax({
- url: siteurl + "/_api/web/lists/getbytitle('CustomlistName')/items(12)",
- method: "GET",
- headers: {
- "Accept": "application/json; odata=verbose"
- },
- success: function(data) {
- success(data);
- },
- error: function(data) {
- failure(data);
- }
- });
- }
Get all items of list order by date ascending order -The below code is used to get all items ordered by ascending date where date is of type date and time.
- function getListItems(siteurl, success, failure) {
- $.ajax({
- url: siteurl + "/_api/web/lists/getbytitle('CustomlistName')/items?$orderby=Date asc",
- method: "GET",
- headers: {
- "Accept": "application/json; odata=verbose"
- },
- success: function(data) {
- success(data);
- },
- error: function(data) {
- failure(data);
- }
- });
- }
Get all list items with oder by and filter - The below code is used to get all list items ordered by title having specific values in the title. We can also set selected value of drop down to valSelected variable.
- function getDetails(siteurl, success, failure) {
- var valSelected = "Specifc string to filter";
- $.ajax({
- url: siteurl + "/_api/web/lists/getbytitle('CustomlistName')/items?$orderby=Title asc&$filter=Title eq '" + valSelected + "'",
- method: "GET",
- headers: {
- "Accept": "application/json; odata=verbose"
- },
- success: function(data) {
- success(data);
- },
- error: function(data) {
- failure(data);
- }
- });
- }
Suppose, we have a custom list of type promoted links and we want to change the URL of that tile based on the current user's location; then we can add the below code to change the URL of the tile's run time.
We are using getuserprofilepropertyfor function to get office property from SharePoint user profile.
We are using getuserprofilepropertyfor function to get office property from SharePoint user profile.
- <script type="text/javascript">
- $(document).ready(function() {
- setURL();
- });
- function setURL() {
- var loginName = _spPageContextInfo.userLoginName;
- $.ajax({
- url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/getuserprofilepropertyfor(accountName=@v,%20propertyname='Office')?@v=%27i%3A0%23.f|membership|" + loginName + "%27",
- type: "GET",
- headers: {
- "accept": "application/json;odata=verbose",
- "content-Type": "application/json;odata=verbose"
- },
- success: function(data) {
- if (data.d.GetUserProfilePropertyFor == "OfficeLocation1") {
- $("#Tile_WPQ2_15_3").attr("clickaction", "PreventDefaultNavigation(); window.open(STSPageUrlValidation('\URL of ur list view to show for users having office OfficeLocation1'),'_blank'); SP.QoS.WriteUserEngagement('PromotedLinksTileClick_NewTabNavigation'); return false;");
- } else if (data.d.GetUserProfilePropertyFor == "OfficeLocation2" || data.d.GetUserProfilePropertyFor == "OfficeLocation3") {
- $("#Tile_WPQ2_15_3").attr("clickaction", "PreventDefaultNavigation(); window.open(STSPageUrlValidation(' URL of ur list view to show for users having office OfficeLocation2 or OfficeLocation3'),'_blank'); SP.QoS.WriteUserEngagement('PromotedLinksTileClick_NewTabNavigation'); return false;");
- } else {
- $("#Tile_WPQ2_15_3").attr("clickaction", "PreventDefaultNavigation(); window.open(STSPageUrlValidation('URL of ur list view to show for users having location other than above'),'_blank'); SP.QoS.WriteUserEngagement('PromotedLinksTileClick_NewTabNavigation'); return false;");
- }
- },
- error: function(error) {
- alert(JSON.stringify(error));
- }
- });
- }
- </script>
Thanks for reading this blog.

Bob SmithPosted Jan 16, 2020, 12:25 AM
Thank you for this article! I'm having issues writing a code that pulls list values from the same column to be inserted in an html table cells in random places, not just top to bottom list. Would you be able to help to show how to use the REST API to store these values individually to call out in the html?
Piyush AgarwalPosted Mar 8, 2018, 11:45 AM
Nice article to begin with using REST....