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?

    build

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

    type

  • You will see the screen below on the app,

    content

  • Click on Default.aspx and paste the code below under the,
    1. “<asp:ContentContentPlaceHolderID="PlaceHolderMain" runat="server">”.  
    Code:
    1. <div>  
    2.     <p>  
    3.         <b>All Site Columns</b>  
    4.         <br />  
    5.         <select style="height:300px; width:250px" multiple="multiple" id="getallsitecolumns"></select>  
    6.     </p>  
    7. </div>  
  • 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.   
    12.     varscriptlink = hostweblink + "/_layouts/15/";  
    13.     $.getScript(scriptlink + "SP.RequestExecutor.js", loadPage);  
    14. });  
    15.   
    16. //function to retrieve values   
    17. function getQueryStringParameter(paramval)  
    18. {  
    19.         var paramvalue = document.URL.split("?")[1].split("&");  
    20.         for (var i = 0; i < paramvalue.length; i = i + 1)   
    21.         {  
    22.             var Paramval1 = paramvalue[i].split("=");  
    23.             if (Paramval1[0] == paramval) return Paramval1[1];  
    24.         }  
    25.     }  
    26.     // load function of the app   
    27. function loadPage()  
    28. {  
    29.     getallSiteColumns();  
    30. }  
    31.   
    32. //Retrieve all of the site columns   
    33. function getallSiteColumns()  
    34. {  
    35.     var play;  
    36.     play = new SP.RequestExecutor(applink);  
    37.     play.executeAsync  
    38.     ({  
    39.         url: applink + "/_api/SP.AppContextSite(@target)/web/Fields?@target='" + hostweblink + "'",  
    40.         method: "GET",  
    41.         headers:   
    42.         {  
    43.             "Accept""application/json; odata=verbose"  
    44.         },  
    45.         success: getcolumnssuccessful,  
    46.         error: getcolumnsfail  
    47.     });  
    48. }  
    49.   
    50. //shows all Columns.  
    51. function getcolumnssuccessful(data)  
    52. {  
    53.     var jsonObjectval = JSON.parse(data.body);  
    54.     var selectallColumns = document.getElementById("getallsitecolumns");  
    55.     if (selectallColumns.hasChildNodes())   
    56.     {  
    57.         while (selectallColumns.childNodes.length >= 1)  
    58.         {  
    59.             selectallColumns.removeChild(selectallColumns.firstChild);  
    60.         }  
    61.     }  
    62.   
    63.     var result = jsonObjectval.d.results;  
    64.     for (var i = 0; i < result.length; i++)  
    65.     {  
    66.         varselectOption = document.createElement("option");  
    67.         selectOption.value = result[i].Title;  
    68.         selectOption.innerText = result[i].Title;  
    69.         selectallColumns.appendChild(selectOption);  
    70.     }  
    71. }  
    72.   
    73. functiongetcolumnsfail(data, errorCode, errorMessage)  
    74. {  
    75.     alert("Could not retrieve site columns, view he error " + errorMessage);  
    76. }  

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

    settings

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

    Permissions

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

    deploy

  • Click on the launch button.

    launch

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

    trust

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

    open

  • 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!

Read more articles on SharePoint:

Up Next
    Ebook Download
    View all
    Learn
    View all