top of page
Writer's pictureNakkeeran Natarajan

How To Get All Site Columns Using REST API In SharePoint Online And Office 365

Welcome to an article on “How to get all Site Columns using REST API in SharePoint Online and Office 365” where we will see the steps of creating an app using Napa Tool which will help us to view all the site columns on our site using REST API.

  • Open the “NAPA” Office 365 Development Tools through your SharePoint store.

  • Click on Add New Project.

  • It will ask you, what type of app do you want to build?

  • Select SharePoint Add-in and provide a name to your app and click on Create.


  • You will see the screen below on the app,

  • Click on Default.aspx and paste the code below under the,

  1. “<asp:ContentContentPlaceHolderID=“PlaceHolderMain” runat=“server”>”.

Code:

All Site Columns       

  1.  “height:300px; width:250px” multiple=“multiple” id=“getallsitecolumns”>

  • Now on the navigation click on the App.js file and paste the code below removing the previous code completely.

Code:

  1. ‘use strict’;

  2. var hostweblink;

  3. var applink;

  4. // Load the links on app load

  5. $(document).ready(function()

  6. {   

  7.  hostweblink = decodeURIComponent(        

  8. getQueryStringParameter(“SPHostUrl”));    

  9. applink = decodeURIComponent(        

  10. getQueryStringParameter(“SPAppWebUrl”));    


  11. varscriptlink = hostweblink + “/_layouts/15/”;    

  12. $.getScript(scriptlink + “SP.RequestExecutor.js”, loadPage);

  13. });


  14. //function to retrieve values 

  15. function getQueryStringParameter(paramval)

  16. {        

  17. var paramvalue = document.URL.split(“?”)[1].split(“&”);

  18. for (var i = 0; i < paramvalue.length; i = i + 1)        

  19. {            

  20. var Paramval1 = paramvalue[i].split(“=”);

  21. if (Paramval1[0] == paramval) return Paramval1[1];        

  22. }    

  23. }


  24. // load function of the app 

  25. function loadPage()

  26. {    

  27. getallSiteColumns();

  28. }


  29. //Retrieve all of the site columns 

  30. function getallSiteColumns(){    

  31. var play;    

  32. play = new SP.RequestExecutor(applink);    

  33. play.executeAsync    (

  34. {        

  35. url: applink + “/_api/SP.AppContextSite(@target)/web/Fields?@target='” + hostweblink + “‘”,        

  36. method: “GET”,       

  37. headers:        

  38. {

  39. “Accept”: “application/json; odata=verbose”        

  40. },        

  41. success: getcolumnssuccessful,        

  42. error: getcolumnsfail    

  43. });

  44. }


  45. //shows all Columns.

  46. function getcolumnssuccessful(data)

  47. {    

  48. var jsonObjectval = JSON.parse(data.body);   

  49. var selectallColumns = document.getElementById(“getallsitecolumns”); if (selectallColumns.hasChildNodes())    

  50. {

  51. while (selectallColumns.childNodes.length >= 1)        

  52. {            

  53. selectallColumns.removeChild(selectallColumns.firstChild);        

  54. }    

  55. }    

  56. var result = jsonObjectval.d.results;

  57. for (var i = 0; i < result.length; i++)    

  58. {       

  59.  varselectOption = document.createElement(“option”);        

  60. selectOption.value = result[i].Title;        

  61. selectOption.innerText = result[i].Title;       

  62.  selectallColumns.appendChild(selectOption);    

  63. }

  64. }

  65. functiongetcolumnsfail(data, errorCode, errorMessage)

  66. {    

  67. alert(“Could not retrieve site columns, view he error “ + errorMessage);

  68. }

  • Click on the settings icon on the tool on the left.

  • Under the properties, select Permissions and provide full control to the app on the Site Collection level.

  • Click on the deploy button on the left and run the project.

  • Click on the launch button.

  • Accept the trust and click on ‘Trust It’.

  • Your app will be deployed and open for you as per the following screenshot:

  • Your entire site columns will load on your app on the page load of the app.


Here we saw today how to get all site columns using REST API in SharePoint Online and Office 365. You will love your app. Keep reading and keep learning!

0 comments

Recent Posts

See All

Comments


bottom of page